用java写圆盘钟表的图形化界面
时间: 2024-05-09 09:16:11 浏览: 62
以下是一个简单的圆盘钟表的Java图形化界面的示例代码:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Clock extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
private static final int MARGIN = 20;
private static final int RADIUS = (WIDTH - 2 * MARGIN) / 2;
private static final Point CENTER = new Point(WIDTH / 2, HEIGHT / 2);
private static final int TICK_LENGTH = 10;
private static final int HOUR_HAND_LENGTH = RADIUS / 2;
private static final int MINUTE_HAND_LENGTH = (int) (RADIUS * 0.8);
private static final int SECOND_HAND_LENGTH = (int) (RADIUS * 0.9);
private static final Font FONT = new Font("Arial", Font.PLAIN, 14);
private Timer timer;
public Clock() {
timer = new Timer(1000, this);
timer.start();
setPreferredSize(new java.awt.Dimension(WIDTH, HEIGHT));
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawTicks(g2);
drawNumbers(g2);
drawHands(g2);
}
private void drawTicks(Graphics2D g2) {
g2.setColor(Color.BLACK);
for (int i = 0; i < 60; i++) {
int tickLength = i % 5 == 0 ? 2 * TICK_LENGTH : TICK_LENGTH;
double angle = Math.PI * i / 30;
int x1 = (int) (CENTER.x + (RADIUS - tickLength) * Math.sin(angle));
int y1 = (int) (CENTER.y - (RADIUS - tickLength) * Math.cos(angle));
int x2 = (int) (CENTER.x + RADIUS * Math.sin(angle));
int y2 = (int) (CENTER.y - RADIUS * Math.cos(angle));
g2.drawLine(x1, y1, x2, y2);
}
}
private void drawNumbers(Graphics2D g2) {
g2.setFont(FONT);
g2.setColor(Color.BLACK);
for (int i = 1; i <= 12; i++) {
double angle = Math.PI * (i - 3) / 6;
int x = (int) (CENTER.x + (RADIUS - 2 * TICK_LENGTH) * Math.sin(angle));
int y = (int) (CENTER.y - (RADIUS - 2 * TICK_LENGTH) * Math.cos(angle));
g2.drawString(Integer.toString(i), x, y);
}
}
private void drawHands(Graphics2D g2) {
LocalTime now = LocalTime.now();
int hour = now.getHour() % 12;
int minute = now.getMinute();
int second = now.getSecond();
double hourAngle = Math.PI * (hour - 3) / 6 + Math.PI * minute / 360;
int hourX = (int) (CENTER.x + HOUR_HAND_LENGTH * Math.sin(hourAngle));
int hourY = (int) (CENTER.y - HOUR_HAND_LENGTH * Math.cos(hourAngle));
g2.setColor(Color.BLACK);
g2.drawLine(CENTER.x, CENTER.y, hourX, hourY);
double minuteAngle = Math.PI * (minute - 15) / 30;
int minuteX = (int) (CENTER.x + MINUTE_HAND_LENGTH * Math.sin(minuteAngle));
int minuteY = (int) (CENTER.y - MINUTE_HAND_LENGTH * Math.cos(minuteAngle));
g2.setColor(Color.BLUE);
g2.drawLine(CENTER.x, CENTER.y, minuteX, minuteY);
double secondAngle = Math.PI * (second - 15) / 30;
int secondX = (int) (CENTER.x + SECOND_HAND_LENGTH * Math.sin(secondAngle));
int secondY = (int) (CENTER.y - SECOND_HAND_LENGTH * Math.cos(secondAngle));
g2.setColor(Color.RED);
g2.drawLine(CENTER.x, CENTER.y, secondX, secondY);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Clock(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
在这个示例中,我们使用Java Swing构建了一个圆盘钟表,它包含一个时针、分针和秒针,以及刻度线和数字。我们使用了Java的时间库来获取当前的时间,并根据当前时间计算时针、分针和秒针的角度。我们使用了Java的绘图库来绘制钟表的图形,并使用定时器来实现每秒钟更新一次钟表的显示。
阅读全文