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]
# 1
How much RAM and CPU does the user have?
morgalr at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
is thier ways to do it yes for sure check out the top linkfor the award winers in the java nerds section the downloads i found inspireingas for how they do it you would have to look into it http://www.cfxweb.net/articles/javapps/applets.shtml
xlightwavex at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
Post the relevant parts of your code so we can make suggestions of improving it...
jsalonen at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

> How much RAM and CPU does the user have?

I pretty much want it to be able to run at a decent speed on any range of CPU's as this applet is intended to be a visual aid, with some user ineractivity, for students taking an optics course. It does not have to be really fast.... but I just need it to be fast enough to be functional.

I'm assuming it's not realistic to expect it to be lightning fast on a 486, for instance, but right now it's barely working even on a 900MHz.

lucretia6 at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5
> Post the relevant parts of your code so we can make> suggestions of improving it...
jsalonen at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6
yup:)I'm on it.It's just taking me awhile cause the computers in our lab are having problems today.
lucretia6 at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 7

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

lucretia6 at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 8
Didn't you say you were loading and rendering *images* and dragging them? Where are they?
jsalonen at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 9

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

lucretia6 at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 10
1) take the repaint call outside the loop in mouseDragged or remove it. You repaint() in already run.2) make a longer sleep time. 50 or 20 milliseconds should be OK.those should give some speed-ups.
jsalonen at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 11
It worked!Thanks a lot :)
lucretia6 at 2007-6-29 18:36:19 > top of Java-index,Archived Forums,New To Java Technology Archive...