The graphics plotter allows users to plot shapes on a canvas. The current commands are:
circle x y r // plots a
circle of radius r with center @ (x, y)
square x y s // plots a square with
side = s and
// upper left corner @ (x, y)
triangle x y a // plots a triangle with altitude a and
// upper left corner @ (x,
y)
In the future we want to add more types of shapes.
For example, here's a typical control loop session:
Here's the corresponding graphics window:
Here's the standard viewer. You should know the imports needed:
public class FrameViewer {
public static final int FRAME_WIDTH =
300;
public static final int FRAME_HEIGHT =
400;
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH,
FRAME_HEIGHT);
frame.setTitle("Plot
Viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PlotComponent plotter = new
PlotComponent();
frame.add(plotter);
frame.setVisible(true);
plotter.controlLoop();
}
}
Here is a library of existing shapes. Unfortunately, the names of the drawing methods differ. You may add to these declarations, but you may not modify any of the existing code.
class Circle {
private int xc, yc, radius;
public Circle(int x, int y, int r) {
xc = x;
yc = y;
radius = r;
}
public void draw(Graphics2D gc) {
Ellipse2D.Double c = new
Ellipse2D.Double(
xc – radius/2, yc – radius/2,
radius, radius);
gc.draw(c);
}
}
class Square {
private int xc, yc, side;
public Square(int x, int y, int s) {
xc = x;
yc = y;
side = s;
}
public void paint(Graphics2D gc) {
Rectangle2D.Double c = new
Rectangle2D.Double(
xc, yc, side, side);
gc.draw(c);
}
}
class Triangle {
private int xc, yc, length;
public Triangle(int x, int y, int l) {
xc = x;
yc = y;
length = l;
}
public void display(Graphics2D gc) {
Line2D.Double side1 = new Line2D.Double(
xc + length /2, yc, xc, yc +
length);
Line2D.Double side2 = new
Line2D.Double(
xc + length /2, yc, xc + length,
yc + length);
Line2D.Double side3 = new
Line2D.Double(
xc, yc + length, xc + length, yc
+ length);
gc.draw(side1);
gc.draw(side2);
gc.draw(side3);
}
}
Here's the problem: the plotter component uses an array list that holds the forms to be plotted. But this array list is heterogeneous. It contains many types of forms: circles, squares, triangles, etc. So you must define an appropriate Form interface and retool the shapes so that they implement this interface.
class PlotComponent extends JComponent {
private ArrayList<???> forms = new ArrayList<???>();
public void paintComponent(Graphics
gc) {
Graphics2D gc2d = (Graphics2D)gc;
for(int i = 0; i < forms.size();
i++) {
forms.get(i).plot(gc2d);
}
}
public void controlLoop() {
Scanner kbd = new Scanner(System.in);
while(true) {
System.out.print("-> ");
String token = kbd.next();
if (token.equals("quit")) {
break;
} else if (token.equals("circle")) {
int xc = kbd.nextInt();
int yc = kbd.nextInt();
int radius = kbd.nextInt();
Circle x = new Circle(xc, yc, radius);
forms.add(x);
repaint();
System.out.println("done");
} else if (token.equals("square")) {
int xc = kbd.nextInt();
int yc = kbd.nextInt();
int side = kbd.nextInt();
Square x = new Square(xc, yc, side);
forms.add(x);
repaint();
System.out.println("done");
} else if
(token.equals("triangle")) {
int xc = kbd.nextInt();
int yc = kbd.nextInt();
int alt = kbd.nextInt();
Triangle x = new Triangle(xc, yc, alt);
forms.add(x);
repaint();
System.out.println("done");
} else {
System.err.println("Unrecognized command: " + token);
}
}
System.out.println("bye");
System.exit(0);
}
}