/* * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM) * Published By SunSoft Press/Prentice-Hall * Copyright (C) 1996 Sun Microsystems Inc. * All Rights Reserved. ISBN 0-13-565755-5 * * Permission to use, copy, modify, and distribute this * software and its documentation for NON-COMMERCIAL purposes * and without fee is hereby granted provided that this * copyright notice appears in all copies. * * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ /** * @version 1.00 07 Feb 1996 * @author Cay Horstmann */ import java.awt.*; public class Sketch extends Frame { public Sketch() { setTitle("Sketch"); } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(evt); } public boolean keyDown(Event evt, int key) { int d = ((evt.modifiers & Event.SHIFT_MASK) == 0) ? 1 : 5; if (key == Event.LEFT) add(-d, 0); else if (key == Event.RIGHT) add(d, 0); else if (key == Event.UP) add(0, -d); else if (key == Event.DOWN) add(0, d); else return false; return true; } public void update(Graphics g) { paint(g); requestFocus(); } public void paint(Graphics g) { g.drawLine(start.x, start.y, end.x, end.y); start.x = end.x; start.y = end.y; } public void add(int dx, int dy) { end.x += dx; end.y += dy; repaint(); } public static void main(String[] args) { Frame f = new Sketch(); f.resize(300, 200); f.show(); } private Point start = new Point(0, 0); private Point end = new Point(0, 0); }