home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 5.2 KB | 228 lines |
- // Clock.java
- // 19.03.96
- //
- // the clock on the control panel
-
- package cybcerone.control;
-
- import java.awt.Image;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.Point;
- import java.awt.Color;
- import java.awt.MediaTracker;
- import java.awt.Event;
-
- import cybcerone.utils.Date;
- import cybcerone.utils.Appletlike;
- import cybcerone.utils.IdPanel;
- import cybcerone.utils.Scaler;
-
- /**
- * It's a clock, what more can I say.
- */
- class Clock extends IdPanel implements Runnable {
- private static final Rectangle placement = new Rectangle (50, 0, 100, 100);
-
- private static final String id = "clock";
- private static final String statusText = "Current time";
-
- public static final String imagePath = "control/horloge/";
- private static final String bgFile = imagePath + "clock.gif";
-
- Thread timer;
-
- private Image numbers[];
- private Image colon;
- private Point secondPos[]; /* where the dots for the seconds go */
-
- private final int pointSize = 1;
-
- /* to speed up paint () */
- private Image hours1;
- private Image hours2;
- private Image minutes1;
- private Image minutes2;
- private int temp;
- private int seconds;
- private boolean drawLastSecond;
-
- /* for double buffering */
- private Image offscreenImg;
- private Graphics offscreenG;
-
- private final int x1 = scale (17);
- private final int x2 = scale (32);
- private final int x3 = scale (46);
- private final int x4 = scale (53);
- private final int x5 = scale (68);
- private final int y = scale (38);
-
- private boolean small;
-
- public Clock (Appletlike app) {
- super (id, statusText, app);
- reshape (placement);
-
- initImages ();
- initPositions ();
- setForeground (Color.white);
- setBackground (Color.black);
- setBackground (bgFile, 0);
-
- small = (Scaler.getScale () < 1.0);
- }
-
- /**
- * Load up the images used for the numbers.
- */
- private void initImages () {
- numbers = new Image[10]; /* 10 digits and the colon */
- for (int i = 0; i < 10; i++) {
- numbers[i] = app.getImage (imagePath + i + ".gif", 0);
- }
- colon = app.getImage (imagePath + "points.gif", 0);
- }
-
- /**
- * I never was all that good at trig.
- */
- private void initPositions () {
- int w = scale (placement.width - 14);
- double p = Math.PI / 30;
- double h = 0.5;
-
- secondPos = new Point[61];
-
- int w1 = w / 2 - scale (1);
- int w6 = w / 2 - scale (6);
-
- int j = scale (7);
-
- for (int i = 0; i < 61; i++) {
- secondPos[i] = new Point ((int)(w1 + w6 * Math.sin (i * p) + h + j),
- (int)(w1 - w6 * Math.cos (i * p) + h + j));
- }
- }
-
- /**
- * Start 'er tickin'.
- */
- public void start () {
- if (timer == null) {
- timer = new Thread (this);
- timer.setPriority (Thread.MAX_PRIORITY);
- timer.start ();
- }
- }
-
- /**
- * Lo and behold, time stands still.
- */
- public void stop () {
- if (timer != null)
- timer.stop ();
- timer = null;
- }
-
- /**
- * Wake up once a second and update yourself.
- */
- public void run () {
- long now;
- long updateTime = 0;
- Date time;
- Point pos;
-
- int hours, minutes;
- int oldSeconds = -2;
-
- while (timer != null) {
- if ((updateTime += 1000) < (now = System.currentTimeMillis ()))
- updateTime = now;
-
- time = new Date ();
- hours = time.getHours ();
- minutes = time.getMinutes ();
- seconds = time.getSeconds ();
-
- hours1 = numbers[hours / 10];
- hours2 = numbers[hours % 10];
- minutes1 = numbers[minutes / 10];
- minutes2 = numbers[minutes % 10];
-
- if (oldSeconds == seconds - 1) {
- drawLastSecond = true;
- } else {
- oldSeconds = seconds;
- drawLastSecond = false;
- }
- repaint ();
-
- try {
- timer.sleep (updateTime - System.currentTimeMillis ());
- } catch (InterruptedException e) {
- }
- }
- }
-
- /**
- * The pretty little clock in the lower left-hand corner.
- */
- public void paint (Graphics g) {
- g.setColor (getBackground ());
- g.fillRect (0, 0, size().width, size().height);
-
- super.paint (g);
-
- g.setColor (getForeground ());
- for (temp = seconds; temp >= 0; temp--) {
- if (small) {
- if (temp % 2 == 0)
- g.fillRect (secondPos[temp].x, secondPos[temp].y,
- pointSize, pointSize);
- } else {
- g.fillRect (secondPos[temp].x, secondPos[temp].y,
- pointSize, pointSize);
- }
- }
-
- g.drawImage (hours1, x1, y, this);
- g.drawImage (hours2, x2, y, this);
-
- g.drawImage (colon, x3, y, this);
-
- g.drawImage (minutes1, x4, y, this);
- g.drawImage (minutes2, x5, y, this);
- }
-
- /**
- * Use clipping and double-buffering to keep the clock moving at a
- * decent pace, without flicker.
- */
- public void update (Graphics g) {
- // for double buffering;
-
- if (offscreenG == null) {
- offscreenImg = createImage (size().width, size().height);
- offscreenG = offscreenImg.getGraphics ();
- }
-
- if (drawLastSecond) {
- g.clipRect (secondPos[seconds].x, secondPos[seconds].y,
- pointSize, pointSize);
- drawLastSecond = false;
- }
-
- paint (offscreenG);
- g.drawImage (offscreenImg, 0, 0, this);
- }
-
- /** So that the ControlPanel doesn't clear the status bar */
- public boolean mouseMove (Event evt, int x, int y) {
- return true;
- }
-
-
- }
-