Java Reference
In-Depth Information
Example 10•12: ScribblePane2.java
package com.davidflanagan.examples.gui;
import javax.swing.*;
// For JPanel component
import java.awt.*;
// For Graphics object
import java.awt.event.*;
// For Event and Listener objects
/**
* A simple JPanel subclass that uses event listeners to allow the user
* to scribble with the mouse. Note that scribbles are not saved or redrawn.
**/
public class ScribblePane2 extends JPanel {
public ScribblePane2() {
// Give the component a preferred size
setPreferredSize(new Dimension(450,200));
// Register a mouse event handler defined as an inner class
// Note the call to requestFocus(). This is required in order for
// the component to receive key events.
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
moveto(e.getX(), e.getY()); // Move to click position
requestFocus();
// Take keyboard focus
}
});
// Register a mouse motion event handler defined as an inner class
// By subclassing MouseMotionAdapter rather than implementing
// MouseMotionListener, we only override the method we're interested
// in and inherit default (empty) implementations of the other methods.
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
lineto(e.getX(), e.getY()); // Draw to mouse position
}
});
// Add a keyboard event handler to clear the screen on key 'C'
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_C) clear();
}
});
}
/** These are the coordinates of the the previous mouse position */
protected int last_x, last_y;
/** Remember the specified point */
public void moveto(int x, int y) {
last_x = x;
last_y = y;
}
/** Draw from the last point to this point, then remember new point */
public void lineto(int x, int y) {
Graphics g = getGraphics();
// Get the object to draw with
g.setColor(color);
// Tell it what color to use
g.drawLine(last_x, last_y, x, y);
// Tell it what to draw
moveto(x, y);
// Save the current point
}
Search WWH ::




Custom Search