example for interface
// mcve for interface with different classes
// draw program
// the nodes
ArrayList<Node> nodes = new ArrayList();
// data for drawing a rect
boolean startingRect=false;
PVector start;
// --------------------------------------------
// Core functions
void setup() {
size(900, 800);
// add new EllipseClass/RectClass with random color
color randomColor = color(random(0, 255), random(0, 255), random(0, 255));
nodes.add ( new EllipseClass(44, 44,
randomColor));
nodes.add ( new EllipseClass(111, 111,
randomColor));
nodes.add ( new RectClass(211, 211,
16, 16,
randomColor));
} // func
void draw() {
// clear screen
background(255);
// for-loop
for (int i = 0; i < nodes.size (); i++) {
Node someClass = nodes.get(i);
someClass.display();
} // for
// small demo
if (frameCount>60 && frameCount<64) {
Node someClass = nodes.get(1);
someClass.incrementXY(22, 0); // change position
someClass.incrementXY(33, 0);
} //
if (startingRect) {
rect(start.x, start.y, mouseX-start.x, mouseY-start.y);
stroke(255, 0, 0);
line(start.x-9, start.y,
start.x+9, start.y);
line(start.x, start.y-9,
start.x, start.y+9);
stroke(255);
fill(0);
text("Click again to store rect (press space bar to abort)", 19, 19);
text("Click again", mouseX+19, mouseY+9);
} else {
fill(0);
text("Click and release to start drawing a rect", 19, 19);
}
} // function draw()
// ----------------------------------------------------------------------
void mousePressed() {
if (!startingRect) {
//first click
startingRect=true;
start = new PVector(mouseX, mouseY);
} else {
//2nd click
startingRect=false;//reset
// add new RectClass with random color
color randomColor = color(random(0, 255), random(0, 255), random(0, 255));
nodes.add ( new RectClass(start.x, start.y,
mouseX-start.x, mouseY-start.y,
randomColor));
start=null;
}
}
void keyPressed() {
// abort rect
if (startingRect) {
startingRect=false;//reset
start=null;
}
}
// ==============================================
// interface and classes
class RectClass implements Node {
// pos
float x=0;
float y=0;
float w, h;
// color
color RectClassColor=0;
// constr
RectClass(float tempX, float tempY,
float wtemp, float htemp,
color tempRectClassColor) {
x = tempX;
y = tempY;
w=wtemp;
h=htemp;
RectClassColor = tempRectClassColor;
//
} // constr
void display() {
fill(RectClassColor);
rect (x, y, w, h);
}
void setXY(float x_, float y_) {
x=x_;
y=y_;
}
void incrementXY(float x_, float y_) {
x+=x_;
y+=y_;
}
//
} // class
// -------------------------
class EllipseClass implements Node {
// pos
float x=0;
float y=0;
// color
color RectClassColor=0;
// constr
EllipseClass(float tempX, float tempY,
color tempRectClassColor) {
x = tempX;
y = tempY;
RectClassColor = tempRectClassColor;
//
} // constr
//
void display() {
fill(RectClassColor);
ellipse (x, y, 15, 15);
}
void setXY(float x_, float y_) {
x=x_;
y=y_;
}
void incrementXY(float x_, float y_) {
x+=x_;
y+=y_;
}
//
} // class
// -------------------------------------
interface Node {
// Let's abstract
// Let's abstract display()
void display();
void incrementXY(float x_, float y_);
}
//