Hi All,
Here is the homework assigned for next week. Please place
the flopping in an envelope with a print out that includes
the listing and a screen shot.
We are printing 3 stick figure houses with two windows and a
door in each.
Please use polymorphism.
Here is some starter code:
import java.util.Vector;
import java.awt.Frame;
import java.awt.Graphics;
public interface GraphicsInterface {
public void draw(Graphics g);
}
public class Line
implements GraphicsInterface {
int x1 = 0;
int y1 = 0;
int x2 = 100;
int y2 = 100;
Line(int _x1,
int _y1,
int _x2,
int _y2) {
x1 = _x1;
x2 = _x2;
y1 = _y1;
y2 = _y2;
}
public void draw(Graphics g) {
g.drawLine(x1,y1,x2,y2);
}
}
public class Circle
implements GraphicsInterface {
int x1 = 0;
int y1 = 0;
int r = 20;
Circle(int _x1,
int _y1,
int _r) {
x1 = _x1;
y1 = _y1;
r = _r;
}
public void draw(Graphics g) {
g.drawOval(x1,y1,r,r);
}
}
public class GraphicsDataBase
implements GraphicsInterface {
private Vector v =
new Vector();
public void draw(Graphics g) {
for (int i=0; i < v.size();
i++) {
(
(GraphicsInterface)
v.elementAt(i)).draw(g);
}
}
public void add(
GraphicsInterface g) {
v.addElement(g);
}
public int getSize() {
return v.size();
}
public GraphicsInterface
elementAt(int i) {
return
(GraphicsInterface)
v.elementAt(i);
}
}
public class MyFrame extends Frame
implements GraphicsInterface {
GraphicsDataBase
gdb = new GraphicsDataBase();
MyFrame() {
setVisible(true);
setSize(200,200);
gdb.add(
new Line(0,0,100,100));
gdb.add(
new Line(100,100,100,0));
gdb.add(
new Circle(100,100,50));
}
public void draw(Graphics g) {
repaint();
}
public void paint(Graphics g) {
gdb.draw(g);
}
}
public class TrivialApplication {
public static void main(String args[]) {
MyFrame mf
= new MyFrame();
}
}
|