问题:让我们表示大小的模拟时钟的初始大小。
当客户端的半径为r时,t秒中同心的半径为r*t/60。
将刻度添加到手表中。
设计是自由。
您可以使用形状来标记比例或添加数字。
import javax.swing.;
import java.awt.;
import java.time.*;
public class ClockWriter extends JPanel {
private final int SIZE;
public ClockWriter(int n) {
SIZE = n;
JFrame frame = new JFrame();
frame.setTitle("Clock");
frame.setSize(SIZE+50, SIZE+150);
frame.getContentPane().add(this);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g) {
g.setColor(Color.BLUE);
g.drawString("TIME IS GOLD", 105, 50);
g.setColor(Color.LIGHT_GRAY);
g.fillOval(25, 100, SIZE, SIZE);
LocalTime now = LocalTime.now();
int radius = SIZE / 2;
int x1 = 25 + radius;
int y1 = 100 + radius;
radius -= 30;
double t = now.getSecond();
double l = t/60;
int r = (int)(SIZE*(l));
g.setColor(Color.PINK);
g.fillOval(r,r,r,r);
double minute_angle = (now.getMinute() - 15) * Math.PI / 30;
int x2 = x1 + (int)(radius * Math.cos(minute_angle));
int y2 = y1 + (int)(radius * Math.sin(minute_angle));
g.setColor(Color.RED);
g.drawLine(x1, y1, x2, y2);
radius -= 30;
double hour_angle = (now.getHour() - 3) * Math.PI / 6 + minute_angle / 12;
x2 = x1 + (int)(radius * Math.cos(hour_angle));
y2 = y1 + (int)(radius * Math.sin(hour_angle));
g.setColor(Color.YELLOW);
g.drawLine(x1, y1, x2, y2);
}
public static void main(String[] args) {
new ClockWriter(250);
}
}
帮你调整了一下参数,分针的角度如果以12点钟方向为基准,比较好计算,顺便加了一下动态刷新和刻度盘。如果对你有帮助,请采纳
public class ClockWriter extends JPanel{
private final int SIZE;
public ClockWriter(int n) {
SIZE = n;
JFrame frame = new JFrame();
frame.setTitle("Clock");
frame.setSize(SIZE+50, SIZE+150);
frame.getContentPane().add(this);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g) {
g.setColor(Color.BLUE);
g.drawString("TIME IS GOLD", 105, 50);
g.setColor(Color.LIGHT_GRAY);
g.fillOval(25, 100, SIZE, SIZE);
LocalTime now = LocalTime.now();
int radius = SIZE / 2;
int x1 = 25 + radius;
int y1 = 100 + radius;
radius -= 30;
double t = now.getSecond();
double l = t/60;
int r = (int)(SIZE*(l));
// 绘制刻度盘
paintClockDialog(g);
g.setColor(Color.PINK);
// 改动行1
g.fillOval(25 + (SIZE - r) / 2, 100 + (SIZE - r) / 2, r, r);
// 改动行2,12点方向顺时针到分针的夹角
double minute_angle = 2 * Math.PI * now.getMinute() / 60;
// 改动行3, 4
int x2 = x1 + (int)(radius * Math.sin(minute_angle));
int y2 = y1 - (int)(radius * Math.cos(minute_angle));
g.setColor(Color.RED);
g.drawLine(x1, y1, x2, y2);
radius -= 30;
double hour_angle = (now.getHour() - 3) * Math.PI / 6 + minute_angle / 12;
x2 = x1 + (int)(radius * Math.cos(hour_angle));
y2 = y1 + (int)(radius * Math.sin(hour_angle));
g.setColor(Color.YELLOW);
g.drawLine(x1, y1, x2, y2);
}
private void paintClockDialog(Graphics g) {
g.setColor(Color.BLACK);
int radius = SIZE / 2;
int x = 25 + radius, y = 100 + radius;
for (int i = 0; i < 12; i++) {
double angle = 2 * Math.PI * i / 12d;
int x1 = x + (int)((radius - 9) * Math.sin(angle));
int y1 = y + (int)((radius - 9) * Math.cos(angle));
int x2 = x + (int)(radius * Math.sin(angle));
int y2 = y + (int)(radius * Math.cos(angle));
g.drawLine(x1, y1, x2, y2);
}
}
public static void main(String[] args) {
// 改动5,1秒钟刷新1次
ClockWriter clockWriter = new ClockWriter(250);
while (true) {
clockWriter.repaint(1000);
}
}
}