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