import java.awt.*; import java.awt.event.*; import java.util.*; class CS61BWindow extends Frame implements MouseListener, WindowListener { public static boolean DEBUGGING = false; private final int DEFAULT_WIDTH = 800; private final int DEFAULT_HEIGHT = 700; /** * EFFECTS: Initialize and display a CS61BWindow object. */ public CS61BWindow ( ) { myWidth = DEFAULT_WIDTH; myHeight = DEFAULT_HEIGHT; this.addMouseListener (this); this.addWindowListener (this); setSize (myWidth, myHeight); myRegions = new RegionStack ( ); setVisible (true); } /** * REQUIRES: x >= 0; y >= 0. * EFFECTS: Initialize and display a CS61BWindow object * with the given width and height. */ public CS61BWindow (int width, int height) { myWidth = width; myHeight = height; this.addMouseListener (this); this.addWindowListener (this); setSize (myWidth, myHeight); myRegions = new RegionStack ( ); setVisible (true); } /** * REQUIRES: r != null. * MODIFIES: this. * EFFECTS: Add a region on top of the window's stack of regions * and redraw the stack. */ public void addRegion (Region r) { myRegions.add (r); repaint ( ); } /** * REQUIRES: g != null. * EFFECTS: Display the regions from bottom to top. */ public void paint (Graphics g) { Iterator iter = myRegions.regions ( ); while (iter.hasNext ( )) { Region r = (Region) iter.next ( ); r.paint (g); } } /** * REQUIRES: 0 <= x < myWidth; 0 <= y < myHeight. * EFFECTS: Simulates a mouse click at the point (x,y) * in the window. */ public void simulateClick (int x, int y) { myRegions.handleClick (x, y); repaint ( ); } /** * REQUIRES: event != null. * MODIFIES: this; System.out if DEBUGGING. * EFFECTS: Handle a mouse click. */ public void mousePressed (MouseEvent event) { int mouseX = event.getX ( ); int mouseY = event.getY ( ); if (DEBUGGING) { System.out.println (""); System.out.println ("Mouse click at (" + mouseX + "," + mouseY + ")"); } myRegions.handleClick (mouseX, mouseY); repaint ( ); } /** * All these are required to implement the MouseListener interface. */ public void mouseClicked (MouseEvent event) { } public void mouseReleased (MouseEvent event) { } public void mouseEntered (MouseEvent event) { } public void mouseExited (MouseEvent event) { } /** * REQUIRES: event != null. * EFFECTS: The user has clicked the "close" box of the window, * so quit the program. */ public void windowClosing (WindowEvent event) { System.exit (0); } /** * All these are required to implement the WindowListener interface. */ public void windowIconified (WindowEvent event) { } public void windowOpened (WindowEvent event) { } public void windowClosed (WindowEvent event) { } public void windowDeiconified (WindowEvent event) { } public void windowActivated (WindowEvent event) { } public void windowDeactivated (WindowEvent event) { } private int myWidth; // window width in pixels private int myHeight; // window height in pixels private RegionStack myRegions; // regions in the window }