I have this small project that upon running the program it will automatically animate. In this case an oval shape should continuously animate using a thread. However in my program it will stop for the fifth direction - meaning following a certain path. Can anyone suggest me a better solution or help me let the oval shape continuously move until the user closes the program.
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);
}
}
}
}