2011-11-14 57 views
0

我有一个程序,创建一个随着多线程移动到随机位置的圆,每个线程将处理每个圆的运动。我知道如何移动图像而不是形状对象。java移动一个形状对象

g2d.draw(s.circle);

这一行只能绘制带有spawn x y的圆。我想尝试添加 s.circle.getBounds()。setLocation(s.x,s.y); 之前 g2d.draw(s.circle); 但没有效果

public void paint(Graphics g) { 
    if (draw == true) { 


     super.paint(g); 
     Graphics2D g2d = (Graphics2D)g; 

      for (Star s : this.items) { 
       //g2d.drawImage(s.starImage, s.x, s.y, this); 

       g2d.draw(s.circle); 

      } 

     Toolkit.getDefaultToolkit().sync(); 
     g.dispose(); 

    } 
} 

public void run() { 

    //if (!items.isEmpty()) { 
     while(true){ 
      try { 
       for (Star s : this.items) { 
        s.move(); 
       } 
       repaint(); 

       Thread.sleep(50); 
      } catch (InterruptedException ex) { 
       Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    //} 
} 
+0

发生了什么?顺便说一句,我不认为你需要一个线程的单一形状。 –

+0

单击一个圈一个动作 – hkguile

+0

我知道它的伙伴,我做了一个像你的应用程序,它运行在一个单一的线程。 –

回答

0

你将不得不

 
1) draw the circle 
2) wait for some short interval (eg, 100ms) 
3) REDRAW the same circle - This time with the background colour, so that it has the effect of erasing the shape. 
4) draw the circle at the new location. 
5) repeat. 

ATLEAST上述工作对我来说,当我做Java/2D - 不张贴实际的代码很抱歉,因为它早就是 - 但我相信你会知道它:)

+0

谢谢,完了〜 – hkguile

0

尝试清除屏幕,然后再次绘画。使用双缓冲来防止闪烁。