Kisama329
Kisama329

Reputation: 639

How can I make a file of a class in processing?

I am making OOP in processing. I am using eclipse, and I am making really big classes for my objects. I don't know how to write the code of my classes in other java file. Could you help me with an example, or tell me how can I split in two documents my code. One file for the setup and draw functions , and other for my PhysicalObject class.

public class MyProcessingSketch extends PApplet {

int i;
PhysicalObject Person;
public void setup() 
{
    Person= new PhysicalObject();
    size(400,400);
    smooth();
    background(0);
  }

 public void draw() 
 {
     //background(24,20,100);
     fill(255,0,0);
     Person.drawObject();
 }

 public class PhysicalObject {
        float objectWidth;
        float objectHeight;
        float posX;
        float posY;
        PhysicalObject()
        {
            posX=width/2;
            posY=height/2;
            objectWidth=50;
            objectHeight=50;

        }
        PhysicalObject(float x, float y,float widthn,float heightn)
        {
            objectWidth=widthn;
            objectHeight=heightn;
            setCenter(x, y);
        }

        public void setCenter(float x, float y)
        {
            posX=x;
            posY=y;
        }

        public void drawObject()
        {
            rectMode(CENTER);
            fill(255,0,0);
            rect(posX,posY,objectWidth,objectHeight);
        }
        public boolean isCollided()
        {
            return true;
        }
        public void drive()
        {

        }

    }

}

Upvotes: 0

Views: 1003

Answers (3)

Kisama329
Kisama329

Reputation: 639

private PApplet p; 

PhysicalObject(PApplet _p) {p=_p

Ok for people who have the same problem as me. You just make another file of your class but remember to make an instance of PApplet, because it is like context in android. so if you want to draw a rectangle in your class just put p.rect(.,.,.,.);

Upvotes: 0

George Profenza
George Profenza

Reputation: 51867

Part of the problem is that in the Processing IDE, all the different tabs become a single java file, so all the extra classes are nested classes of a single main PApplet subclass.

As @fxm suggest, you should be able to move classes to separate files, but you need to be aware of a few gotchas, mainly PApplet code in your subclasses. If it's simply drawing functions, you can pass a PGraphics reference, otherwise, you can pass your whole PApplet as a reference to the classes to access.

I've posted a few tips in this answer which also includes a video of the refactoring process involved in moving classes into separate files.

Upvotes: 0

merours
merours

Reputation: 4106

The simplest way is to put each class in a separate file (in eclipse, file/new/other/class). You'll have to provide a package (which can be considered as the folder containing the class). The best option is to make semantic packaging (for instance, you may put PhysicalObject in the hitbox package)

You'll then have to add these various classes to your classpath, ie a variable containing the path of all classes of your project. If you created a java project in eclipse, this will be done automatically.

Once your classpath is set, you'll be able to import the PhysicalObject into MyProcessingSketch with the command import hitbox.PhysicalObject.

Upvotes: 1

Related Questions