/* * 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 PanelTest extends Frame { public PanelTest() { setTitle("PanelTest"); Panel p = new Panel(); p.setLayout(new FlowLayout()); p.add(new Button("Tick")); p.add(new Button("Reset")); p.add(new Button("Close")); add("South", p); clock = new ClockCanvas(); add("Center", clock); } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(evt); } public boolean action(Event evt, Object arg) { if (arg.equals("Tick")) clock.tick(); else if (arg.equals("Reset")) clock.reset(); else if (arg.equals("Close")) System.exit(0); else return false; return true; } public static void main(String[] args) { Frame f = new PanelTest(); f.resize(300, 200); f.show(); } private ClockCanvas clock; } class ClockCanvas extends Canvas { public void paint(Graphics g) { g.drawOval(0, 0, 100, 100); double hourAngle = 2 * Math.PI * (minutes - 3 * 60) / (12 * 60); double minuteAngle = 2 * Math.PI * (minutes - 15) / 60; g.drawLine(50, 50, 50 + (int)(30 * Math.cos(hourAngle)), 50 + (int)(30 * Math.sin(hourAngle))); g.drawLine(50, 50, 50 + (int)(45 * Math.cos(minuteAngle)), 50 + (int)(45 * Math.sin(minuteAngle))); } public void reset() { minutes = 0; repaint(); } public void tick() { minutes++; repaint(); } int minutes = 0; }