/* * 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 ScrollTest extends Frame { public ScrollTest() { setTitle("ScrollTest"); add("East", vert = new Scrollbar(Scrollbar.VERTICAL)); add("South", horiz = new Scrollbar(Scrollbar.HORIZONTAL)); add("Center", canvas = new SquareCanvas()); } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); else if (evt.id == Event.WINDOW_MOVED) { Dimension d = canvas.size(); horiz.setValues(horiz.getValue(), d.width, 0, 600); vert.setValues(vert.getValue(), d.height, 0, 400); } else if (evt.id == Event.SCROLL_ABSOLUTE || evt.id == Event.SCROLL_LINE_DOWN || evt.id == Event.SCROLL_LINE_UP || evt.id == Event.SCROLL_PAGE_DOWN || evt.id == Event.SCROLL_PAGE_UP) { canvas.translate(horiz.getValue(), vert.getValue()); return true; } return super.handleEvent(evt); } public void show() { super.show(); Dimension d = canvas.size(); horiz.setValues(0, d.width, 0, 600); vert.setValues(0, d.height, 0, 400); } public static void main(String args[]) { Frame f = new ScrollTest(); f.resize(300, 200); f.show(); } private Scrollbar horiz; private Scrollbar vert; private SquareCanvas canvas; } class SquareCanvas extends Canvas { public void translate(int x, int y) { dx = x; dy = y; repaint(); } public void paint(Graphics g) { g.translate(-dx, -dy); for (int i = 0; i < nsquares; i++) draw(g, i); } public int find(int x, int y) { for (int i = 0; i < nsquares; i++) if (squares[i].x <= x && x <= squares[i].x + SQUARELENGTH && squares[i].y <= y && y <= squares[i].y + SQUARELENGTH) return i; return -1; } public void draw(Graphics g, int i) { g.drawRect(squares[i].x, squares[i].y, SQUARELENGTH, SQUARELENGTH); } public void add(int x, int y) { if (nsquares < MAXNSQUARES) { squares[nsquares] = new Point(x, y); nsquares++; repaint(); } } public void remove(int n) { nsquares--; squares[n] = squares[nsquares]; if (current == n) current = -1; repaint(); } public boolean mouseDown(Event evt, int x, int y) { x += dx; y += dy; current = find(x, y); if (current < 0) // not inside a square { add(x, y); } else if (evt.clickCount >= 2) { remove(current); } return true; } public boolean mouseDrag(Event evt, int x, int y) { x += dx; y += dy; if (current >= 0) { Graphics g = getGraphics(); g.translate(-dx, -dy); g.setXORMode(getBackground()); draw(g, current); squares[current].x = x; squares[current].y = y; draw(g, current); g.dispose(); } return true; } private static final int SQUARELENGTH = 10; private static final int MAXNSQUARES = 100; private Point[] squares = new Point[MAXNSQUARES]; private int nsquares = 0; private int current = -1; private int dx = 0; private int dy = 0; }