I am trying to create a java method, move() that will change the location of my object (which is an ellipse). my ellipse has an initial x,y position so I'd like to move it along the Jframe by calling the following method from the JComponent.
public class ShapeAnimation extends Shape {
public void move() {
xVel=(int)(Math.random()*11);
yVel=(int)(Math.random()*11);
x=xVel+x;
y=yVel+y;
if(x>this.x)
xVel=xVel*-1;
if(y>this.y)
yVel=yVel*-1;
}
}
you are using x variable in
x=xVel+x;
but it is not declared in function, so java assumes it isthis.x
so your code looks like this:
you need to change it to:
maxX and maxY should have maximum values which x and y can have
NOTE - this code do not move object during some iterations, for teaching purposes I suggest you to update it for such cases