Simplified Animation
Hi,
I am new to Java 2D. I am mainly have done web/database java stuff.
I am doing a project for friend who is a Neurologist. It is pretty simple really. I just need a ball to back an forth across the screen and need to be able to set the velocity of the ball to various speeds. It has to be as accurate as possible because it was for a research project on perception.
All the examples I have seen in Java seem overly complicated for to do what I want. I was wondering if there was some API library for animation that I don't know about.
Basically I need to able to say move this circle at X speed for so long. Or move this circle for D distance and take T time to do it.
Thanks for any help
[730 byte] By [
tedbowa] at [2007-11-27 10:23:31]

# 1
Not sure, but the easiest way I can think of off the top of my head would be to use a Swing Timer and a BufferedImage. Have a method that's called something like drawStuff() and have the timer call it every however-many milliseconds.
In the drawStuff method, you first call createGraphics() to get a Graphics2D object (let's say we call it my2D). So the very rough and not-optimized code would be something like this:
private void drawStuff() {
Graphics2D my2D=myBufferedImage.createGraphics();
my2D.setColor(java.awt.color.Black);
my2D.fillRect(0,0, myBufferedImage.getWidth(),myBufferedImage.getHeight()); //This clears the image
my2D.setColor(java.awt.color.Red);
my2D.drawOval(currentBallX, currentBallY, ballDiameter, ballDiameter);
my2D.dispose();
myCanvasPanel.paintImmediately(0,0,myBufferedImage.getWidth(), myBufferedImage.getHeight());
}
The object myCanvasPanel is an instance of the following baby inner class (that should be added to the current class):
public class MyCanvasPanel extends JPanel
{
public void paint(Graphics g) //overrides the JPanel one
{
Graphics2D my2D=(Graphics2D)g;
my2D.drawImage(myBufferedImage, 0, 0, null);
}
}
You should also have the timer call a doPhysics() method, one that changes the currentBallX and currentBallY. A decent way would be to have the variables currentBallVelocityX and currentBallVelocityY. Every timer cycle, have it do something like:
currentBallX = currentBallX + currentBallVelocityX;
if ((currentBallX > rightSideX) &&(currentBallVelocityX>0)) currentBallVelocityX=currentBallVelocityX * -1); //ball bounces off right wall
//and so on
I haven't tested this particular code, and again, it's very rough and not optimal, but it's a good start to play around with. It may be better to, instead of using a JPanel, to make an Icon object, and simply add it to a label.
JLA
# 2
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class BouncieBall extends JFrame
{
public static void main(String[] args) {new BouncieBall();}
private Timer tim=new Timer(50,new MoveBall());
// The direction of the ball, 0 is right, 1 left
private int dir=0;
private double speed=10;
private Point2D pos=null;
private Dimension2D size=null;
private Ellipse2D ball=null;
public BouncieBall()
{
this.setSize(400,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
// Set
size=new Dimension(this.getWidth()/4,this.getHeight()/4);
pos=new Point2D.Double(0,(this.getHeight()/2)-(size.getHeight()/2));
ball=new Ellipse2D.Double(pos.getX(),pos.getY(),
size.getWidth(),size.getHeight());
this.setVisible(true);
// Start the timer
tim.start();
}
public void paint(Graphics g)
{
// The buffer to draw on
BufferedImage buffer=new BufferedImage(this.getWidth(),this.getHeight(),
BufferedImage.TYPE_3BYTE_BGR);
// The buff G
Graphics2D buffG=(Graphics2D)buffer.getGraphics();
//super.paint(g);
if (isOpaque()) { //paint background
buffG.setColor(getBackground());
buffG.fillRect(0, 0, getWidth(), getHeight());
}
// Fill it in red
buffG.setPaint(Color.red);
buffG.fill(ball);
// Draw the buffer
g.drawImage(buffer,0,0,null);
}
private class MoveBall implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// If x is less then or equal to 0 (on the left)
if (pos.getX()<=0)
{
// Move right
dir=0;
} // More or equal to width (on the right)
// - width cos pos is top left corner
else if (pos.getX()>=(getWidth()-size.getWidth()))
{
// Move left
dir=1;
}
// If right
if (dir==0)
{
// Add speed to x
pos.setLocation(pos.getX()+speed,pos.getY());
} // Left
else
{
// - speed
pos.setLocation(pos.getX()-speed,pos.getY());
}
// Set the frame
ball.setFrame(pos,size);
// Update the ball
repaint();
}
}
}
:D