2016-02-29 62 views
0

我有这个运行程序时会自动生成动画的小项目。在这种情况下,椭圆形应该使用线程连续动画。然而,在我的程序中,它将停止第五个方向 - 意味着遵循某个路径。任何人都可以给我一个更好的解决方案,或者帮助我让椭圆形不断移动,直到用户关闭程序。当使用可运行线程时循环不连续

package movingball; 

    import java.awt.Color; 
    import java.awt.Graphics; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 

    public class MovingBall extends JPanel{ 


    private int ballX = 30; 
    private int ballY = 30; 
    private int pattern = 1; 
    private int limitHeight; 
    private int limitWidth; 
    boolean horizontalBoundary = true; 
    boolean verticalBoundary = true; 

    public MovingBall(){ 
     setBackground(Color.BLACK); 
    } 
    public MovingBall(int x, int y){ 
     x = this.ballX; 
     y = this.ballY; 
     repaint(); 
    } 
    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(500,700); 

     MovingBall movingBall = new MovingBall(); 
     frame.add(movingBall);  
     frame.setVisible(true); 

     BallUsingThread ball = new BallUsingThread(movingBall); 
     Thread first = new Thread(ball); 

     first.start(); 

    } 
    @Override 
    public void paintComponent(Graphics canvas){ 
     super.paintComponent(canvas); 

     canvas.setColor(Color.BLUE); 
     canvas.fillOval(ballX, ballY, 100, 100); 
    } 

    public void animateBall(){ 

     if(horizontalBoundary && verticalBoundary){ 
      if(pattern == 1){ 
       diagonalDown(getWidth()-100,365); 

      }else if(pattern == 2){ 
       diagonalDown(getWidth(),150); 
      } 
     } 
     if(!horizontalBoundary && !verticalBoundary){   
      diagonalDownLeft(150, getHeight()); 
      pattern = 4;    
     } 
     if(horizontalBoundary && !verticalBoundary){   
      if(pattern == 4){ 
       diagonalUp(0, 490); 
      } 
      if(pattern == 5){ 
       System.out.print("helo"); 
       diagonalUp(500,10); 
      } 
      System.out.print("last move" + pattern); 
     } 
     if(!horizontalBoundary && verticalBoundary){ 
      diagonalUpRight(getWidth(),100); 
      pattern = 5; 
      System.out.print(pattern); 
     } 
     repaint(); 
    } 
    public void diagonalDown(int limitWidth, int limitHeight){ 
     this.limitWidth = limitWidth; 
     this.limitHeight = limitHeight; 

     if((ballX += 30) >= limitWidth){ 
      horizontalBoundary = false; 
     } 
     if((ballY += 30) >= limitHeight){ 
      verticalBoundary = false; 
     } 

    } 
    public void diagonalUp(int limitWidth, int limitHeight){ 
     this.limitWidth = limitWidth; 
     this.limitHeight = limitHeight; 

     if((ballX -= 30) <= limitWidth) { 
      horizontalBoundary = false; 
     } 
     if((ballY -= 30) <= limitHeight){ 
      verticalBoundary = true; 
     } 
    } 
    public void diagonalUpRight(int limitWidth, int limitHeight){ 
     this.limitWidth = limitWidth; 
     this.limitHeight = limitHeight; 

     if((ballX += 30) >= limitWidth) { 
      horizontalBoundary = true; 
     } 
     if((ballY -= 30) <= limitHeight){ 
      verticalBoundary = false; 
     } 

    } 
    public void diagonalDownLeft(int limitWidth, int limitHeight){ 
     this.limitWidth = limitWidth; 
     this.limitHeight = limitHeight; 

     if((ballX -= 30) <= limitWidth){ 
      horizontalBoundary = true; 
     } 
     if((ballY += 30) >= limitHeight){ 
      verticalBoundary = false; 

     } 
     //System.out.print("downleft"); 
    } 

} 

     class BallUsingThread implements Runnable{ 

     private final MovingBall movingBall; 
     public BallUsingThread(MovingBall mb){ 
      movingBall = mb; 
     } 
     @Override 
     public void run() { 
      for(;;){ 
       movingBall.animateBall(); 
       try { 
        Thread.sleep(100); 
       } catch (InterruptedException ex) { 
        System.out.printf("Error",ex); 
       } 
      }  
     } 
    } 
+0

参见http://stackoverflow.com/questions/7551069/update-ui-using-swingworker-thread?rq=1 – 2016-02-29 06:09:20

+0

当'horizo​​ntalBoundary'和'verticalBoundary'都是true和'pattern'时,代码中会发生什么?是5? –

回答

0

它不与循环的问题,如果你System.out.println("loop running");你会看到,循环运行,但有一个与你的抽奖球的逻辑问题,在你的代码,这部分

if(!horizontalBoundary && verticalBoundary){ 
    diagonalUpRight(getWidth(),100); 
    pattern = 5; 
    System.out.print(pattern); 
} 

你试图diagonalUpRight(),所以在一些调用这个方法后,然后if子句都不会执行,因为条件不满足,所以球在每个循环都保持在相同的位置,似乎循环停止加工。

小建议:在这种情况下,最好使用while(tru /*some condition*/)而不是for(;;)

解决方案:如果你仔细检查,你会看到,有没有被用于以下条件的if条款的逻辑:

horizontalBoundary = true 
verticalBoundary = true 

pattern = 5所以,当这两个变量成为truepattern = 5那么球位置不会发生任何变化,因此您可以编写一个条件,用于检查这两个变量的值是否为true

Eidt Eidt你的代码下面的部分,你会看到哪个环路是不停止的结果,我只是初始化球的位置值变为truepattern = 5

if(horizontalBoundary && verticalBoundary){ 
    if(pattern == 1){ 
     diagonalDown(getWidth()-100,365); 

    }else if(pattern == 2){ 
     diagonalDown(getWidth(),150); 
    } else { // added this else block 
      ballX = 30; 
      ballY = 30; 
      pattern = 1; 
    } 
} 
+0

感谢那么解决方案呢? –

+0

答案已更新,你可以查看。 –

0

重写paintComponent(Graphics)并实现此方法内的所有绘制(但不包括动画)。

然后使用Swing timer安排周期性事件在每100毫秒,实现一切改变在此事件的处理程序的图片,并呼吁从那里repaint()在年底刷新画面。