package convexHull; import java.awt.*; import java.awt.event.*; /** * This is a container for a canvas. This container will display rulers * around the canvas (on the top and left). */ public class MouseCanvas extends java.awt.Panel { /** canvas for the top ruler */ private TopCanvas topCanvas; /** canvas for the left ruler */ private LeftCanvas leftCanvas; /** the main canvas */ private Canvas mainCanvas; /** * Lays out its children (top & left rulers + the user * defined canvas). Associates callbacks with various mouse events, * so that the rulers can be properly updated. * * @param mainCanvas java.awt.Canvas * - the real user-graphics canvas */ public MouseCanvas(Canvas mainCanvas) { super(); setLayout(null); // get the dimensions of the panel // setup the top canvas topCanvas = new TopCanvas(); add(topCanvas); // setup the left canvas leftCanvas = new LeftCanvas(); add(leftCanvas); // setup the main canvas this.mainCanvas = mainCanvas; add(mainCanvas); // add a mouse motion listener to all canvases mainCanvas.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { mouseCB(e); } }); mainCanvas.addMouseListener(new MouseAdapter() { public void mouseExited(MouseEvent e) { mouseCB(e); } }); } /** * Makes sure that the rulers and the main canvas are always properly * laid out. */ public void doLayout() { // invoke the default layout procedure super.doLayout(); // get the size of the panel Dimension d = getSize(); // layout the canvases topCanvas.setBounds(10, 0, d.width - 10, 10); leftCanvas.setBounds(0, 10, 10, d.height - 10); mainCanvas.setBounds(10, 10, d.width - 10, d.height - 10); } /** * Callback for the mouse * * @param e java.awt.Event.MouseEvent * - mouse event that triggered this event */ public void mouseCB(MouseEvent e) { if (e.getID() == MouseEvent.MOUSE_MOVED) { topCanvas.setPointer(e.getX()); leftCanvas.setPointer(e.getY()); } else if (e.getID() == MouseEvent.MOUSE_EXITED) { topCanvas.setPointer(-1); leftCanvas.setPointer(-1); } } /** * This method allows the user to set the background. * * @param col java.awt.Color * - the user defined color */ public void setBackground(Color col) { super.setBackground(col); // topCanvas.setBackground(col); // leftCanvas.setBackground(col); } }