File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Animation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Animation " Watch "Animation " New topic


Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
Trying some animation, and the animation works, however there is a probelm. When the rectangle gets to the bottom of the frame it disappears and then comes back, and when it hits the left side, it goes past the frame (theoretically) and then comes back, but I have no idea why. Here is my code if you want to run it, but I'm baffled by it. It should compile and run without a problem.

import javax.swing.*;
import java.awt.*;

// this class will be for animating a box around the inside of a frame

public class Animate {

// creating instance variables for the location, size, and speed of said box

private int currX = (int)((Math.random() * 80) + 1);
private int currY = (int)((Math.random() * 80) + 10);
private int imageHeight = 25;
private int imageWidth = 40;
private int xVelocity = 3;
private int yVelocity = 3;

public static void main(String[] args) {

Animate myAnimator = new Animate();

} // end of main method

// buids gui and starts the anmiation (note I will be doing this with a thread later

public void go() {

JFrame frame = new JFrame("Animator");

MyPanel panel = new MyPanel();



while(true) {

try {


} catch (Exception ex) {



currX = currX + xVelocity;
currY = currY + yVelocity;

// setting it so if the box reaches the edges of the frame
// it goes in the other direction

if ((currX + imageWidth) >= frame.getWidth()) {

xVelocity = -3;

} else if (currX <= 0) {

xVelocity = 3;


if ((currY + imageHeight) >= frame.getHeight()) {

yVelocity = -3;

} else if (currY <= 0) {

yVelocity = 3;



} // end of while loop

} // end of go method

// inner class for creating the Panel

class MyPanel extends JPanel {

public void paintComponent(Graphics g) {




} // end of inner class

} // end of out class

Teaching yourself anything is always the cheapest way, but it definitely takes a lot of time and effort.<br /> <br />Thank you javaranch <a href="http://""" target="_blank" rel="nofollow">Learn How to Ask Your Question</a> and be nice
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 41994
Thank you. I had a similar problem a week or two back, only it was doing peculiar things at the left of the screen rather than at the bottom.

Why are you setting the colour of the background to white, when it defaults to white anyway? Take out the bit saying
and replace it with what you are supposed to write which is

Try altering what height you are using for test of currY.
What you are using for both your tests is frame.getWidth() and frame.getHeight(). You have forgotten that the JFrame is larger than the JPanel you are drawing on; I think it is 8 px wider and about 25 px taller. You can actually see that your blue rectangle goes off the panel at the right as well, but you have to look very closely to see it.
Try taking the sizes from your JPanel, see whether that works.
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
k, that works. I was just basically just repainting the frame before I repainted the image so that it would appear to be animating instead of just strecthing. Anyways, it worked. I played with the size of the panel and frame, and of how big the image can be and I was able to get it to where I needed it.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 41994
We're only too pleased to help.
I agree. Here's the link:
subject: Animation