package awtExtExamples ; /* * * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved. * * 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. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES 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. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Event; import awtExt.* ; import sysExt.* ; /** * A test of an awtExt.Scrollbar invoking user defined methods. * Each scrollbar has 1 callback method registered with it. * Note: JDK Beta-2 scrollbars are somewhat broken, so they * don't respond to repeat events. The next version of the JDk * Should fix this problem. */ public class ScrollbarTest extends Frame { Scrollbar vert; Scrollbar horz; ScrollablePaintCanvas cv; public ScrollbarTest() { super("ScrollbarTest"); add("Center", cv = new ScrollablePaintCanvas()); try { /* ** Create the vertical and horizontal callbacks */ methodRef mrV = Callback.newRef( this, "vertScrollMethod" ) ; methodRef mrH = Callback.newRef( this, "horzScrollMethod" ) ; /* ** Create the vertical and horizontal scrollbars. ** Attach the callbacks to them. */ add("East", vert = new Scrollbar(Scrollbar.VERTICAL, cv.ty, 100, 0, ScrollablePaintCanvas.SIZE, mrV)); add("South", horz = new Scrollbar(Scrollbar.HORIZONTAL, cv.tx, 100, 0, ScrollablePaintCanvas.SIZE, mrH)); } catch( Exception e ) { e.printStackTrace();} ; reshape(100, 200, 300, 250); show(); } /* ** Called back from vertical scrollbar */ public void vertScrollMethod( CallbackInfo cbi ) { System.out.println( "In V Scrollbar Callback..." ); cv.ty = ((Integer)cbi.evt.arg).intValue(); cv.repaint(); } /* ** Called back from horizontal scrollbar */ public void horzScrollMethod( CallbackInfo cbi ) { System.out.println( "In H Scrollbar Callback..."); cv.tx = ((Integer)cbi.evt.arg).intValue(); cv.repaint(); } public static void main(String args[]) { new ScrollbarTest(); } } class ScrollablePaintCanvas extends Canvas { int tx = 20; int ty = 100; static final int SIZE = 500; public void paint(Graphics g) { g.translate(-tx, -ty); for (int i = 0 ; i < 200 ; i += 20) { g.drawOval(i, i, SIZE - 2*i, SIZE - 2*i); } g.setColor(Color.yellow); g.drawLine(0, SIZE/2, SIZE, SIZE/2); g.drawLine(SIZE/2, 0, SIZE/2, SIZE); g.setColor(Color.red); g.fillOval(SIZE/2 - 20, SIZE/2 - 20, 40, 40); } }