-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.java
More file actions
executable file
·151 lines (122 loc) · 4.38 KB
/
Copy pathClock.java
File metadata and controls
executable file
·151 lines (122 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Creates a clock that is inspired by the Touhou character, Cirno.
* @author Michelle Chan
* @version 31-5-2017
*
*/
import java.time.LocalTime;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.BasicStroke;
import javax.swing.*;
import java.net.URL;
import javax.imageio.ImageIO;
import java.io.*;
public class Clock {
BufferedImage img = null;
//constants for the frame
public final static double RADIANS = 2*Math.PI;
public final static int SCALE = 200;
public final static int CENTRE = SCALE/2;
//constants for the clock hands
private final static int NUM_TICKS = 12;
private final static int NUM_FAT_TICKS = 4;
private final static int TICK_LENGTH = SCALE / 10;
//clock face constants
public final static int CLOCK_FACE_SCALE = SCALE - 16;
public final static int CLOCK_CENTRE = (CLOCK_FACE_SCALE / 2) + 8;
//Creates a custom colour based on Cirno's dress and hair
Color cirnoLightBlue = new Color(0.435f, 0.545f, 0.976f);
Color cirnoDarkBlue = new Color(0.204f, 0.51f, 0.753f);
private LocalTime time;
public Clock() {
this(LocalTime.now());
}
public Clock(LocalTime aTime) {
time = aTime;
}
/**
* Loads and draws images given its name and position.
* @param imageName - name of the imageName
* @param x - x position of the image
* @param y - y position of the image
*/
public void drawImage(Graphics g, String imageName, int x, int y) {
//reads the image
try {
img = ImageIO.read(new File(imageName));
} catch (IOException e) {
}
g.drawImage(img, x, y, null);
}
/**
* Draws the clock face
* @param g - the graphics component
*/
public void draw(Graphics2D g) {
//draws the UI border
g.setColor(cirnoLightBlue);
RoundRectangle2D.Double border = new RoundRectangle2D.Double(0,0, SCALE - 1, SCALE - 1,0,0);
g.setStroke(new BasicStroke(2f));
g.draw(border);
//Draws the clock face and its border
Ellipse2D.Double clockFace = new Ellipse2D.Double(8, 8, CLOCK_FACE_SCALE, CLOCK_FACE_SCALE);
g.setStroke(new BasicStroke(4f));
g.draw(clockFace);
//Draws the tick marks
g.setColor(Color.BLACK);
for (int i = 1; i <= 60; i++) {
//draws the hour tick marks
int endPoint;
if (i % 5 == 0) {
//creates the stroke settings for the "cardinal" directions
if (i % 15 == 0) {
g.setStroke(new BasicStroke(3f));
}
endPoint = 20;
g.setColor(Color.BLACK);
} else {
//minute/second tick mark settings
endPoint = 15;
g.setStroke(new BasicStroke(1f));
g.setColor(cirnoDarkBlue);
}
//regardless of setting, draws the tick marks
Line2D.Double tick = new Line2D.Double(13, CLOCK_CENTRE, endPoint, CLOCK_CENTRE);
g.rotate(RADIANS/60, CENTRE, CENTRE);
g.draw(tick);
}
//draws Cirno
drawImage(g, "cirno.png", CLOCK_CENTRE - 40, CLOCK_CENTRE - 55);
//draws the clock numbers
drawImage(g, "3.png", CLOCK_FACE_SCALE - 35, CENTRE - 10);
drawImage(g, "9.png", CLOCK_CENTRE - 72, CENTRE - 6);
drawImage(g, "6.png", CLOCK_CENTRE - 30, CENTRE + 60);
drawImage(g, "12.png", CLOCK_CENTRE - 30, CENTRE - 75);
//holds the hand objects
Hand[] arr = new Hand[3];
arr = getHands();
//draws the hands
for (Hand h: arr) {
h.draw(g);
}
Ellipse2D.Double iceFastener = new Ellipse2D.Double(CENTRE - 2, CENTRE - 2, 4, 4);
g.setColor(new Color(0.56f, .95f, 1f));
g.fill(iceFastener);
g.draw(iceFastener);
g.setColor(cirnoDarkBlue);
g.setStroke(new BasicStroke(.5f));
g.draw(new Line2D.Double(CENTRE - 2.5, CENTRE, CENTRE + 2.5, CENTRE));
}
//return hand objects
public Hand[] getHands() {
LocalTime now = LocalTime.now();
Hand[] arr = new Hand[3];
//creates hands for the clock
arr[0] = new Hand(Hand.TimeUnit.SECOND, now);
arr[1] = new Hand(Hand.TimeUnit.MINUTE, now, arr[0]);
arr[2] = new Hand(Hand.TimeUnit.HOUR, now, arr[1]);
return arr;
}
}