import java.awt.*; import java.awt.event.*; import java.awt.image.*; public class SigningSurface extends Canvas { protected int width, height; // dimensions of surface protected int x,y, lastX, lastY; // stroke coordinates protected Image offscreenImage = null; protected Graphics offscreenG; //------------------------------------------------------------------- // Constructor //------------------------------------------------------------------- public SigningSurface() { // since we are bypassing any registration of listeners, we have // to manually enable the set of events we wish to deal with ! enableEvents( AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK); } //------------------------------------------------------------------- // SigningSurface (on-screen) redaw routines // Override update() to eliminate the component erase, but still // call paint() so that component can repaint itself when windowing // system orders it. //------------------------------------------------------------------- public void update(Graphics g) { paint(g); } //------------------------------------------------------------------- public void paint(Graphics g) { if ( offscreenImage == null ) { width = getSize().width; height = getSize().height; // System.out.println("Width = " + width); // System.out.println("Height = " + height); offscreenImage = createImage(width, height); if ( offscreenImage == null ) { System.out.println("ERROR: Couldn't create offscreen.."); } offscreenG = offscreenImage.getGraphics(); } g.drawImage(offscreenImage, 0,0, this); } //------------------------------------------------------------------- // Override event dispatcher methods to catch and process events locally //------------------------------------------------------------------- protected void processMouseEvent(MouseEvent mouseEvent) { x = mouseEvent.getX(); y = mouseEvent.getY(); switch ( mouseEvent.getID() ) { case MouseEvent.MOUSE_PRESSED : // System.out.println("click at " + x +" "+ y); lastX = x; // note start point for line lastY = y; repaint(); break; default: break; } } protected void processMouseMotionEvent(MouseEvent mouseMotionEvent) { x = mouseMotionEvent.getX(); y = mouseMotionEvent.getY(); switch ( mouseMotionEvent.getID() ) { case MouseEvent.MOUSE_DRAGGED : // System.out.println("draw to " + x +" "+ y); offscreenG.drawLine(x,y, lastX,lastY); lastX = x; lastY = y; repaint(); break; default: break; } } //------------------------------------------------------------------- // getImage() allows clients access to image managed by a SigningSurface //------------------------------------------------------------------- public Image getImage() { return offscreenImage; } //------------------------------------------------------------------- // wipeSignature() allows clients to erase image managed by a SigningSurface //------------------------------------------------------------------- public void wipeSignature() { wipeImage(offscreenImage, Color.white, this); offscreenG.setColor(Color.black); repaint(); // show erased image } //------------------------------------------------------------------- // Wipe an image to some background color //------------------------------------------------------------------- public static void wipeImage(Image im, Color col, ImageObserver observer) { Graphics g; int width, height; g = im.getGraphics(); width = im.getWidth(observer); height = im.getHeight(observer); g.setColor(col); g.fillRect(0,0, width, height); } } // End of Class SigningSurface