Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created December 9, 2011 22:15
Show Gist options
  • Save zeffii/1453555 to your computer and use it in GitHub Desktop.
Save zeffii/1453555 to your computer and use it in GitHub Desktop.
super class and inheritance
// super class
import java.util.ArrayList;
class GraphicsObject {
PVector pos;
int overthreshold = 2;
GraphicsObject(PVector _pos) {
pos = _pos;
}
boolean over(int x, int y) {
return abs(pos.x-x)<=overthreshold && abs(pos.y-y)<=overthreshold;
}
void setXY(PVector newpos) {
pos = newpos;
}
PVector getXY() {
return new PVector(pos.x,pos.y);
}
void display() {
// implemented by subclasses
}
}
// subclass for text graphics
class TextObject extends GraphicsObject {
String content;
TextObject(PVector pos, String _content){
super(pos);
content = _content;
}
// override
void display(){
textFont(displayFont);
text(content, pos.x, pos.y);
}
}
// subclass for SVG graphics
class SVGObject extends GraphicsObject {
PShape svgGraphic;
SVGObject(PVector pos, String fileName){
super(pos);
svgGraphic = loadShape(fileName);
}
void display(){
shapeMode(CORNER);
shape(svgGraphic, pos.x, pos.y);
}
}
// ---------------------------------
PFont displayFont;
ArrayList<GraphicsObject> gobjs;
void setup() {
size(640, 360);
displayFont = createFont("DroidSans.ttf", 20);
textFont(displayFont);
text("",0,0);
gobjs = new ArrayList<GraphicsObject>();
gobjs.add(new TextObject(new PVector(20,20), "this line"));
gobjs.add(new TextObject(new PVector(50,40), "the other line"));
gobjs.add(new SVGObject(new PVector(120,120), "da_logo.svg"));
noLoop();
}
void draw() {
background(204);
for (GraphicsObject go : gobjs){
go.display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment