hej
Jeg har lavet en applet som laver en animation.. Problemet er at den kører
fint på nogle computere of meget langsomt på andre. Selv hurtige computere..
(2ghz og på)..
her er hvad jeg gør
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);
}
nogle ideer?
her er hele test programmet
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);
}}
|