Java Reference
In-Depth Information
Example 13−3: ScribbleCutAndPaste.java (continued)
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
// If this isn't mouse button 1, ignore it
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == 0)
return;
scribble.lineto(e.getX(), e.getY());
// Add a line
repaint();
}
});
}
/**
* Draw the component.
* This method relies on Scribble which implements Shape.
**/
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(linestyle); // Specify wide lines
g2.draw(scribble);
// Draw the scribble
}
/** This is the ActionListener method invoked by the popup menu items */
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("clear")) clear();
else if (command.equals("cut")) cut();
else if (command.equals("copy")) copy();
else if (command.equals("paste")) paste();
}
/** Clear the scribble. Invoked by popup menu */
void clear() {
scribble = new Scribble();
// Get a new, empty scribble
repaint();
// And redraw everything.
}
/**
* Make a copy of the current Scribble and put it on the clipboard
* We can do this because Scribble implements Transferable
* The user invokes this method through the popup menu
**/
public void copy() {
// Get system clipboard
Clipboard c = this.getToolkit().getSystemClipboard();
// Make a copy of the Scribble object to put on the clipboard
selection = (Scribble) scribble.clone();
// Put the copy on the clipboard
c.setContents(selection, // What to put on the clipboard
this);
// Who to notify when it is no longer there
}
/**
* The cut action is just like the copy action, except that we erase the
Search WWH ::




Custom Search