Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用可运行线程时循环不连续_Java_Multithreading_Swing_Swingworker - Fatal编程技术网

Java 使用可运行线程时循环不连续

Java 使用可运行线程时循环不连续,java,multithreading,swing,swingworker,Java,Multithreading,Swing,Swingworker,我有一个小项目,在运行程序时,它会自动设置动画。在这种情况下,椭圆形应使用线程连续设置动画。然而,在我的程序中,它将在第五个方向停止——这意味着沿着某条路径。有谁能给我一个更好的解决方案,或者帮助我让椭圆形不断移动,直到用户关闭程序 package movingball; import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swi

我有一个小项目,在运行程序时,它会自动设置动画。在这种情况下,椭圆形应使用线程连续设置动画。然而,在我的程序中,它将在第五个方向停止——这意味着沿着某条路径。有谁能给我一个更好的解决方案,或者帮助我让椭圆形不断移动,直到用户关闭程序

    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);
                }
            }      
        }   
    }
封装移动球;
导入java.awt.Color;
导入java.awt.Graphics;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类MovingBall扩展了JPanel{
私人int ballX=30;
私人int ballY=30;
私有int模式=1;
私人室内高度;
私人有限宽度;
布尔水平边界=真;
布尔垂直边界=真;
公众运动球{
挫折背景(颜色:黑色);
}
公共移动球(整数x,整数y){
x=这个.ballX;
y=这个。巴里;
重新油漆();
}
公共静态void main(字符串[]args){
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(500700);
MovingBall MovingBall=新的MovingBall();
帧。添加(移动球);
frame.setVisible(true);
BallUsingRead ball=新的BallUsingRead(移动球);
螺纹优先=新螺纹(球);
首先,开始();
}
@凌驾
公共虚空绘制组件(图形画布){
super.paintComponent(画布);
canvas.setColor(Color.BLUE);
画布.圆角(ballX,ballY,100100);
}
public void animateBall(){
if(水平边界和垂直边界){
如果(模式==1){
对角线向下(getWidth()-100365);
}else if(模式==2){
对角线向下(getWidth(),150);
}
}
如果(!horizontalBoundary&&!verticalBoundary){
对角线左下角(150,getHeight());
模式=4;
} 
如果(水平边界和垂直边界){
如果(模式==4){
对角线向上(0490);
} 
如果(模式==5){
系统输出打印(“helo”);
对角线向上(500,10);
}
系统输出打印(“最后一步”+图案);
}
if(!水平边界和垂直边界){
对角线垂直(getWidth(),100);
模式=5;
系统输出打印(图案);
}
重新油漆();
}
公共空间对角线向下(int limitWidth,int limitHeight){
this.limitWidth=limitWidth;
this.limitHeight=limitHeight;
如果((ballX+=30)>=limitWidth){
水平边界=假;
} 
如果((巴利+=30)>=极限高度){
垂直边界=假;
} 
}
公共空间对角线向上(int limitWidth,int limitHeight){
this.limitWidth=limitWidth;
this.limitHeight=limitHeight;

如果((ballX-=30)这不是循环的问题,如果您
System.out.println(“循环运行”);
您将看到循环正在运行,但在代码的这一部分中,您的绘图球逻辑存在问题

if(!horizontalBoundary && verticalBoundary){
    diagonalUpRight(getWidth(),100);
    pattern = 5;
    System.out.print(pattern);
}
您正在尝试
diagonalvertight()
,因此在调用此方法之后,将执行
if
子句中的none,因为条件不满足,所以球在每个循环中都保持在相同的位置,并且似乎循环停止工作

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

解决方案:如果仔细检查逻辑,您将看到没有用于以下条件的
If
子句:

horizontalBoundary = true
verticalBoundary = true
pattern=5
时,当这两个变量变为
true
pattern=5
时,球的位置不会发生任何变化,因此您可以编写一个条件,用于检查这两个变量的
true

EidtEidt您代码的下面部分,您将看到循环没有停止的结果,我刚刚初始化了球的位置,值变为
true
,并且
模式=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;
    }
}
覆盖并实现此方法内的所有绘制(但不包括动画)


然后使用每100毫秒计划一次周期性事件,在该事件的处理程序中实现所有更改图片的操作,并调用
repaint()
从结尾处刷新图片。

查看当
水平边界
垂直边界
均为真且
模式
为5时,代码中会发生什么情况?谢谢,那么解决方案是什么?