Jump to content

Java animation help


Recommended Posts

ok so i have an assignment to do in which i have to have 4 squares moving inwards at each corner of the applet screen one next to another (2 by 2)

i have one of the squares working but dont know how to do the other 3 (positioning)


a little help would be appreciated... this is what i have so far:




import java.awt.*;

public class MovingRects2 extends SimpleAnimationApplet {

public void drawFrame(Graphics g){


int width; //Width of the applet, in pixels.

int height; //Height of the applet, in pixels.


int inset; //Gap between border of applet and a rectangle.

//The inset for the outmost rectangle goes from 0 to 14 then back to

//0, and so on, as the frameNumber varies.


int rectWidth, rectHeight; //the size of one of the rectangles.


width = getWidth(); //Find out the seize of the drawing area.

height = getHeight();


g.setColor(Color.green); //Fill the frame with green

g.fillRect(0, 0,width,height);


g.setColor(Color.black); //Switch color to black.


inset = getFrameNumber() % 15; //Get the inset for the outermost rect


rectWidth = (width - 4*inset - 1)/2; //set size of outermost rect.

rectHeight = (height -4*inset - 1)/2;


while (rectWidth >= 0 && rectHeight >= 0){


inset += 15; //Rects are 15 pixels apart.

rectWidth -= 30; //Width decreases by 15 pixels on left and 15 on right.

rectHeight -= 30; //Height decreases by 15 pixels on top and 15 on bottom.




}//end drawFrame()

}//end class MovingRects

Edited by russellmaster
Link to comment
Share on other sites

  • 2 weeks later...

I think your code is messy, besides that you should use



You should take care of what should be done the first time this code runs,


and what things that should run on every update, which progress your animation

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.