有没有办法像Java中的模拟时钟一样旋转Rectangles2D?

时间:2018-06-12 12:03:41

标签: java

我必须用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();
}

我的意图是使用计时器旋转手,但如果没有允许我这样做的方法,我就被困住了!有人能帮我吗?非常感谢提前。

0 个答案:

没有答案