I am working on a Java program that uses a JApplet which makes a ball bounce up and down. I am able to paint the shape onto the JApplet and everything like that. I just can't seem to get it to move. I have looked into this and saw that I need to create a method that pauses the shape then clears it from the JApplet, changes coordinates, and then redraws the shape in the new area but for some reason it's just not working for me.
Thank you in advance for the help.
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class Circle extends JApplet {
int x=100;
int y=100;
int diameter=50;
public void paint(Graphics g) {
int xResize=500;
int yResize=500;
super.paint(g);
resize(xResize,yResize);
g.drawOval(x, y, diameter, diameter);
}
public Circle (int startX, int startY,int startDiameter) {
this.x=startX;
this.y=startY;
this.diameter=startDiameter;
}
public int getX() {
return x;
}
public void setX(int startX){
x=startX;
}
public int getY() {
return y;
}
public void setY(int startY){
y=startY;
}
public int getDiameter() {
return diameter;
}
public void setDiameter(int startDiameter){
diameter=startDiameter;
}
while (ball.getY() + ballDiameter < windowHeight) {
g.clearRect(x-1,100,20,20);
g.fillOval(x,100,20,20);
try
{
Thread.sleep(70);
}
catch(Exception e)
{
}
pause.wait(0.05);
//g.clearRect(0,0,windowWidth,windowHeight);
g.clearRect(x-1,100,20,20);
g.fillOval(x,100,20,20);
try
{
Thread.sleep(70);
}
catch(Exception e)
{
}
ball.setY( ball.getY()+spacer);
}
while (ball.getY() + ballDiameter > 0) {
g.clearRect(x-1,100,20,20);
g.fillOval(x,100,20,20);
try
{
Thread.sleep(70);
}
catch(Exception e)
{
}
pause.wait(0.05);
//g.clearRect(0,0, windowWidth, windowHeight);
ball.setY(ball.getY()-spacer);
}
Bouncing Ball Class:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class BouncingBall extends JApplet {
public void paint(Graphics g) {
super.paint(g);
final int x = 0;
int y = 0;
final int diameter = 15;
final int spacer = 5;
int windowWidth = getWidth();
int windowHeight = getHeight();
Circle ball = new Circle(x, y, diameter);
Pause pause = new Pause();
int ballDiameter = ball.getDiameter();
int roof = getHeight();