Java 如何使用swing制作更好更快的挂钟?

Java 如何使用swing制作更好更快的挂钟?,java,swing,Java,Swing,我已经开始了,但是我不知道如何在时钟上加上数字。估算线是一件非常痛苦的事情你知道如何做得更简单更快吗?这是我的密码 import java.awt.*; import javax.swing.*; public class DrawingProject extends JFrame { public static void main(String[] args) { DrawingProject frame = new DrawingProject();

我已经开始了,但是我不知道如何在时钟上加上数字。估算线是一件非常痛苦的事情你知道如何做得更简单更快吗?这是我的密码

import java.awt.*;
import javax.swing.*;

public class DrawingProject extends JFrame
{
    public static void main(String[] args)
{
        DrawingProject frame = new DrawingProject();
        frame.setSize(750, 750);
        frame.setVisible(true);
}

public void paint(Graphics g)
{
    g.drawOval(170, 170, 300, 300);
    g.drawLine(235, 235, 330, 330);
    g.drawLine(235, 235, 237, 257);
    g.drawLine(235, 235, 257, 237);
    g.drawLine(330, 255, 330, 330);
    g.drawLine(330, 255, 345, 270);
    g.drawLine(330, 255, 313, 270);


}
}
用于实现在何处查找号码。一整圈有360度。有12个小时,这意味着从角度0开始每隔30度必须放置一个数字

.

用于了解数字的位置。一整圈有360度。有12个小时,这意味着从角度0开始每隔30度必须放置一个数字


.

考虑使用。有了它,你可以给每个点一个到中心的距离(总是一样的)和一个旋转角度(你可以为每个数字增加这个角度)。

考虑使用它。使用它,你可以给每个点一个到中心的距离(总是相同的)和一个旋转角度(你可以为每个数字增加这个角度)。

首先,用坐标画线是不好的做法。特别是如果这些坐标可以用一些简单的几何计算出来。单击提供给您的第一个链接,我们可以看到以下公式:

// polar to Cartesian
double x = Math.cos( angleInRadians ) * radius;
double y = Math.sin( angleInRadians ) * radius;

// Cartesian to polar.
double radius = Math.sqrt( x * x + y * y );
double angleInRadians = Math.acos( x / radius );
资料来源:

前两行完全符合您的要求!它们计算在轴上以一定角度移动的距离。我们可以使用这些公式来计算线路的终点。对于起点,我们将使用框架的中心

现在,让我们将这些公式放入一些方法中:

private int calculateHorizontalOffset(int angle, int radius) {
    return (int) Math.round(Math.cos(Math.toRadians(angle)) * radius);
}

private int calculateVerticalOffset(int angle, int radius) {
    return (int) Math.round(Math.sin(Math.toRadians(angle)) * radius);
}
现在很容易计算直线的坐标,下面是最终结果:

import java.awt.*;
import javax.swing.*;

public class DrawingProject extends JFrame {

    /**
     * This is the size of our clock
     */
    int radius = 300;

    public static void main(String[] args) {
        DrawingProject frame = new DrawingProject();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(750, 750);
        frame.setVisible(true);
    }

    private int calculateHorizontalOffset(int angle, int radius) {
        return (int) Math.round(Math.cos(Math.toRadians(angle)) * radius);
    }

    private int calculateVerticalOffset(int angle, int radius) {
        return (int) Math.round(Math.sin(Math.toRadians(angle)) * radius);
    }

    public void paint(Graphics g) {

        int offsetX, offsetY;
        //here we calculate the center of our drawing pane
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;

        //then we draw a circle using the data we have so far
        g.drawOval(centerX - radius / 2, centerY - radius / 2, radius, radius);

        //then, with our formulas, we create a line to the left (angle = 0)
        offsetX = calculateHorizontalOffset(0, radius / 2 - 50);
        offsetY = calculateVerticalOffset(0, radius / 2 - 50);
        g.drawLine(centerX, centerY, centerX + offsetX, centerY + offsetY);

        //and a second line to the top (angle = 270)
        offsetX = calculateHorizontalOffset(270, radius / 2 - 10);
        offsetY = calculateVerticalOffset(270, radius / 2 - 10);
        g.drawLine(centerX, centerY, centerX + offsetX, centerY + offsetY);
    }
}

另一个技巧是,JFrame的默认关闭操作是HIDE_ON_CLOSE。仅仅隐藏帧通常是不够的。我们希望在窗口关闭后立即关闭应用程序。为此,我使用了EXIT_ON_CLOSE。

首先,使用坐标画线是不好的做法。特别是如果这些坐标可以用一些简单的几何计算出来。单击提供给您的第一个链接,我们可以看到以下公式:

// polar to Cartesian
double x = Math.cos( angleInRadians ) * radius;
double y = Math.sin( angleInRadians ) * radius;

// Cartesian to polar.
double radius = Math.sqrt( x * x + y * y );
double angleInRadians = Math.acos( x / radius );
资料来源:

前两行完全符合您的要求!它们计算在轴上以一定角度移动的距离。我们可以使用这些公式来计算线路的终点。对于起点,我们将使用框架的中心

现在,让我们将这些公式放入一些方法中:

private int calculateHorizontalOffset(int angle, int radius) {
    return (int) Math.round(Math.cos(Math.toRadians(angle)) * radius);
}

private int calculateVerticalOffset(int angle, int radius) {
    return (int) Math.round(Math.sin(Math.toRadians(angle)) * radius);
}
现在很容易计算直线的坐标,下面是最终结果:

import java.awt.*;
import javax.swing.*;

public class DrawingProject extends JFrame {

    /**
     * This is the size of our clock
     */
    int radius = 300;

    public static void main(String[] args) {
        DrawingProject frame = new DrawingProject();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(750, 750);
        frame.setVisible(true);
    }

    private int calculateHorizontalOffset(int angle, int radius) {
        return (int) Math.round(Math.cos(Math.toRadians(angle)) * radius);
    }

    private int calculateVerticalOffset(int angle, int radius) {
        return (int) Math.round(Math.sin(Math.toRadians(angle)) * radius);
    }

    public void paint(Graphics g) {

        int offsetX, offsetY;
        //here we calculate the center of our drawing pane
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;

        //then we draw a circle using the data we have so far
        g.drawOval(centerX - radius / 2, centerY - radius / 2, radius, radius);

        //then, with our formulas, we create a line to the left (angle = 0)
        offsetX = calculateHorizontalOffset(0, radius / 2 - 50);
        offsetY = calculateVerticalOffset(0, radius / 2 - 50);
        g.drawLine(centerX, centerY, centerX + offsetX, centerY + offsetY);

        //and a second line to the top (angle = 270)
        offsetX = calculateHorizontalOffset(270, radius / 2 - 10);
        offsetY = calculateVerticalOffset(270, radius / 2 - 10);
        g.drawLine(centerX, centerY, centerX + offsetX, centerY + offsetY);
    }
}

另一个技巧是,JFrame的默认关闭操作是HIDE_ON_CLOSE。仅仅隐藏帧通常是不够的。我们希望在窗口关闭后立即关闭应用程序。我用EXIT_ON_CLOSE来做这个。

你说的
更快的时钟是什么意思?如果它是一个时钟,它意味着与其他时钟一起运行,但如果它运行得很快,它会有任何用途吗?:-)最好把你的摆钟放在太空中,这样会使它比地面上的其他摆钟跑得更快:-)更快的钟?就像一个能在900毫秒内完成一秒钟的机器人一样“.更好更快的挂钟…”“如果更好的是“定位”,更快的是“编码”,也许解决方案是将它变成一个数字钟@GagandeepBali“最好把你的秋千放在太空中,这样它会比地面上的其他人跑得更快”啧啧啧啧。只有当你的时钟所处的重力场小于地面上的时钟时(例如,没有接近黑洞),并且没有非常非常快地移动(相对于观察者)。我的上一个时钟程序可以在这里找到:。如果我必须自己说的话,这是一个不错的小时钟。你说的快一点的时钟是什么意思?如果它是一个时钟,它意味着与其他时钟一起运行,但如果它运行得很快,它会有任何用途吗?:-)最好把你的摆钟放在太空中,这样会使它比地面上的其他摆钟跑得更快:-)更快的钟?就像一个能在900毫秒内完成一秒钟的机器人一样“.更好更快的挂钟…”“如果更好的是“定位”,更快的是“编码”,也许解决方案是将它变成一个数字钟@GagandeepBali“最好把你的秋千放在太空中,这样它会比地面上的其他人跑得更快”啧啧啧啧。只有当你的时钟所处的重力场小于地面上的时钟时(例如,没有接近黑洞),并且没有非常非常快地移动(相对于观察者)。我的上一个时钟程序可以在这里找到:。一个不错的小闹钟,如果我必须自己说的话。挂钟不必移动。只是设计而已。有点混淆了所有这些代码和注释。x-x挂钟不必移动。只是设计而已。有点混淆了所有这些代码和注释。x-x