java - 有没有办法像 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();
}
我的意图是使用计时器来旋转指针,但没有允许我这样做的方法,我被卡住了!有人能帮我吗?提前谢谢了。
解决方案
推荐阅读
- haskell - Haskell - 用不同的函数迭代元组
- javascript - Firebase - 使用回调检索和存储数组中的数据
- python - 使用 Flask 运行时启动功能
- javascript - PDF.JS:由于缩放而重新加载/重新渲染停止 JS 事件
- angular - Angular 6 Mat-Dialog 无法从路由器插座打开
- python - 我如何才能在此字典中的“byUSer”列表用户中获得平衡?
- javascript - 检查一个字符串是否startsWith()并且不等于另一个字符串
- php - PHP如何给代码块一个父节点
- bash - 从命名管道捕获非零退出代码
- merge - 从临时表中删除其他更新