/* Author: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 26/10/09 Description: Provides users with a choice of shapes and a fill option. Updates the screen in response to these selections. */ import java.awt.*; import java.applet.Applet; public class GraphChoice extends Applet { Choice choice; //pop-up list of choices Canvas rectangle; // graphic area to illustrate drawing Checkbox filled; // check box to fill rectangle or not static boolean DEBUG = true; public void init() { if (DEBUG) System.out.println("Here4"); filled = new Checkbox("Filled?"); choice = new Choice(); choice.addItem("Select a shape to be drawn..."); choice.addItem("drawRect(int, int, int, int)"); choice.addItem("draw3DRect(int, int, int, int, boolean)"); choice.addItem("drawRoundRect(int, int, int, int, int, int)"); choice.addItem("drawOval(int, int, int, int)"); rectangle = new Canvas(); setLayout(new BorderLayout(2,2)); //Add components to the Applet. add("North", choice); add("Center", rectangle); add("South", filled); validate(); } public boolean action(Event e, Object arg) { Graphics g = rectangle.getGraphics(); // get graphics handle of canvas Dimension the_size = size(); Point the_position = location(); int width = the_size.width/2; int height = the_size.height/2; int new_x = the_position.x+(the_size.width/4); int new_y = the_position.x+(the_size.width/4); g.setColor(Color.blue); g.clearRect(the_position.x, the_position.y, the_size.width, the_size.height); if ((filled.getState()== false) && ((e.target instanceof Choice) || (e.target instanceof Checkbox))) { if (choice.getSelectedItem()=="draw3DRect(int, int, int, int, boolean)") { if (DEBUG) System.out.println("3D..."); g.draw3DRect(new_x, new_y, width-1, height-1, true); // true here if raised g.draw3DRect(new_x+3, new_y+3, width-7, height-7, false); } else if (choice.getSelectedItem()=="drawRoundRect(int, int, int, int, int, int)") { if (DEBUG) System.out.println("Round..."); g.drawRoundRect(new_x, new_y,width, height,30, 30); // last two parameters are the diameter of the arcs at the corners } else if (choice.getSelectedItem()=="drawRect(int, int, int, int)") { if (DEBUG) System.out.println("Rect..."); g.drawRect(new_x, new_y, width, height); } else if (choice.getSelectedItem()=="drawOval(int, int, int, int)") { if (DEBUG) System.out.println("Oval..."); g.drawOval(new_x+(new_x/2), new_y, width/2, height); } return true; } else if ((e.target instanceof Choice)|| (e.target instanceof Checkbox)) { if (choice.getSelectedItem()=="draw3DRect(int, int, int, int, boolean)") { if (DEBUG) System.out.println("Filled 3D..."); g.draw3DRect(new_x, new_y, width-1, height-1, true); // true here if raised g.fill3DRect(new_x+3, new_y+3, width-7, height-7, false); } else if (choice.getSelectedItem()=="drawRoundRect(int, int, int, int, int, int)") { if (DEBUG) System.out.println("Filled Round...");; g.fillRoundRect(new_x, new_y, width, height, 30, 30); } else if (choice.getSelectedItem()=="drawRect(int, int, int, int)") { if (DEBUG) System.out.println("Filled Rect - sorted...");; g.fillRect(new_x, new_y, width, height); } else if (choice.getSelectedItem()=="drawOval(int, int, int, int)") { if (DEBUG) System.out.println("Filled Oval - sorted...");; g.fillOval(new_x+(new_x/2), new_y, width/2, height); } return true; } return false; } }