[英]Rotating a line every second using repaint and a timer
So I'm trying to rotate a line every second using a Timer and a paint method. 因此,我尝试使用计时器和绘画方法每秒旋转一条线。 However, I'm not quite sure whats going on.
但是,我不太确定发生了什么。 Here are some of the relevant methods:
以下是一些相关方法:
public static ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Clock cl = new Clock();
seconds++;
cl.repaint();
}
};
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
for(int c = 0; c<10; c++){
g2.setPaint(Color.BLACK);
g2.drawOval(90-c/2,90-c/2,500+c,500+c); //thick outlined circle
}
g2.setPaint(Color.WHITE);
g2.fillOval(90,90,501,501);
g2.setPaint(Color.BLACK);
g2.rotate(Math.toRadians(seconds*6));
g2.drawLine(340,340,340,90);
}
The line remains stationary. 线路保持静止。 However if I add
但是,如果我添加
System.out.println("tick");
to my actionPerformed method, the command line spits out "tick" 3 times a second. 对于我的actionPerformed方法,命令行每秒发出“滴答声” 3次。 Any ideas as to why these things are happening?
关于这些事情为什么发生的任何想法?
Some context: 一些背景:
public static int seconds = 0;
public static int minutes = 0;
public static int hours = 0;
public static Clock cl = new Clock();
private ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("tick");
seconds++;
cl.repaint();
}
};
public static Timer timer = new Timer(1000,taskPerformer);
public static void main(String[] args){
Clock cl = new Clock();
init();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGUI();
}
});
}
public static void init(){
timer.start();
}
public Clock() {
super("Clock");
timer.addActionListener(taskPerformer);
}
You are creating a new clock at every tick: 您正在每一个时钟创建一个新时钟:
public void actionPerformed(ActionEvent e) {
Clock cl = new Clock();
...
Instead you should use an existing instance. 相反,您应该使用现有实例。
// A field in the class:
Clock cl = new Clock();
...
// removed static so that it can access cl
private ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent e) {
seconds++;
cl.repaint();
}
};
You could also make the clock a field in the action listener, if you do not need to access it elsewhere. 如果您不需要在其他地方访问它,则也可以将时钟设置为动作侦听器中的一个字段。
Also note that you generally should not be overriding paint()
, but should override paintComponent()
instead. 还要注意,通常不应该覆盖
paint()
,而应该覆盖paintComponent()
。 More about custom painting in swing here. 有关挥杆定制绘画的更多信息,请点击此处。
Edit: Now that there's more code available, it's possible to say that if you make the clock and action listener static it should work. 编辑:现在有更多代码可用,可以说,如果将时钟和动作侦听器设为静态,则它应该可以工作。 However , you need to start the timer after the relevant components are ready:
但是 ,您需要在相关组件准备就绪后启动计时器:
public static void main(String[] args){
// Removed spurious clock here
SwingUtilities.invokeLater(new Runnable(){
public void run() {
createAndShowGUI();
// Start the timer once the components are ready
init();
}
});
}
The above mentioned point about not creating a clock in the action listener still stands. 关于在动作监听器中不创建时钟的上述要点仍然存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.