Skip to content

Instantly share code, notes, and snippets.

@rjlutz rjlutz/Circle.pde
Last active Aug 31, 2017

Embed
What would you like to do?
ITEC 2150 Chapter 11 Inheritance
public class Circle extends Shape {
private float r; // r is the radius of the circle
public Circle(float a, float b, float c) {
super(); // this is effectively like new Shape()
x = a;
y = b;
r = c;
// accept Shape's default of filled=false
}
public float getArea() {
float area = (float)Math.PI * r * r;
return area;
}
public float getPerimeter() {
return (float)Math.PI * 2.0 * r;
}
public void display() {
super.display();
ellipse(x,y,2*r,2*r);
fill(0,0,0);
text("area=" + getArea(), x, y);
text("perimeter=" + getPerimeter(), x, y+20);
}
}
public class HexagonalRing extends Shape {
float sInner;
float sOuter;
HexagonalRing(float a, float b, float c, float d) {
// super(); // this happens, silently, even if omitted
x = a;
y = b;
sInner = c;
sOuter = d;
}
HexagonalRing() {
super();
x = 0;
y = 0;
sInner = 20;
sOuter = 30;
}
float getArea() {
double area = ((3*Math.sqrt(3.0)*sOuter*sOuter)/2) - ((3*Math.sqrt(3.0)*sInner*sInner)/2) ;
return (float)area;
}
float getPerimeter() {
float perimeter = 6 * (sOuter - sInner);
return perimeter;
}
public void display() {
super.display();
hexagon(x,y,sOuter);
fill(192);// fill inner with background color, regardless of state of filled
hexagon(x,y,sInner);
fill(0,0,0,255);
text("perimeter =" + this.getPerimeter(), x, y);
text("area =" + getArea(), x, y+20); // this. is implied here
}
// hexagon drawing code provided here, as promised
void hexagon(float x, float y, float s) {
//
// let's not worry about the geometry of a hexagon too much
// this should work if you provide the x,y of the center
// and the side length s
//
float x_v1 = x - (s /4) * 3;
float y_v1 = y;
float x_v2 = x - (s * 0.375);
float y_v2 = y + (s * 0.65);
float x_v3 = x + (s * 0.375);
float y_v3 = y + (s * 0.65);
float x_v4 = x + (s /4) * 3;
float y_v4 = y;
float x_v5 = x + (s * 0.375);
float y_v5 = y - s * 0.65;
float x_v6 = x - (s * 0.375);
float y_v6 = y - s * 0.65;
beginShape();
vertex(x_v1, y_v1);
vertex(x_v2, y_v2);
vertex(x_v3, y_v3);
vertex(x_v4, y_v4);
vertex(x_v5, y_v5);
vertex(x_v6, y_v6);
endShape(CLOSE);
}
}
public class Rectangle extends Shape {
private float w,h;
public Rectangle(float a, float b, float c, float d) {
//super(); // even if skip, we still get this call invis
x = a;
y = b;
w = c;
h = d;
// accept Shape's default of filled=false
}
public float getArea() {
float area = w * h;
return area;
}
public float getPerimeter() {
return 2*w + 2*h;
}
public void display() {
super.display();
rect(x,y,w,h);
fill(0,0,0);
text("area=" + getArea(), x, y);
text("perimeter=" + getPerimeter(), x, y+20);
}
}
public class Ring extends Shape {
float rInner;
float rOuter;
Ring(float a, float b, float c, float d) {
// super(); // this happens, silently, even if omitted
x = a;
y = b;
rInner = c;
rOuter = d;
}
Ring() {
super();
x = 0;
y = 0;
rInner = 10;
rOuter = 20;
}
float getArea() {
float area = (rOuter * rOuter - rInner * rInner) * (float)Math.PI;
return area;
}
float getPerimeter() {
float perimeter = 2 * (rOuter + rInner) * (float)Math.PI;
return perimeter;
}
public void display() {
super.display();
ellipse(x,y,rOuter*2,rOuter*2);
fill(192);// fill inner with background color, regardless of state of filled
ellipse(x,y,rInner*2,rInner*2);
fill(0,0,0,255);
text("perimeter =" + this.getPerimeter(), x, y);
text("area =" + getArea(), x, y+20); // this. is implied here
}
}
public class Shape {
protected float x,y;
private boolean filled = false;
protected int red,green,blue;
private boolean outline = true;
protected int outline_red, outline_green, outline_blue;
Shape() {
red = 255; // default color white, RGB = 255,255,255 in Processing
green = 255;
blue = 255;
}
public boolean isFilled() {
return filled;
}
public boolean isOutline() {
return outline;
}
public void setFilled(boolean a) {
filled = a;
}
public void setOutline(boolean a) {
outline = a;
}
public void setColor(int a, int b, int c) {
red = a;
green = b;
blue = c;
}
public void display() {
if (isFilled())
fill(red,green,blue);
else
fill(0,0,0,0); // transparent
if (isOutline())
stroke(0,0,0);
else
stroke(0,0,0,0); // transparent
}
}
Circle c1, c2, c3;
Rectangle r1, r2, r3;
Ring ring1, ring2, ring3;
HexagonalRing hring1,hring2,hring3;
ArrayList<Shape> shapes;
void setup() {
size(800,600);
shapes = new ArrayList<Shape>();
c1 = new Circle(width/2, height*0.60, 30);
c1.setColor(255,0,0);
shapes.add(c1);
c2 = new Circle(width*0.75, height*0.60, 45);
c2.setColor(0,255,0);
c2.setFilled(true);
shapes.add(c2);
c3 = new Circle(width*0.25, height*0.60, 45);
c3.setColor(0,0,255);
c3.setFilled(false);
shapes.add(c3);
r1 = new Rectangle(width/2, height*0.4, 30 , 15);
r1.setColor(0,255,0);
shapes.add(r1);
r2 = new Rectangle(width*0.75, height*0.4, 45, 40);
r2.setColor(0,255,0);
r2.setFilled(true);
shapes.add(r2);
r3 = new Rectangle(width*0.25, height*0.4, 45, 40);
r3.setColor(0,0,255);
r3.setFilled(true);
shapes.add(r3);
ring1 = new Ring(width/2, height*0.8, 10, 20);
ring1.setColor(255, 0, 0);
ring1.setFilled(false);
shapes.add(ring1);
ring2 = new Ring(width*0.75, height*0.8, 10, 50);
ring2.setColor(0, 255, 0);
ring2.setFilled(true);
shapes.add(ring2);
ring3 = new Ring(width*0.25, height*0.8, 30, 40);
ring3.setColor(0, 0, 255);
ring3.setFilled(true);
shapes.add(ring3);
hring1 = new HexagonalRing(width/2.0, height*0.20, 10.0, 20.0); // the middle hex
hring1.setColor(255, 0, 0);
hring1.setFilled(false);
shapes.add(hring1);
hring2 = new HexagonalRing(width*0.75, height*0.20, 10, 50); // the rightmost hex
hring2.setColor(0, 255, 0);
hring2.setFilled(true);
hring2.setOutline(false);
shapes.add(hring2);
hring3 = new HexagonalRing(width*0.25, height*0.20, 30, 40); // the leftmost hex
hring3.setColor(0, 0, 255);
hring3.setFilled(true);
shapes.add(hring3);
}
void draw() {
background(192);
for(Shape s : shapes) {
s.display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.