import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * WindowManager provides the graphical output that mimics * a system window manager. * * The WindowManager object uses a ListManager to manage * a list of windows (rectangles) * **/ public class WindowManager extends JFrame implements MouseListener{ private ListManager mgr; private int x; private int y; /** * Answers a WindowManager object * * @param ListManager m the list manager that keeps * the actual list of windows/rectangles to * be drawn **/ public WindowManager(ListManager m) { mgr = m; setSize(800,800); setLocation(100,100); addMouseListener(this); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public void paint(Graphics g) { mgr.setIndex(); //start at the oldes rectangle in the list while (mgr.hasMore() ) { // get the next Rect and draw it... Rect next = mgr.getNextRect(); int x = next.getX(); int y = next.getY(); g.setColor(Color.black); g.fillRect(x-2,y-2,134,84); g.setColor(Color.cyan); g.fillRect(x, y,130,80); } } public void mouseReleased(MouseEvent e ) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseClicked(MouseEvent e) { // get (x,y) of where the mouse button was clicked x = e.getX(); y = e.getY(); // figure out which button was clicked if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { // moveToRear() finds the rectangle containing (x,y) and // move this rectangle to the end of the list // ...when we repaint, it will be painted last // so it will appear 'on top' mgr.moveToRear(x,y); } if ((e.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) { // add a new rectangle with (x,y) as the // upperleft corner mgr.add(x,y); } repaint();} public static void main(String argv[]) { ListManager m = new ListManager(); WindowManager c = new WindowManager(m); } }