// // Jeff Mark // May 6, 1996 // CS 162 // Program 2 - Shapes Applet // import java.awt.*; /////////////////////////////////////////////////////////////////////////////// // // // Definition of the Shapes // // // /////////////////////////////////////////////////////////////////////////////// // // The general structure of a "Shape" // public class Shapes { // Each shape is defined with at least 2 points and a color. public Point point1; public Point point2; public Color c; // Constructor public Shapes() { point1 = null; point2 = null; c = Color.black; } public void setColor(Color c) { this.c = c; } public void setPoints(Point p1, Point p2) { point1 = p1; point2 = p2; } public void moveBy(Point delta) { point1.x += delta.x; point1.y += delta.y; point2.x += delta.x; point2.y += delta.y; } public void draw(Graphics g) { g.setColor(c); } } /*****************************************************************************/ /*****************************************************************************/ class Dot extends Shapes // Only uses one point { // Constructor public Dot() { super(); } public void draw(Graphics g) { super.draw(g); g.fillRect(point2.x - 1, point2.y - 1, 2, 2); } } /*****************************************************************************/ class Line extends Shapes { // Constructor public Line() { super(); } public void draw(Graphics g) { super.draw(g); g.drawLine(point1.x, point1.y, point2.x, point2.y); } } /*****************************************************************************/ class Square extends Shapes { // If the points are in the correct format or not private Rectangle r = new Rectangle(0, 0, 0, 0); // Constructor public Square() { super(); } // Get the upper left and lower right points private void fixPoints() { int width, height; width = Math.abs(point1.x - point2.x); height = Math.abs(point1.y - point2.y); // Find upper left point r.x = (point1.x < point2.x) ? point1.x : point2.x; r.y = (point1.y < point2.y) ? point1.y : point2.y; // Put the height, width into p2 r.width = width; r.height = height; } public void draw(Graphics g) { super.draw(g); // Put points in correct format (Upper left point, and height, width) fixPoints(); g.fillRect(r.x, r.y, r.width, r.height); } } /*****************************************************************************/ class Circle extends Shapes { // If the points are in the correct format or not private Rectangle r = new Rectangle(0, 0, 0, 0); // Constructor public Circle() { super(); } // Get the upper left and lower right points private void fixPoints() { int width, height; width = Math.abs(point1.x - point2.x); height = Math.abs(point1.y - point2.y); // Find upper left point r.x = (point1.x < point2.x) ? point1.x : point2.x; r.y = (point1.y < point2.y) ? point1.y : point2.y; // Put the height, width into p2 r.width = width; r.height = height; } public void draw(Graphics g) { super.draw(g); // Put points in correct format (Upper left point, and height, width) fixPoints(); g.fillOval(r.x, r.y, r.width, r.height); } }