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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
| package labs;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Clock {
private int hours;
private int minutes;
public Clock(int hours, int minutes) {
this.setHours(hours);
this.setMinutes(minutes);
}
public void setMinutes(int minutes) {
this.minutes = minutes % 60;
}
public int getMinutes() {
return minutes;
}
public void setHours(int hours) {
this.hours = hours % 24;
}
public int getHours() {
return hours;
}
}
public class ClockPanel extends JPanel {
private Clock clock;
public ClockPanel(Clock сlock) {
setDoubleBuffered(true);
setPreferredSize(new Dimension(600, 400));
setClock(сlock);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (clock != null) {
drawClock(g, clock);
}
}
/**
* Метод рисует на канве круг с заданным центром, радиусом и цветом
* @param g канва
* @param color цвет круга
* @param center центр круга
* @param radius радиус круга
*/
private void drawCircle(Graphics g, Point center, int radius) {
g.setColor(Color.RED);
g.fillOval(center.x - radius / 2, center.y - radius / 2, radius, radius);
}
public Point convertCoordinatesToPanel(Point point) {
Point O = new Point(getSize().width / 2, getSize().height / 2);
return new Point(O.x + point.x, O.y - point.y);
}
private Point getEndPoint(double angle, int radius) {
Point O = new Point(getSize().width / 2, getSize().height / 2);
int x = (int) (O.x + radius * Math.cos(angle));
int y = (int) (O.y - radius * Math.sin(angle));
return new Point(x, y);
}
private void drawClock(Graphics g, Clock сlock) {
Point O = new Point(getSize().width / 2, getSize().height / 2);
int radiusClock = Math.min(O.x, O.y) - 20;
int radiusMinute = radiusClock - 10;
int radiusHour = radiusMinute - 20;
double angle;
for (int i = 1; i < 13; i++) {
angle = Math.PI / 2 - i * Math.PI / 6;
Point point = getEndPoint(angle, radiusClock);
drawCircle(g, point, 8);
}
angle = Math.PI / 2 - (clock.getHours() + clock.getMinutes() / 60.0) * Math.PI / 6;
Point point = getEndPoint(angle, radiusHour);
g.setColor(Color.GREEN);
g.drawLine(O.x, O.y, point.x, point.y);
angle = Math.PI / 2 - clock.getMinutes() * Math.PI / 30;
point = getEndPoint(angle, radiusMinute);
g.setColor(Color.GRAY);
g.drawLine(O.x, O.y, point.x, point.y);
}
public Clock getClock() {
return clock;
}
public void setClock(Clock clock) {
this.clock = clock;
}
public static void main(String[] args) throws Exception {
JFrame f = new JFrame("Часики");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
final ClockPanel clockPanel = new ClockPanel(new Clock(0, 0));
clockPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2));
f.add(clockPanel, BorderLayout.CENTER);
JPanel controlPanel = new JPanel();
controlPanel.setBorder(BorderFactory.createTitledBorder("Настройки"));
controlPanel.setLayout(new GridLayout(2, 2, 6, 6));
JButton but = (JButton) controlPanel.add(new JButton("Часы +1"));
but.putClientProperty("plus", true);
ActionListener alHours = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
boolean plus = ((Boolean) b.getClientProperty("plus")).booleanValue();
clockPanel.getClock().setHours(clockPanel.getClock().getHours() + (plus ? 1 : -1));
clockPanel.repaint();
}
};
but.addActionListener(alHours);
but = (JButton) controlPanel.add(new JButton("Часы -1"));
but.putClientProperty("plus", false);
but.addActionListener(alHours);
but = (JButton) controlPanel.add(new JButton("Минуты +1"));
but.putClientProperty("plus", true);
ActionListener alMinutes = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
boolean plus = ((Boolean) b.getClientProperty("plus")).booleanValue();
clockPanel.getClock().setMinutes(clockPanel.getClock().getMinutes() + (plus ? 1 : -1));
clockPanel.repaint();
}
};
but.addActionListener(alMinutes);
but = (JButton) controlPanel.add(new JButton("Минуты -1"));
but.putClientProperty("plus", false);
but.addActionListener(alMinutes);
f.add(controlPanel, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
} |