use java design a running Analog clock as shown in 3rd slide
时间: 2024-04-12 12:28:14 浏览: 146
Sure, I can help you design a running analog clock in Java. Here's an example code that you can use:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class AnalogClock extends JPanel {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setResizable(false);
AnalogClock clock = new AnalogClock();
frame.add(clock);
frame.setVisible(true);
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
clock.repaint();
}
});
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int radius = Math.min(centerX, centerY) - 10;
// Draw clock outline
g2d.setColor(Color.BLACK);
g2d.drawOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
// Get current time
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
int hours = calendar.get(Calendar.HOUR);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);
// Draw hour hand
int hourHandLength = radius * 3 / 4;
int hourHandX = (int) (centerX + hourHandLength * Math.sin(Math.toRadians((hours * 30) - 90)));
int hourHandY = (int) (centerY - hourHandLength * Math.cos(Math.toRadians((hours * 30) - 90)));
g2d.drawLine(centerX, centerY, hourHandX, hourHandY);
// Draw minute hand
int minuteHandLength = radius * 11 / 12;
int minuteHandX = (int) (centerX + minuteHandLength * Math.sin(Math.toRadians((minutes * 6) - 90)));
int minuteHandY = (int) (centerY - minuteHandLength * Math.cos(Math.toRadians((minutes * 6) - 90)));
g2d.drawLine(centerX, centerY, minuteHandX, minuteHandY);
// Draw second hand
int secondHandLength = radius - 10;
int secondHandX = (int) (centerX + secondHandLength * Math.sin(Math.toRadians((seconds * 6) - 90)));
int secondHandY = (int) (centerY - secondHandLength * Math.cos(Math.toRadians((seconds * 6) - 90)));
g2d.setColor(Color.RED);
g2d.drawLine(centerX, centerY, secondHandX, secondHandY);
// Draw center point
g2d.setColor(Color.BLACK);
g2d.fillOval(centerX - 5, centerY - 5, 10, 10);
}
}
```
This code creates a simple Swing application that displays an analog clock. It uses the `paintComponent` method to paint the clock's components (outline, hands, and center point). The `Timer` class is used to update the clock every second by calling the `repaint` method.
You can run this code in a Java IDE or compile it using the command line. I hope this helps! Let me know if you have any further questions.
阅读全文