我有這個運行程序時會自動生成動畫的小項目。在這種情況下,橢圓形應該使用線程連續動畫。然而,在我的程序中,它將停止第五個方向 - 意味着遵循某個路徑。任何人都可以給我一個更好的解決方案,或者幫助我讓橢圓形不斷移動,直到用戶關閉程序。當使用可運行線程時循環不連續
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);
}
}
}
}
參見http://stackoverflow.com/questions/7551069/update-ui-using-swingworker-thread?rq=1 – 2016-02-29 06:09:20
當'horizontalBoundary'和'verticalBoundary'都是true和'pattern'時,代碼中會發生什麼?是5? –