📜  Java小程序 |如何显示模拟时钟

📅  最后修改于: 2022-05-13 01:55:44.594000             🧑  作者: Mango

Java小程序 |如何显示模拟时钟

在本文中,我们将为小程序窗口设置动画,以显示延迟 1 秒的模拟时钟。这个想法是显示每个实例的系统时间。

方法:
时钟的每根指针都会以 1 秒的延迟进行动画处理,并将一端保持在中心。另一端的位置可以通过系统时间得出。时钟的指针每秒钟形成的角度在整个行程中都会有所不同。这就是为什么各种实例与水平线形成不同角度的原因。

如何运行:

1. Save the file as analogClock.java 
   and run the following commands.
2. javac analogClock.java
3. appletviewer analogClock.java

下面是上述方法的实现:

程序:

// Java program to illustrate
// analog clock using Applets
  
import java.applet.Applet;
import java.awt.*;
import java.util.*;
  
public class analogClock extends Applet {
  
    @Override
    public void init()
    {
        // Applet window size & color
        this.setSize(new Dimension(800, 400));
        setBackground(new Color(50, 50, 50));
        new Thread() {
            @Override
            public void run()
            {
                while (true) {
                    repaint();
                    delayAnimation();
                }
            }
        }.start();
    }
  
    // Animating the applet
    private void delayAnimation()
    {
        try {
  
            // Animation delay is 1000 milliseconds
            Thread.sleep(1000);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
  
    // Paint the applet
    @Override
    public void paint(Graphics g)
    {
        // Get the system time
        Calendar time = Calendar.getInstance();
  
        int hour = time.get(Calendar.HOUR_OF_DAY);
        int minute = time.get(Calendar.MINUTE);
        int second = time.get(Calendar.SECOND);
  
        // 12 hour format
        if (hour > 12) {
            hour -= 12;
        }
  
        // Draw clock body center at (400, 200)
        g.setColor(Color.white);
        g.fillOval(300, 100, 200, 200);
  
        // Labeling
        g.setColor(Color.black);
        g.drawString("12", 390, 120);
        g.drawString("9", 310, 200);
        g.drawString("6", 400, 290);
        g.drawString("3", 480, 200);
  
        // Declaring variables to be used
        double angle;
        int x, y;
  
        // Second hand's angle in Radian
        angle = Math.toRadians((15 - second) * 6);
  
        // Position of the second hand
        // with length 100 unit
        x = (int)(Math.cos(angle) * 100);
        y = (int)(Math.sin(angle) * 100);
  
        // Red color second hand
        g.setColor(Color.red);
        g.drawLine(400, 200, 400 + x, 200 - y);
  
        // Minute hand's angle in Radian
        angle = Math.toRadians((15 - minute) * 6);
  
        // Position of the minute hand
        // with length 80 unit
        x = (int)(Math.cos(angle) * 80);
        y = (int)(Math.sin(angle) * 80);
  
        // blue color Minute hand
        g.setColor(Color.blue);
        g.drawLine(400, 200, 400 + x, 200 - y);
  
        // Hour hand's angle in Radian
        angle = Math.toRadians((15 - (hour * 5)) * 6);
  
        // Position of the hour hand
        // with length 50 unit
        x = (int)(Math.cos(angle) * 50);
        y = (int)(Math.sin(angle) * 50);
  
        // Black color hour hand
        g.setColor(Color.black);
        g.drawLine(400, 200, 400 + x, 200 - y);
    }
}

输出: