在Java用户界面上显示一个实时指针时钟
时间: 2024-05-02 14:18:50 浏览: 62
以下是一个使用Java Swing实现的实时指针时钟的示例代码:
```
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class PointerClock extends JFrame {
private JPanel contentPane;
private ClockPanel clockPanel;
public PointerClock() {
setTitle("Pointer Clock");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 400);
contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
clockPanel = new ClockPanel();
contentPane.add(clockPanel, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new PointerClock();
}
private class ClockPanel extends JPanel {
private int centerX, centerY;
private int radius;
private int hourHandLength, minuteHandLength, secondHandLength;
private int hourHandWidth, minuteHandWidth, secondHandWidth;
private Color hourHandColor, minuteHandColor, secondHandColor;
public ClockPanel() {
setOpaque(false);
Timer timer = new Timer(1000, e -> repaint());
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int size = Math.min(width, height);
centerX = width / 2;
centerY = height / 2;
radius = size / 2 - 10;
hourHandLength = (int) (radius * 0.5);
minuteHandLength = (int) (radius * 0.7);
secondHandLength = (int) (radius * 0.9);
hourHandWidth = 6;
minuteHandWidth = 4;
secondHandWidth = 2;
hourHandColor = Color.BLACK;
minuteHandColor = Color.BLACK;
secondHandColor = Color.RED;
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawClockFace(g2d);
drawHourHand(g2d);
drawMinuteHand(g2d);
drawSecondHand(g2d);
}
private void drawClockFace(Graphics2D g2d) {
g2d.setColor(Color.WHITE);
g2d.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
g2d.setColor(Color.BLACK);
g2d.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
for (int i = 1; i <= 12; i++) {
double angle = Math.PI / 6 * i;
int x = (int) (centerX + Math.sin(angle) * (radius - 20));
int y = (int) (centerY - Math.cos(angle) * (radius - 20));
g2d.drawString(String.valueOf(i), x, y);
}
}
private void drawHourHand(Graphics2D g2d) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
double angle = Math.PI / 6 * hour + Math.PI / 360 * minute + Math.PI / 21600 * second;
int x = (int) (centerX + Math.sin(angle) * hourHandLength);
int y = (int) (centerY - Math.cos(angle) * hourHandLength);
g2d.setColor(hourHandColor);
g2d.setStroke(new BasicStroke(hourHandWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(centerX, centerY, x, y);
}
private void drawMinuteHand(Graphics2D g2d) {
Calendar calendar = Calendar.getInstance();
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
double angle = Math.PI / 30 * minute + Math.PI / 1800 * second;
int x = (int) (centerX + Math.sin(angle) * minuteHandLength);
int y = (int) (centerY - Math.cos(angle) * minuteHandLength);
g2d.setColor(minuteHandColor);
g2d.setStroke(new BasicStroke(minuteHandWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(centerX, centerY, x, y);
}
private void drawSecondHand(Graphics2D g2d) {
Calendar calendar = Calendar.getInstance();
int second = calendar.get(Calendar.SECOND);
double angle = Math.PI / 30 * second;
int x = (int) (centerX + Math.sin(angle) * secondHandLength);
int y = (int) (centerY - Math.cos(angle) * secondHandLength);
g2d.setColor(secondHandColor);
g2d.setStroke(new BasicStroke(secondHandWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(centerX, centerY, x, y);
}
}
}
```
该代码使用了Java Swing中的JFrame和JPanel来创建用户界面,并使用Graphics2D类来绘制指针时钟。实时更新时钟的功能是通过使用Java定时器实现的。在绘制时钟的过程中,该代码使用了Calendar类来获取当前时间,并通过一些基本的几何计算来计算时针、分针和秒针的位置和角度。最终绘制时针、分针和秒针时,该代码使用了Graphics2D类的一些基本绘图方法来绘制它们。
阅读全文