File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Animation " Watch "Animation " New topic
Author

Animation

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();
myAnimator.go();

} // 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");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyPanel panel = new MyPanel();

frame.getContentPane().add(panel);
frame.setSize(300,300);
frame.setVisible(true);

panel.repaint();

while(true) {

try {

Thread.sleep(50);

} catch (Exception ex) {

System.out.println(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;

}

panel.repaint();

} // end of while loop

} // end of go method

// inner class for creating the Panel

class MyPanel extends JPanel {

public void paintComponent(Graphics g) {

g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());

g.setColor(Color.blue);
g.fillRect(currX,currY,imageWidth,imageHeight);

}

} // 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://"http://faq.javaranch.com/view?HowToAskQuestionsOnJavaRanch"" target="_blank" rel="nofollow">Learn How to Ask Your Question</a> and be nice
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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.
CR
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.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
We're only too pleased to help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Animation