import java.awt.*; import java.awt.event.*; /* This is sample program to demonstrate the use of mouse listeners, certain GUI controls, and double-buffered graphics. By Dan Schroeder, December 2011 */ class SimplePaint extends Canvas implements MouseListener, MouseMotionListener { int canvasSize = 400; // width and height of canvas in pixels Image offScreenImage; // the off-screen image, for storing what's painted Graphics offScreenGraphics; // graphics object of the off-screen image boolean mouseInCanvas = false; // true when mouse is within canvas int mX, mY; // current mouse coordinates Choice colorChoice = new Choice(); // popup menu for choosing colors Color purple = new Color(150,0,255); // since Java doesn't predefine "Color.purple" Checkbox largeBrushCheck = new Checkbox("Large brush",false); // checked to enlarge brush // Constructor method initializes everything: SimplePaint() { // Create a Frame to hold everything: Frame theFrame = new Frame("Simple Paint!"); theFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); // exit when user clicks close button } }); theFrame.setResizable(false); Panel canvasPanel = new Panel(); theFrame.add(canvasPanel); canvasPanel.add(this); // Set the Canvas properties, including MouseListener and MouseMotionListener: setSize(canvasSize,canvasSize); setBackground(Color.white); addMouseListener(this); addMouseMotionListener(this); // Add some controls: Panel controlPanel = new Panel(); // Panel to hold controls theFrame.add(controlPanel,BorderLayout.SOUTH); controlPanel.add(colorChoice); // Popup menu for colors colorChoice.add("Purple"); // Order must be same as in currentColor() colorChoice.add("Red"); colorChoice.add("Green"); colorChoice.add("Blue"); colorChoice.add("Cyan"); colorChoice.add("Yellow"); colorChoice.add("Magenta"); colorChoice.add("Black"); colorChoice.add("White"); controlPanel.add(largeBrushCheck); Button clearButton = new Button("Clear"); controlPanel.add(clearButton); clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { clear(); } }); // That's all, so pack the frame, set up the off-screen image, and go live: theFrame.pack(); offScreenImage = createImage(canvasSize,canvasSize); offScreenGraphics = offScreenImage.getGraphics(); // must come after pack theFrame.setVisible(true); } // end of constructor method // MouseListener methods keep track of mouse location and handle initial press: public void mouseEntered(MouseEvent e) { mouseInCanvas = true; mX = e.getX(); mY = e.getY(); repaint(); } public void mouseExited(MouseEvent e) { mouseInCanvas = false; repaint(); } public void mousePressed(MouseEvent e) { mX = e.getX(); mY = e.getY(); draw(mX,mY); repaint(); } public void mouseReleased(MouseEvent e) {} // not needed but required for interface public void mouseClicked(MouseEvent e) {} // MouseMotionListener methods keep track of mouse location and handle dragging: public void mouseMoved(MouseEvent e) { mX = e.getX(); mY = e.getY(); repaint(); } public void mouseDragged(MouseEvent e) { mX = e.getX(); mY = e.getY(); draw(mX,mY); repaint(); } // Returns the current drawing Color: Color currentColor() { int c = colorChoice.getSelectedIndex(); if (c == 0) return purple; // order of colors must match that in constructor else if (c == 1) return Color.red; else if (c == 2) return Color.green; else if (c == 3) return Color.blue; else if (c == 4) return Color.cyan; else if (c == 5) return Color.yellow; else if (c == 6) return Color.magenta; else if (c == 7) return Color.black; else return Color.white; } // Returns the current brush size (only two possibilities): int currentBrushSize() { if (largeBrushCheck.getState()) return 30; else return 10; } // Draw on the off-screen image with the current pen settings at the given location: void draw(int x, int y) { int width = currentBrushSize(); offScreenGraphics.setColor(currentColor()); offScreenGraphics.fillOval(mX-width/2,mY-width/2,width,width); repaint(); } // Clear the off-screen image: void clear() { offScreenGraphics.setColor(Color.white); offScreenGraphics.fillRect(0,0,canvasSize,canvasSize); repaint(); } // Paint method copies off-screen image to Canvas and adds temporary "brush" at cursor: public void paint(Graphics g) { g.drawImage(offScreenImage,0,0,null); if (mouseInCanvas) { int width = currentBrushSize(); g.setColor(currentColor()); g.fillOval(mX-width/2,mY-width/2,width,width); // draw the "brush" } } // Main method just gets us started: public static void main(String[] arg) { new SimplePaint(); } }