Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/acme/gui/presObjs/applet/ClockPaint.java


1   /*
2    * Enhydra Java Application Server Project
3    * 
4    * The contents of this file are subject to the Enhydra Public License
5    * Version 1.1 (the "License"); you may not use this file except in
6    * compliance with the License. You may obtain a copy of the License on
7    * the Enhydra web site ( http://www.enhydra.org/ ).
8    * 
9    * Software distributed under the License is distributed on an "AS IS"
10   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
11   * the License for the specific terms governing rights and limitations
12   * under the License.
13   * 
14   * The Initial Developer of the Enhydra Application Server is Lutris
15   * Technologies, Inc. The Enhydra Application Server and portions created
16   * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17   * All Rights Reserved.
18   * 
19   * Contributor(s):
20   * 
21   * $Id: ClockPaint.java,v 1.6.10.1 2000/10/19 17:59:06 jasona Exp $
22   */
23  
24  
25  
26  
27  /*
28   * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
29   *
30   * Permission to use, copy, modify, and distribute this software
31   * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
32   * without fee is hereby granted.
33   * Please refer to the file http://java.sun.com/copy_trademarks.html
34   * for further important copyright and trademark information and to
35   * http://java.sun.com/licensing.html for further important licensing
36   * information for the Java (tm) Technology.
37   *
38   * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
39   * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
40   * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
41   * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
42   * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
43   * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
44   *
45   * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
46   * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
47   * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
48   * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
49   * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
50   * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
51   * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
52   * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
53   * HIGH RISK ACTIVITIES.
54   */
55  
56  // author: Rachel Gollub, 1995
57  // modified 96/04/24 Jim Hagen : use getBackground()
58  // modified 96/05/29 Rachel Gollub : add garbage collecting
59  // modified 96/10/22 Rachel Gollub : add bgcolor, fgcolor1, fgcolor2 params
60  // Time!
61  
62  /*
63   * Lutris: Split off from Clock2.java to help in the testing the loading of
64   * classes referenced by LBS.
65   */
66  
67  package com.acme.gui.presObjs.applet;
68  import java.util.*;
69  import java.awt.*;
70  import java.applet.*;
71  
72  public class ClockPaint {
73    int lastxs=0, lastys=0, lastxm=0, lastym=0, lastxh=0, lastyh=0;
74    Date dummy = new Date();
75    String lastdate = dummy.toLocaleString();
76    Font F = new Font("TimesRoman", Font.PLAIN, 14);
77    Date dat = null;
78    Color fgcol;
79    Color fgcol2;
80    Color bgcol;
81  
82  public ClockPaint(Color initFgcol,  Color initFgcol2, Color initBgcol) {
83      fgcol = initFgcol;
84      fgcol2 = initFgcol2;
85      bgcol = initBgcol;
86  }
87  
88  
89    // Plotpoints allows calculation to only cover 45 degrees of the circle,
90    // and then mirror
91  
92  public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
93  
94    g.drawLine(x0+x,y0+y,x0+x,y0+y);
95    g.drawLine(x0+y,y0+x,x0+y,y0+x);
96    g.drawLine(x0+y,y0-x,x0+y,y0-x);
97    g.drawLine(x0+x,y0-y,x0+x,y0-y);
98    g.drawLine(x0-x,y0-y,x0-x,y0-y);
99    g.drawLine(x0-y,y0-x,x0-y,y0-x);
100   g.drawLine(x0-y,y0+x,x0-y,y0+x);
101   g.drawLine(x0-x,y0+y,x0-x,y0+y);
102 }
103 
104   // Circle is just Bresenham's algorithm for a scan converted circle
105 
106 public void circle(int x0, int y0, int r, Graphics g) {
107   int x,y;
108   float d;
109 
110   x=0;
111   y=r;
112   d=5/4-r;
113   plotpoints(x0,y0,x,y,g);
114 
115   while (y>x){
116     if (d<0) {
117       d=d+2*x+3;
118       x++;
119     }
120     else {
121       d=d+2*(x-y)+5;
122       x++;
123       y--;
124     }
125     plotpoints(x0,y0,x,y,g);
126   }
127 }
128 
129 
130   // Paint is the main part of the program
131 
132 public void paint(Graphics g) {
133   int xh, yh, xm, ym, xs, ys, s, m, h, xcenter, ycenter;
134   String today;
135 
136   dat = new Date();
137   s = dat.getSeconds();
138   m = dat.getMinutes();
139   h = dat.getHours();
140   today = dat.toLocaleString();
141   xcenter=80;
142   ycenter=55;
143   
144   // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
145   // x = r(cos a) + xcenter, y = r(sin a) + ycenter
146   
147   xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
148   ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
149   xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
150   ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
151   xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
152   yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
153   
154   // Draw the circle and numbers
155   
156   g.setFont(F);
157   g.setColor(fgcol);
158   circle(xcenter,ycenter,50,g);
159   g.setColor(fgcol2);
160   g.drawString("9",xcenter-45,ycenter+3); 
161   g.drawString("3",xcenter+40,ycenter+3);
162   g.drawString("12",xcenter-5,ycenter-37);
163   g.drawString("6",xcenter-3,ycenter+45);
164 
165   // Erase if necessary, and redraw
166   
167   g.setColor(bgcol);
168   if (xs != lastxs || ys != lastys) {
169     g.drawLine(xcenter, ycenter, lastxs, lastys);
170     g.drawString(lastdate, 5, 125);
171   }
172   if (xm != lastxm || ym != lastym) {
173     g.drawLine(xcenter, ycenter-1, lastxm, lastym);
174     g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
175   if (xh != lastxh || yh != lastyh) {
176     g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
177     g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
178   g.setColor(fgcol2);
179   g.drawString(today, 5, 125);  
180   g.drawLine(xcenter, ycenter, xs, ys);
181   g.setColor(fgcol);
182   g.drawLine(xcenter, ycenter-1, xm, ym);
183   g.drawLine(xcenter-1, ycenter, xm, ym);
184   g.drawLine(xcenter, ycenter-1, xh, yh);
185   g.drawLine(xcenter-1, ycenter, xh, yh);
186   lastxs=xs; lastys=ys;
187   lastxm=xm; lastym=ym;
188   lastxh=xh; lastyh=yh;
189   lastdate = today;
190   dat=null;
191 }
192 
193 }
194