/* File: ButtonExample.java */ import java.awt.*; public class ButtonExample extends Frame{ boolean drawCircle; boolean drawRectangle; Canvas myCanvas; public ButtonExample() { // set window layout, title, and colors setLayout(new BorderLayout()); setTitle("ButtonExample"); setBackground(Color.white); setForeground(Color.black); // create a panel of buttons Panel p = new Panel(); // position panel in window and buttons // within panel add("North",p); p.setLayout(new FlowLayout()); // create four buttons p.add(new Button("Quit")); p.add(new Button("Circle")); p.add(new Button("Rectangle")); p.add(new Button("Restart")); // create a place to draw myCanvas = new ShapeCanvas(this); myCanvas.reshape(0,0,500,500); add("Center",myCanvas); // when we start, no drawings drawCircle = false; drawRectangle = false; // pack the layout and display the buttons pack(); show(); } public boolean action(Event e, Object arg) { // this code fragmet prints out the class of the target of the event; // try it! // System.out.println // ("The class of the event target " + e.target + " is of class " // + ((e.target).getClass()).getName()); // this code fragment checks the class of the target of the // event and prints out a message indication if it is a button // try it! // if (0 == (((e.target). // getClass()). // getName()).compareTo("java.awt.Button")) { // System.out.println("The event target is a button"); // } // else { // System.out.println("The event target is NOT a button"); // } // get the target of the event (assume it's a button) // before doing this we could have checked Button theButton = (Button) e.target; // get the button label, which we could print out (for example, // if we wanted a trace of user actions String label = theButton.getLabel(); // System.out.println // ("The button that you pressed has the label: " + label); // Set the flag to draw the requested component. if (0 == label.compareTo("Quit")) { System.out.println("Quit"); // I'm not sure about this; it seems a little brash // but I can't think of any other way to do it !!! System.exit(1); } else if (0 == label.compareTo("Circle")) { drawCircle = !drawCircle; System.out.println("Setting Circle flag to: " + drawCircle); myCanvas.repaint(); } else if (0 == label.compareTo("Rectangle")) { drawRectangle = !drawRectangle; System.out.println("Setting Rectangle flag to: " + drawRectangle); myCanvas.repaint(); } else if (0 == label.compareTo("Restart")) { drawCircle = false; drawRectangle = false; System.out.println("Resetting both the Circle and Rectangle flags"); myCanvas.repaint(); } else { // this print statement should never get run! // SO, why did I put it here? System.out.println("This is an unrecognized label:" + label); } return (true); // means that we took care of the event } public static void main (String arg[]) { new ButtonExample(); } } class ShapeCanvas extends Canvas { ButtonExample itsGeoWorld; public ShapeCanvas(ButtonExample _itsGeoWorld){ super(); itsGeoWorld = _itsGeoWorld; } public void paint(Graphics g) { Rectangle r = bounds(); if (itsGeoWorld.drawCircle) { g.drawOval(100,200,150,150); } if (itsGeoWorld.drawRectangle) { g.drawRect(50,150,300,250); } System.out.println("Graphics redrawn"); } }