M
Mike Crenshaw
hi
I've made an applet doing an animation. The problem is that it runs great on
some computers and really slow on others. Even fast computers.. (2ghz and
up)..
here is what i do:
private Image background; //544 * 425
private Image object; //66 * 26
private Image doubleBufferImage; //544 * 425
private Graphics doubleBufferGraphics;
private int ox; //new position to draw object
private int oy; //same
....
public void paint(Graphics g) {
doubleBufferGraphics.setClip(0,0,background.getWidth(this),
background.getHeight(this));
doubleBufferGraphics.drawImage(background,0,0,this);
doubleBufferGraphics.setClip(ox, oy, object.getWidth(this),
object.getHeight(this));
doubleBufferGraphics.drawImage(object, ox, oy, this);
g.drawImage(doubleBufferImage, 0, 0, this);
}
any ideas?
heres the whole thing
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
public class Test extends Applet implements Runnable, KeyListener{
private Image background;
private Image object;
private Image doubleBufferImage;
private Graphics doubleBufferGraphics;
private MediaTracker tracker;
private int ox;
private int oy;
private Thread t;
public void init() {
tracker = new MediaTracker(this);
background = getImage (getCodeBase (), "background.gif");
object = getImage (getCodeBase (), "object.gif");
tracker.addImage(background,0);
tracker.addImage(object,1);
addKeyListener(this);
try
{
tracker.waitForAll();
}
catch(Exception e){System.out.println("error "+e);}
doubleBufferImage = createImage (background.getWidth(this),
background.getHeight(this));
doubleBufferGraphics= doubleBufferImage.getGraphics ();
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e)
{
t = new Thread(this);
t.start();
}
public void run()
{
for (int i = 0; i < 25; i++)
{
ox +=20;
oy +=15;
paint(getGraphics());
}
ox=0;
oy=0;
}
public synchronized void paint(Graphics g) {
doubleBufferGraphics.setClip(0,0,background.getWidth(this),
background.getHeight(this));
doubleBufferGraphics.drawImage(background,0,0,this);
doubleBufferGraphics.setClip(ox, oy, object.getWidth(this),
object.getHeight(this));
doubleBufferGraphics.drawImage(object, ox, oy, this);
g.drawImage(doubleBufferImage, 0, 0, this);
}}
I've made an applet doing an animation. The problem is that it runs great on
some computers and really slow on others. Even fast computers.. (2ghz and
up)..
here is what i do:
private Image background; //544 * 425
private Image object; //66 * 26
private Image doubleBufferImage; //544 * 425
private Graphics doubleBufferGraphics;
private int ox; //new position to draw object
private int oy; //same
....
public void paint(Graphics g) {
doubleBufferGraphics.setClip(0,0,background.getWidth(this),
background.getHeight(this));
doubleBufferGraphics.drawImage(background,0,0,this);
doubleBufferGraphics.setClip(ox, oy, object.getWidth(this),
object.getHeight(this));
doubleBufferGraphics.drawImage(object, ox, oy, this);
g.drawImage(doubleBufferImage, 0, 0, this);
}
any ideas?
heres the whole thing
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
public class Test extends Applet implements Runnable, KeyListener{
private Image background;
private Image object;
private Image doubleBufferImage;
private Graphics doubleBufferGraphics;
private MediaTracker tracker;
private int ox;
private int oy;
private Thread t;
public void init() {
tracker = new MediaTracker(this);
background = getImage (getCodeBase (), "background.gif");
object = getImage (getCodeBase (), "object.gif");
tracker.addImage(background,0);
tracker.addImage(object,1);
addKeyListener(this);
try
{
tracker.waitForAll();
}
catch(Exception e){System.out.println("error "+e);}
doubleBufferImage = createImage (background.getWidth(this),
background.getHeight(this));
doubleBufferGraphics= doubleBufferImage.getGraphics ();
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e)
{
t = new Thread(this);
t.start();
}
public void run()
{
for (int i = 0; i < 25; i++)
{
ox +=20;
oy +=15;
paint(getGraphics());
}
ox=0;
oy=0;
}
public synchronized void paint(Graphics g) {
doubleBufferGraphics.setClip(0,0,background.getWidth(this),
background.getHeight(this));
doubleBufferGraphics.drawImage(background,0,0,this);
doubleBufferGraphics.setClip(ox, oy, object.getWidth(this),
object.getHeight(this));
doubleBufferGraphics.drawImage(object, ox, oy, this);
g.drawImage(doubleBufferImage, 0, 0, this);
}}