问题:让我们表示大小的模拟时钟的初始大小。
当客户端的半径为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);
}
}