m@0
|
1 import java.awt.*;
|
m@0
|
2 import java.awt.geom.*;
|
m@0
|
3 import javax.swing.*;
|
m@0
|
4
|
m@0
|
5 /** Clocks displayed in a panel. */
|
m@0
|
6 public class Clock extends JPanel implements Runnable {
|
m@0
|
7
|
m@0
|
8 /* The default colours */
|
m@0
|
9 static final Color BACKGROUND = Color.black;
|
m@0
|
10 static final Color FOREGROUND = Color.white;
|
m@0
|
11
|
m@0
|
12 /* Panel width, height and border. */
|
m@0
|
13 public static final int WIDTH = 450;
|
m@0
|
14 public static final int BORDER = 50;
|
m@0
|
15
|
m@0
|
16 /* Minutes in a day. */
|
m@0
|
17 public static final int ONE_DAY = 24 * 60;
|
m@0
|
18
|
m@0
|
19 /* Show the clock? */
|
m@0
|
20 public Boolean showClock = false;
|
m@0
|
21 public Boolean showFullClock = false;
|
m@0
|
22
|
m@0
|
23 /* minutes since midnight, range 0 to (ONE_DAY - 1) inclusive */
|
m@0
|
24 private int minutes = 0;
|
m@0
|
25
|
m@0
|
26 /* Milliseconds per tick of the minute hand */
|
m@0
|
27 private long millisecondsPerTick = 1000;
|
m@0
|
28 public void setMillisecondsPerTick(long n) { millisecondsPerTick = n; }
|
m@0
|
29
|
m@0
|
30 /* Create a new Clock at time midnight. */
|
m@0
|
31 public Clock() {
|
m@0
|
32 this.setBackground(BACKGROUND);
|
m@0
|
33 this.setPreferredSize (new Dimension (WIDTH, WIDTH));
|
m@0
|
34 }
|
m@0
|
35
|
m@0
|
36 public void run() {
|
m@0
|
37 reset();
|
m@0
|
38 if (showClock == false) {
|
m@0
|
39 showClock = true;
|
m@0
|
40 repaint();
|
m@0
|
41 }
|
m@0
|
42 for (int i = 0; i < 60; i++) {
|
m@0
|
43 try { Thread.sleep (millisecondsPerTick); }
|
m@0
|
44 catch (InterruptedException e) {}
|
m@0
|
45 tick(1);
|
m@0
|
46 repaint();
|
m@0
|
47 }
|
m@4
|
48 showClock = false;
|
m@4
|
49 showFullClock = false;
|
m@4
|
50 reset();
|
m@4
|
51 repaint();
|
m@4
|
52 }
|
m@0
|
53
|
m@0
|
54 /* Move time on by 1 second. */
|
m@0
|
55 public void tick(int n) {
|
m@0
|
56 minutes = (minutes + n) % ONE_DAY;
|
m@0
|
57 }
|
m@0
|
58
|
m@0
|
59 /* Reset the clock to 00:00. */
|
m@0
|
60 public void reset() {
|
m@0
|
61 minutes = 0;
|
m@0
|
62 }
|
m@0
|
63
|
m@0
|
64 /* Draw the clock or fixation point. */
|
m@0
|
65 public void paintComponent (Graphics g) {
|
m@0
|
66 super.paintComponent (g);
|
m@0
|
67
|
m@0
|
68 if (showClock == true) {
|
m@0
|
69 paintClock(g);
|
m@0
|
70 } else {
|
m@0
|
71 paintFixationPoint(g);
|
m@0
|
72 }
|
m@0
|
73 }
|
m@0
|
74
|
m@0
|
75 private void paintFixationPoint(Graphics g) {
|
m@4
|
76 //System.out.println("paintFixationPoint");
|
m@0
|
77 int left_edge = BORDER;
|
m@0
|
78 int top = BORDER;
|
m@0
|
79 int diameter = WIDTH - (BORDER * 2);
|
m@0
|
80 int centrex = left_edge + (diameter / 2);
|
m@0
|
81 int centrey = top + (diameter / 2);
|
m@0
|
82
|
m@0
|
83 int linelength = diameter / 20;
|
m@0
|
84
|
m@0
|
85 g.setColor(FOREGROUND);
|
m@0
|
86 g.drawLine(centrey, centrex - linelength, centrey, centrex + linelength);
|
m@0
|
87 g.drawLine(centrey - linelength, centrex, centrey + linelength, centrex);
|
m@0
|
88 }
|
m@0
|
89
|
m@0
|
90 private void paintClock (Graphics g) {
|
m@4
|
91 //System.out.println("paintClock");
|
m@0
|
92 int minutes_angle = 90 - ((minutes % 60) * 6);
|
m@0
|
93 int hours_angle = 90 - ((minutes / 60) * 30);
|
m@0
|
94 int left_edge = BORDER;
|
m@0
|
95 int top = BORDER;
|
m@0
|
96 int diameter = WIDTH - (BORDER * 2);
|
m@0
|
97 int radius = diameter / 2;
|
m@0
|
98 int centrex = left_edge + radius;
|
m@0
|
99 int centrey = top + radius;
|
m@0
|
100
|
m@0
|
101 g.setColor(FOREGROUND);
|
m@0
|
102
|
m@0
|
103 // draw the clock as a black circle:
|
m@0
|
104 g.drawOval(left_edge, top, diameter, diameter);
|
m@0
|
105
|
m@0
|
106 // draw the hour numbers
|
m@0
|
107 int offset = 0;
|
m@0
|
108 for(int i = 1; i <= 12; i++) {
|
m@0
|
109 int hours_pos = 90 - (i * 30);
|
m@0
|
110 int x = (centrex - offset) +
|
m@0
|
111 (int)((radius - 5) * Math.cos(hours_pos * Math.PI / 180));
|
m@0
|
112 int y = (centrey + offset) -
|
m@0
|
113 (int)((radius - 5) * Math.sin(hours_pos * Math.PI / 180));
|
m@0
|
114 //g.drawString(String.valueOf(i), x, y);
|
m@0
|
115 g.drawOval(x, y, diameter / 200, diameter / 200);
|
m@0
|
116 }
|
m@0
|
117
|
m@0
|
118 // get the coordinate of the line_ends
|
m@0
|
119 int minutesx = (int)((radius - 5) *
|
m@0
|
120 Math.cos(minutes_angle * Math.PI / 180));
|
m@0
|
121 int minutesy = (int)((radius - 5) *
|
m@0
|
122 Math.sin(minutes_angle * Math.PI /180));
|
m@0
|
123
|
m@0
|
124 // draw the minutes' hand
|
m@0
|
125 g.drawLine(centrex, centrey, centrex + minutesx, centrey - minutesy);
|
m@0
|
126
|
m@0
|
127 int arc_angle = minutes_angle - 90;
|
m@0
|
128 Graphics2D g2 = (Graphics2D)g;
|
m@0
|
129 g2.setPaint(FOREGROUND);
|
m@0
|
130
|
m@0
|
131 //System.out.println("minutes_angle = " + minutes_angle +
|
m@0
|
132 // "; arc_angle = " + arc_angle);
|
m@0
|
133
|
m@0
|
134 if (arc_angle == 0 & showFullClock == true) {
|
m@0
|
135 g2.fillOval(left_edge, top, diameter, diameter);
|
m@0
|
136 showFullClock = false;
|
m@0
|
137 } else {
|
m@0
|
138 showFullClock = true;
|
m@0
|
139 if (arc_angle >= -180) {
|
m@0
|
140 Arc2D pie = new Arc2D.Float(centrex - radius, centrey - radius,
|
m@0
|
141 2 * radius, 2 * radius,
|
m@0
|
142 90, arc_angle,
|
m@0
|
143 Arc2D.PIE);
|
m@0
|
144 g2.fill(pie);
|
m@0
|
145 } else {
|
m@0
|
146 Arc2D pie1 = new Arc2D.Float(centrex - radius, centrey - radius,
|
m@0
|
147 2 * radius, 2 * radius,
|
m@0
|
148 90, -180, Arc2D.PIE);
|
m@0
|
149 Arc2D pie2 = new Arc2D.Float(centrex - radius, centrey - radius,
|
m@0
|
150 2 * radius, 2 * radius,
|
m@0
|
151 270, arc_angle + 180,
|
m@0
|
152 Arc2D.PIE);
|
m@0
|
153 g2.fill(pie1);
|
m@0
|
154 g2.fill(pie2);
|
m@0
|
155 }
|
m@0
|
156 }
|
m@0
|
157 }
|
m@0
|
158 }
|