Speeding up Animation
Hello,
I have written an applet in which an image is loaded and displayed on the screen at certain coordinates. When the user clicks on it, it can be dragged anywhere on the screen. However, the image moves so slowly and jerkily it's very difficult to control it with the mouse.
Does anyone know any faster methods for performing animation of this kind?
Thanks
[393 byte] By [
lucretia6] at [2007-9-26 4:46:53]

> Post the relevant parts of your code so we can make
> suggestions of improving it...
Here is a shortened version of my code:
public class myApplet extends Applet implements Runnable, MouseMotionListener, MouseListener{
//(All initialization code omitted for the sake of simplicity)
etc.....
//Stop thread by setting mouse to 'true' if user clicks
within the rectangles that represent the mirrors
public void mousePressed(MouseEvent e) {
if(e.getX()<=((MirrorSeparation+20))&&(e.getX()>=(MirrorSeparation))&&
(e.getY()<=100)&&(e.getY()>=20)) {
mouse = true;
}
}
public void mouseDragged(MouseEvent e) {
swidth = getParameter("width");
width = Integer.parseInt(swidth);
//Update the position of the mirror
//Update the position of any photons that will end up outside the bounds of the mirror as a result of this change
if(e.getX()<(width-40)&&(e.getX()>40)) {
MirrorSeparation = e.getX();
for(int i =0; i<N; i++) {
if(x[i]>= MirrorSeparation) {
x[i] = MirrorSeparation;
}
repaint();
}}
}
//Start thread again once user as released the mouse button and finished repositioning the mirror
public void mouseReleased(MouseEvent e) {
mouse = false;
}
public void start(){
thread = new Thread(this);
thread.start();
}
public void run() {
for (;;) {
if(mouse == false) {
//Update coordinates of objects
for(int i=0; i<N; i++) {
if (v[i] ==1) {
x[i]++;
}
if (v[i] ==-1){
x[i]--;
}
float non = (float)Math.random();
if (non >< 0.000001) {on[i]=1;}
if ((x[i] == MirrorSeparation-Diameter + 20)&&(non <Rback)) {v[i]=-1;}
if ((x[i] == 40)&&(non><Routput)) {v[i]=1;};
if (x[i] == MirrorSeparation-Diameter + 500) {v[i] = 1; x[i]= 40; on[i]=0;}
}
}
try {
thread.sleep(1);
}
catch(InterruptedException e) {}
repaint();
}
}
public void paint(Graphics g) {
//Draw mirrors
g.fillRect(20,100,20,100);
g.fillRect(20 + MirrorSeparation,100,20,100);
//Draw photons
for(int i =0; i><N; i++) {
if(on[i] == 1)
g.fillOval(x[i],y[i],Diameter,Diameter);
}
}
hmmm... I apoligize if this excerpt is missing any vital information.
Let me know if anything isn't clear.
Thanks everybody for taking the time to help :)>
Crap, ya you're right.... :)
I forgot, I was trying something new this morning and I forgot to change it back before I posted it.
To do the loading images method, I used the same code that I just posted, with a couple of exceptions.
a.) At the beginning of the code I did this:
Image mirrorBack;
Image mirrorOutput, photon;
Toolkit toolkit = Toolkit.getDefaultToolkit();
public void init() {
mirrorBack = toolkit.getImage("mirror.jpg");
mirrorOutput = toolkit.getImage("mirror2.jpg");
photon = toolkit.getImage("photon.jpg");
}
b.) And in the paint method, instead of
g.fillRect(20,100,20,100);
g.fillRect(20 + MirrorSeparation,100,20,100);
and
g.fillOval(x[i],y[i],Diameter,Diameter);
I used:
g.drawImage(mirrorBack, 20,100, this);
g.drawImage(mirrorOutput, MirrorSeparation+20, 100, this);
And
g.drawImage(photon,x[i],y[i], this);
If it helps at all.... both methods (1. Loading and painting pre-drawn images to the canvas and 2. Drawing the shapes from scratch onto the canvas) were equally slow.
Sorry about the confusion :)