/** * This class is a custom component that supports scribbling. It also has * a popup menu that allows the scribble color to be set and provides access * to printing, cut-and-paste, and file loading and saving facilities. * Note that it extends Component rather than Canvas, making it "lightweight." */ class Scribble extends Component implements ActionListener { protected short last_x, last_y; // Coordinates of last click. protected Vector lines = new Vector(256,256); // Store the scribbles. protected Color current_color = Color.black; // Current drawing color. protected int width, height; // The preferred size. protected PopupMenu popup; // The popup menu. protected Frame frame; /** This constructor requires a Frame and a desired size */ public Scribble(Frame frame, int width, int height) { this.frame = frame; this.width = width; this.height = height; // We handle scribbling with low-level events, so we must specify which // events we are interested in. enableEvents: Enables the events defined // by the specified event mask parameter to be delivered to this component this.enableEvents(AWTEvent.MOUSE_EVENT_MASK); //variable in java.awt.AWTEvent:The event mask for selecting mouse events this.enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); //The event mask for selecting mouse motion events. // Create the popup menu using a loop. Note the separation of menu // "action command" string from menu label. Good for internationalization. String[] labels = new String[] { "Clear", "Print", "Save", "Load", "Cut", "Copy", "Paste" }; String[] commands = new String[] { "clear", "print", "save", "load", "cut", "copy", "paste" }; popup = new PopupMenu(); // Create the menu for(int i = 0; i < labels.length; i++) { MenuItem mi = new MenuItem(labels[i]); // Create a menu item. mi.setActionCommand(commands[i]); // Set its action command. mi.addActionListener(this); // And its action listener. popup.add(mi); // Add item to the popup menu. } Menu colors = new Menu("Color"); // Create a submenu. popup.add(colors); // And add it to the popup. String[] colornames = new String[] { "Black", "Red", "Green", "Blue"}; for(int i = 0; i < colornames.length; i++) { MenuItem mi = new MenuItem(colornames[i]); // Create the submenu items mi.setActionCommand(colornames[i]); // in the same way. mi.addActionListener(this); colors.add(mi); } // Finally, register the popup menu with the component it appears over this.add(popup); } /** Specifies big the component would like to be. It always returns the * preferred size passed to the Scribble() constructor */ public Dimension getPreferredSize() { return new Dimension(width, height); } /** This is the ActionListener method invoked by the popup menu items */ /* Event handling for the pop up menu - same as for pull-down menu items public void actionPerformed(ActionEvent event) { // Get the "action command" of the event, and dispatch based on that. // This method calls a lot of the interesting methods in this class. String command = event.getActionCommand(); if (command.equals("clear")) clear(); else if (command.equals("print")) print(); else if (command.equals("save")) save(); else if (command.equals("load")) load(); else if (command.equals("cut")) cut(); else if (command.equals("copy")) copy(); else if (command.equals("paste")) paste(); else if (command.equals("Black")) current_color = Color.black; else if (command.equals("Red")) current_color = Color.red; else if (command.equals("Green")) current_color = Color.green; else if (command.equals("Blue")) current_color = Color.blue; } /** Draw all the saved lines of the scribble, in the appropriate colors */ public void paint(Graphics g) { for(int i = 0; i < lines.size(); i++) { Line l = (Line)lines.elementAt(i); g.setColor(l.color); g.drawLine(l.x1, l.y1, l.x2, l.y2); } } /** * In addition to creating and registering a popup menu, we need to arrange * for it to pop up at the appropriate time * This is the low-level event-handling method called on mouse events * that do not involve mouse motion. Note the use of isPopupTrigger() * to check for the platform-dependent popup menu posting event, and of * the show() method to make the popup visible. If the menu is not posted, * then this method saves the coordinates of a mouse click or invokes * the superclass method. */ public void processMouseEvent(MouseEvent e) { if (e.isPopupTrigger()) // If popup trigger, popup.show(this, e.getX(), e.getY()); // pop up the menu. else if (e.getID() == MouseEvent.MOUSE_PRESSED) { last_x = (short)e.getX(); last_y = (short)e.getY(); // Save position. } else super.processMouseEvent(e); // Pass other event types on. } }