Jump to content

Java animation help

Featured Replies

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){

g.drawRect(inset,inset,rectWidth,rectHeight);

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

  • 2 weeks later...

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

 tags

 

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

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.