-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClockComponent.java
More file actions
executable file
·39 lines (32 loc) · 1.07 KB
/
Copy pathClockComponent.java
File metadata and controls
executable file
·39 lines (32 loc) · 1.07 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
import java.awt.*;
import javax.swing.JComponent;
import javax.swing.Timer;
import java.time.LocalTime;
public class ClockComponent extends JComponent {
public ClockComponent() {
}
public void paint(Graphics g) {
// the frame's dimensions
final int w = getWidth(); // width
final int h = getHeight(); // height
// clear the specified rectangle by filling it with the background colour
// useful when resizing because we want to erase what there was before
g.clearRect(0, 0, w, h);
// set up our transform to be relative to a 200x200 box
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// move to centre
g2.translate(w/2.0, h/2.0);
// scale
int min = Math.min(w, h);
double scaleFactor = min/(double)Clock.SCALE;
g2.scale(scaleFactor, scaleFactor);
// move to top left of bound
g2.translate(-Clock.CENTRE, -Clock.CENTRE);
// draw the 200x200 box
Clock c = new Clock();
c.draw(g2);
g2.dispose();
}
}