// // Jeff Mark // May 4, 1996 // CS 162 // Program 2 - Shapes Applet // import java.applet.*; import java.awt.*; // Sets up the Drawing Area, handles the mouse events. public class DrawShapes extends Applet { // The drawing Area private CoordinateArea area; public void init() { // Set background color this.setBackground(Color.white); // Add the drawing area area = new CoordinateArea(this); add(area); } // Called when user clicks the buttons or chooses a color/shape public boolean action(Event event, Object arg) { // If clear was clicked if (event.target == area.clear) { return area.clear(); } // If shift right was clicked else if (event.target == area.rightShift) { return area.rightShift(); } // If shift left was clicked else if (event.target == area.leftShift) { return area.leftShift(); } // If randomize the colors was clicked. else if (event.target == area.randomColor) { return area.randomColor(); } // Otherwise let superclass deal with it. return super.action(event, arg); } } class CoordinateArea extends Canvas { private Point p1; // Initial coordiantes of object. private Point p2; // Final coordinates of the object. private DrawShapes controller; private Dimension mySize = new Dimension(700, 300); // Shift amounts private static Point leftShiftAmount = new Point(-10, 0); private static Point rightShiftAmount = new Point(10, 0); // Buttons and Menus to control what we are drawing public Button leftShift; public Button clear; public Button rightShift; public Button randomColor; public Choice color_choices; public Choice shape_choices; public CoordinateArea(DrawShapes controller) { super(); this.controller = controller; // Create left shift button leftShift = new Button("Shift Left"); controller.add(leftShift); // Create clear button clear = new Button("Clear"); controller.add(clear); // Create right shift button rightShift = new Button("Shift Right"); controller.add(rightShift); // Create the random color button. randomColor = new Button("Randomize Colors"); controller.add(randomColor); // Create a menu of colors and add it to the applet color_choices = new Choice(); color_choices.addItem("RANDOM"); color_choices.addItem("black"); color_choices.addItem("blue"); color_choices.addItem("red"); color_choices.addItem("yellow"); color_choices.addItem("green"); controller.add(new Label("Color:")); controller.add(color_choices); // Create a menu of shapes and add it to the applet shape_choices = new Choice(); shape_choices.addItem("RANDOM"); shape_choices.addItem("Point"); shape_choices.addItem("Line"); shape_choices.addItem("Square"); shape_choices.addItem("Circle"); controller.add(new Label("Shape:")); controller.add(shape_choices); } public Dimension minimumSize() { return mySize; } public Dimension preferredSize() { return mySize; } // List of shapes created and index to the list. private static int MaxShapes = 100; private Shapes mosaic[] = new Shapes[MaxShapes]; private int index = -1; private int Randomize(int range) { double result; result = Math.random() * range; return ((int) result); } private void selectColor() { String select = color_choices.getSelectedItem(); Color pickColor = Color.black; if (select == "RANDOM") pickColor = new Color(Randomize(255), Randomize(255), Randomize(255)); else if (select == "black") pickColor = Color.black; else if (select == "blue") pickColor = Color.blue; else if (select == "red") pickColor = Color.red; else if (select == "yellow") pickColor = Color.yellow; else if (select == "green") pickColor = Color.green; mosaic[index].setColor(pickColor); //controller.showStatus("Color is --> " + select); } // Picks a new shape -> the one selected by shape_choice private void selectShape() { int select = shape_choices.getSelectedIndex(); int numitems = shape_choices.countItems() - 1; if (select == 0) select = Randomize(numitems) + 1; // Increase the count on the objects drawn index++; switch(select) { case 1 : mosaic[index] = new Dot(); break; case 2 : mosaic[index] = new Line(); break; case 3 : mosaic[index] = new Square(); break; case 4 : mosaic[index] = new Circle(); break; } //controller.showStatus("Shape selected = " + select); } // Called when user clicks the mouse to place an object public boolean mouseDown(Event e, int x, int y) { // Test if we have reached the max if (index == MaxShapes - 1) { return false; } controller.showStatus("Mouse Down"); // Pick a new shape and color selectShape(); selectColor(); // Save the coordinates - they are both initial and final points (for now) p1 = new Point(x, y); p2 = new Point(x, y); mosaic[index].setPoints(p1, p2); repaint(); return true; } // Called when the user scribbles with the mouse button down public boolean mouseDrag(Event e, int x, int y) { // Test if we have reached the max if (index == MaxShapes - 1) { return false; } // Save intermediate coordinates p2 = new Point(x, y); controller.showStatus("Mouse Drag (" + p1.x + "," + p1.y + ") to (" + p2.x + "," + p2.y + ")"); // Set the new points of this object mosaic[index].setPoints(p1, p2); repaint(); return true; } // Called when the user "Unclicks" the mouse public boolean mouseUp(Event e, int x, int y) { controller.showStatus(""); // Test if we have reached the max if (index == MaxShapes - 1) { return false; } // Save final coordinates. p2 = new Point(x, y); mosaic[index].setPoints(p1, p2); repaint(); return true; } // Called by DrawShapes when the Random Color button is pressed. // Changes the color of all the objects to something random. public boolean randomColor() { int i, r, g, b; for (i = 0; i <= index; i++) { r = Randomize(255); g = Randomize(255); b = Randomize(255); mosaic[i].setColor(new Color(r, g, b)); } repaint(); return true; } // Called by DrawShapes class when user click on clear button; public boolean clear() { // Set number of objects to -1 and repaint the screen index = -1; repaint(); return true; } // Called by DrawShapes when leftShift is clicked public boolean leftShift() { int i; for (i = 0; i <= index; i++) mosaic[i].moveBy(leftShiftAmount); repaint(); return true; } // Called by DrawShapes when rightShift is clicked public boolean rightShift() { int i; for (i = 0; i <= index; i++) mosaic[i].moveBy(rightShiftAmount); repaint(); return true; } // Paints all the objects. public void paint(Graphics g) { int i; // Paint the objects in the Shapes array if (index >= 0) { for (i = 0; i <= index; i++) { mosaic[i].draw(g); g.setColor(Color.black); g.drawString("Number of objects: " + (index+1) + " of " + MaxShapes, 5, 260); if (index+1 == MaxShapes) g.drawString("You are at your max, press --Clear-- to make more shapes...", 200, 260); } } else { g.setColor(Color.white); g.fillRect(0, 0, 700, 300); } } }