Skip to content

Instantly share code, notes, and snippets.

@spencerdcarlson
Created September 26, 2012 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spencerdcarlson/3785744 to your computer and use it in GitHub Desktop.
Save spencerdcarlson/3785744 to your computer and use it in GitHub Desktop.
Polymorphism
import java.awt.Graphics;
public class Circle extends Shape {
int x;
int y;
int width;
int height;
/**
* Create a circle object
* @param x Top left x position
* @param y Top left y position
* @param width Width
* @param height Height
*/
public Circle(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
@Override
public void draw(Graphics g, boolean fill){
if (fill){
g.fillOval(x, y, width, height);
}else {
g.drawOval(x, y, width, height);
}
}
}
import java.awt.Graphics;
import java.awt.Polygon;
public class IsoscelesTriangle extends Shape {
private Polygon triangle;
/**
* Create an Isosceles Triangle object
* @param x Top x position
* @param y Top y position
* @param base Length of the base of the triangle
* @param height Height of the triangle
*/
public IsoscelesTriangle(int x, int y, int base, int height){
triangle = new Polygon();
triangle.addPoint(x, y);
triangle.addPoint( ( x - ( base / 2 ) ), ( y + height ) );
triangle.addPoint( ( x + ( base / 2 ) ), ( y + height ) );
}
public void draw(Graphics g, boolean fill){
if (fill) {
g.fillPolygon(triangle);
}else {
g.drawPolygon(triangle);
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
@SuppressWarnings("serial")
public class Main extends JApplet {
private List<Shape> shapes = new ArrayList<Shape>();
public void init(){
// Create Shapes
Shape square = new Square(5, 5, 50,50);
Shape circle = new Circle(100,5,50,50);
Shape triangle = new IsoscelesTriangle(70,50,100,50);
// Create Triforce
Shape triforce = new Triforce(200, 200, 100, 50);
triforce.setShapeColor(Color.YELLOW);
// Add shapes to arrayList
shapes.add(triangle);
shapes.add(square);
shapes.add(circle);
shapes.add(triforce);
}
@Override
public void paint(Graphics g){
// Set Color and draw background
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
// Draw every shape in the arrayList
// Polymorphism: The same message call ( .draw ) executes different code depending on the child type
for(Shape s: shapes){
g.setColor(s.shapeColor);
s.draw(g,false);
}
}
}
import java.awt.Color;
import java.awt.Graphics;
public class Shape {
// Default shape color
protected Color shapeColor = Color.WHITE;
// Change the default shape color
public void setShapeColor(Color c){
this.shapeColor = c;
}
/**
* Method to be overridden by each child class
* @param g Graphics object to be passed in from JApplet paint
* @param fill Fill in the object?
*/
public void draw(Graphics g, boolean fill){
}
}
import java.awt.Graphics;
public class Square extends Shape {
int x;
int y;
int width;
int height;
/**
* Create a square object
* @param x Top left x position
* @param y Top left y position
* @param width Width
* @param height Height
*/
public Square(int x, int y, int width, int height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
@Override
public void draw(Graphics g, boolean fill){
if (fill){
g.fillRect(x, y, width, height);
}else {
g.drawRect(x, y, width, height);
}
}
}
import java.awt.Graphics;
public class Triforce extends Shape {
Shape triangle1;
Shape triangle2;
Shape triangle3;
/**
* Create a triforce object
* @param x Top x position of the top triangle
* @param y Top y position of the top triangle
* @param base Length of the base of each individual triangle
* @param height Height of each individual triangle
*/
public Triforce(int x, int y, int base, int height){
triangle1 = new IsoscelesTriangle(x,y,base,height);
triangle2 = new IsoscelesTriangle(x-(base/2),y+height,base,height);
triangle3 = new IsoscelesTriangle(x+(base/2),y+height,base,height);
}
// When drawing a triforce object always fill in the triangles
@Override
public void draw (Graphics g, boolean fill){
triangle1.draw(g,true);
triangle2.draw(g,true);
triangle3.draw(g,true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment