分别用python和Java写一个钟表程序,要求有圆形的图形化界面,动态显示表针
时间: 2023-08-31 19:40:23 浏览: 113
### 回答1:
对于这个问题,我可以回答。以下是Python和Java的代码示例:
Python代码:
```python
import tkinter as tk
import time
class Clock:
def __init__(self, root):
self.root = root
self.canvas = tk.Canvas(root, width=300, height=300)
self.canvas.pack()
self.draw_clock()
self.update_clock()
def draw_clock(self):
self.canvas.create_oval(50, 50, 250, 250, width=2)
for i in range(12):
x1 = 150 + 100 * math.cos(math.radians(30 * i - 90))
y1 = 150 + 100 * math.sin(math.radians(30 * i - 90))
x2 = 150 + 120 * math.cos(math.radians(30 * i - 90))
y2 = 150 + 120 * math.sin(math.radians(30 * i - 90))
self.canvas.create_line(x1, y1, x2, y2, width=2)
self.hour_hand = self.canvas.create_line(150, 150, 150, 100, width=4)
self.minute_hand = self.canvas.create_line(150, 150, 150, 80, width=3)
self.second_hand = self.canvas.create_line(150, 150, 150, 60, width=2, fill='red')
def update_clock(self):
now = time.localtime()
hour = now.tm_hour % 12
minute = now.tm_min
second = now.tm_sec
hour_angle = (hour + minute / 60) * 30 - 90
minute_angle = minute * 6 - 90
second_angle = second * 6 - 90
self.canvas.coords(self.hour_hand, 150, 150, 150 + 60 * math.cos(math.radians(hour_angle)), 150 + 60 * math.sin(math.radians(hour_angle)))
self.canvas.coords(self.minute_hand, 150, 150, 150 + 80 * math.cos(math.radians(minute_angle)), 150 + 80 * math.sin(math.radians(minute_angle)))
self.canvas.coords(self.second_hand, 150, 150, 150 + 100 * math.cos(math.radians(second_angle)), 150 + 100 * math.sin(math.radians(second_angle)))
self.root.after(100, self.update_clock)
root = tk.Tk()
clock = Clock(root)
root.mainloop()
```
Java代码:
```java
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class Clock extends JPanel implements Runnable {
private Thread thread;
private Calendar calendar;
private int hour, minute, second;
public Clock() {
setPreferredSize(new Dimension(300, 300));
setBackground(Color.WHITE);
thread = new Thread(this);
thread.start();
}
public void run() {
while (true) {
calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR_OF_DAY) % 12;
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int width = getWidth();
int height = getHeight();
int cx = width / 2;
int cy = height / 2;
int r = Math.min(width, height) / 2 - 10;
g2d.setColor(Color.BLACK);
g2d.drawOval(cx - r, cy - r, 2 * r, 2 * r);
for (int i = ; i < 12; i++) {
double angle = Math.toRadians(30 * i - 90);
int x1 = (int) (cx + (r - 20) * Math.cos(angle));
int y1 = (int) (cy + (r - 20) * Math.sin(angle));
int x2 = (int) (cx + r * Math.cos(angle));
int y2 = (int) (cy + r * Math.sin(angle));
g2d.drawLine(x1, y1, x2, y2);
}
double hourAngle = Math.toRadians((hour + minute / 60.) * 30 - 90);
double minuteAngle = Math.toRadians(minute * 6 - 90);
double secondAngle = Math.toRadians(second * 6 - 90);
g2d.setStroke(new BasicStroke(4));
g2d.setColor(Color.BLUE);
g2d.draw(new Line2D.Double(cx, cy, cx + (r - 60) * Math.cos(hourAngle), cy + (r - 60) * Math.sin(hourAngle)));
g2d.setStroke(new BasicStroke(3));
g2d.setColor(Color.GREEN);
g2d.draw(new Line2D.Double(cx, cy, cx + (r - 40) * Math.cos(minuteAngle), cy + (r - 40) * Math.sin(minuteAngle)));
g2d.setStroke(new BasicStroke(2));
g2d.setColor(Color.RED);
g2d.draw(new Line2D.Double(cx, cy, cx + (r - 20) * Math.cos(secondAngle), cy + (r - 20) * Math.sin(secondAngle)));
}
public static void main(String[] args) {
JFrame frame = new JFrame("Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Clock());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
### 回答2:
Python 代码示例:
```python
import tkinter as tk
import time
from math import cos, sin, pi
def update_clock():
current_time = time.strftime("%H:%M:%S")
hour = int(current_time[:2])
minute = int(current_time[3:5])
second = int(current_time[6:8])
second_angle = -pi / 2 + (2 * pi) / 60 * second
minute_angle = -pi / 2 + (2 * pi) / 60 * minute
hour_angle = -pi / 2 + (2 * pi) / 12 * hour + (2 * pi) / 12 * (minute / 60)
canvas.delete("all")
canvas.create_oval(50, 50, 250, 250)
canvas.create_line(150, 150, 150 + 80 * cos(hour_angle), 150 + 80 * sin(hour_angle), width=4)
canvas.create_line(150, 150, 150 + 100 * cos(minute_angle), 150 + 100 * sin(minute_angle), width=3)
canvas.create_line(150, 150, 150 + 120 * cos(second_angle), 150 + 120 * sin(second_angle), fill='red')
root.after(1000, update_clock)
root = tk.Tk()
root.title("Python钟表程序")
canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()
update_clock()
root.mainloop()
```
Java 代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Clock extends JFrame {
public Clock() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
double secondAngle = -Math.PI / 2 + (2 * Math.PI) / 60 * second;
double minuteAngle = -Math.PI / 2 + (2 * Math.PI) / 60 * minute;
double hourAngle = -Math.PI / 2 + (2 * Math.PI) / 12 * hour + (2 * Math.PI) / 12 * (minute / 60.0);
g2d.drawOval(50, 50, 200, 200);
g2d.setStroke(new BasicStroke(4));
g2d.draw(new Line2D.Double(150, 150, 150 + 80 * Math.cos(hourAngle), 150 + 80 * Math.sin(hourAngle)));
g2d.setStroke(new BasicStroke(3));
g2d.draw(new Line2D.Double(150, 150, 150 + 100 * Math.cos(minuteAngle), 150 + 100 * Math.sin(minuteAngle)));
g2d.setColor(Color.RED);
g2d.draw(new Line2D.Double(150, 150, 150 + 120 * Math.cos(secondAngle), 150 + 120 * Math.sin(secondAngle)));
new Timer(1000, e -> repaint()).start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Clock());
}
}
```
以上两个程序分别使用Python和Java语言编写了一个钟表程序。它们都具有圆形图形化界面,并能够动态地显示表针的位置,每隔一秒钟更新一次。程序通过获取当前系统时间来计算表针的角度,并使用数学函数来确定表针的坐标位置。最后,使用绘图函数将表针绘制在图形化界面上。
### 回答3:
钟表程序是一种可以动态显示时间的程序,要求有圆形的图形界面,并且能够动态显示表针。下面分别用Python和Java来实现这个程序。
Python版本:
```python
import turtle
import datetime
def draw_clock():
window = turtle.Screen()
window.bgcolor("white")
window.title("Python Clock")
clock = turtle.Turtle()
clock.shape("circle")
clock.color("black")
clock.width(3)
def draw_hour_hand():
hour_hand = turtle.Turtle()
hour_hand.shape("arrow")
hour_hand.shapesize(stretch_wid=0.3, stretch_len=6)
hour_hand.penup()
hour_hand.speed(0)
hour_hand.goto(0, 0)
hour_hand.pendown()
hour = datetime.datetime.now().hour % 12
angle = (hour / 12) * 360
hour_hand.setheading(-angle)
def draw_minute_hand():
minute_hand = turtle.Turtle()
minute_hand.shape("arrow")
minute_hand.shapesize(stretch_wid=0.3, stretch_len=9)
minute_hand.penup()
minute_hand.speed(0)
minute_hand.goto(0, 0)
minute_hand.pendown()
minute = datetime.datetime.now().minute
angle = (minute / 60) * 360
minute_hand.setheading(-angle)
def draw_second_hand():
second_hand = turtle.Turtle()
second_hand.shape("arrow")
second_hand.shapesize(stretch_wid=0.3, stretch_len=12)
second_hand.penup()
second_hand.speed(0)
second_hand.goto(0, 0)
second_hand.pendown()
second = datetime.datetime.now().second
angle = (second / 60) * 360
second_hand.setheading(-angle)
while True:
draw_hour_hand()
draw_minute_hand()
draw_second_hand()
turtle.done()
draw_clock()
```
Java版本:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Clock extends JPanel {
public Clock() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.getContentPane().add(this);
frame.setVisible(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int radius = Math.min(getWidth(), getHeight()) / 2;
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
g2d.setColor(Color.BLACK);
g2d.fillOval(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
int secondLength = (int) (radius * 0.8);
int minuteLength = (int) (radius * 0.7);
int hourLength = (int) (radius * 0.5);
int secondX = (int) (xCenter + secondLength * Math.sin(second * 2 * Math.PI / 60));
int secondY = (int) (yCenter - secondLength * Math.cos(second * 2 * Math.PI / 60));
int minuteX = (int) (xCenter + minuteLength * Math.sin(minute * 2 * Math.PI / 60));
int minuteY = (int) (yCenter - minuteLength * Math.cos(minute * 2 * Math.PI / 60));
int hourX = (int) (xCenter + hourLength * Math.sin((hour * 60 + minute) * 2 * Math.PI / 720));
int hourY = (int) (yCenter - hourLength * Math.cos((hour * 60 + minute) * 2 * Math.PI / 720));
g2d.setStroke(new BasicStroke(10));
g2d.setColor(Color.RED);
g2d.drawLine(xCenter, yCenter, secondX, secondY);
g2d.setStroke(new BasicStroke(20));
g2d.setColor(Color.BLUE);
g2d.drawLine(xCenter, yCenter, minuteX, minuteY);
g2d.setStroke(new BasicStroke(30));
g2d.setColor(Color.GREEN);
g2d.drawLine(xCenter, yCenter, hourX, hourY);
}
public static void main(String[] args) {
new Clock();
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Calendar.getInstance();
}
}
}
```
以上分别用Python和Java实现了一个钟表程序,具有圆形的图形化界面,并且可以动态显示表针。需要注意的是,Java版本使用Swing来创建图形界面,Python版本使用turtle库来实现图形界面。请根据个人需求选择适合的版本进行使用。
阅读全文