首页 > 解决方案 > 有没有办法像 Java 中的模拟时钟指针一样旋转 Rectangles2D?

问题描述

我必须在 Java 中创建一个模拟时钟,为此我使用 Rectangles2D,就好像它们是指针一样。现在我需要一种允许我围绕象限中心旋转它们的方法,但我找不到任何可以让我这样做的方法。我暂时留下我写的代码。

    Ellipse2D out, in;
    Rectangle2D h, m, s;

    public Graphics2DComponent() {
        out=new Ellipse2D.Double(500, 400, 500, 500);
        in=new Ellipse2D.Double(500, 400, 470, 470);
        h=new Rectangle2D.Double(500, 400, 5, 150);
        m=new Rectangle2D.Double(500, 400, 5, 200);
        s=new Rectangle2D.Double(500, 400, 5, 150);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2=(Graphics2D)g;

        g2.setPaint(Color.GRAY);
        g2.fill(out);
        g2.setPaint(Color.WHITE);
        g2.fill(in);
        g2.setPaint(Color.BLACK);
        g2.fill(h);
        g2.fill(m);
        g2.setPaint(Color.RED);
        g2.fill(s);
    }
}

Timer seconds, minutes, hours;

public Clock() {
    super("Clock");
    setSize(1000, 1000);
    setLocationRelativeTo(null);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Graphics2DComponent panel=new Graphics2DComponent();
    panel.out.setFrame(getWidth()/2-panel.out.getWidth()/2, getHeight()/2-panel.out.getHeight()/2, panel.out.getWidth(), panel.out.getHeight());
    panel.in.setFrame(getWidth()/2-panel.in.getWidth()/2, getHeight()/2-panel.in.getHeight()/2, panel.in.getWidth(), panel.in.getHeight());
    panel.h.setFrame(getWidth()/2-panel.h.getWidth()/2, getHeight()/2-panel.h.getHeight(), panel.h.getWidth(), panel.h.getHeight());
    panel.m.setFrame(getWidth()/2-panel.m.getWidth()/2, getHeight()/2-panel.m.getHeight(), panel.m.getWidth(), panel.m.getHeight());
    panel.s.setFrame(getWidth()/2-panel.s.getWidth()/2, getHeight()/2-panel.s.getHeight(), panel.s.getWidth(), panel.s.getHeight());

    add(panel);
    validate();
}

我的意图是使用计时器来旋转指针,但没有允许我这样做的方法,我被卡住了!有人能帮我吗?提前谢谢了。

标签: java

解决方案


推荐阅读