codice:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Engine extends Applet implements Runnable {
Graphics bufferGraphics;
Bot[] bots = new Bot[2];
Image offscreen;
public static final int SLEEP_DELAY = 80;
Dimension dim;
int curX, curY;
int x = 0;
Thread t1 = new Thread(this);
public void init() {
this.setSize(400, 400);
dim = getSize();
setBackground(Color.WHITE);
offscreen = createImage(dim.width, dim.height);
bufferGraphics = offscreen.getGraphics();
t1.start();
}
public void start() {
bots[0] = new Bot(getWidth()/2, getHeight()/2, 50, 50, getImage("images/beebot.gif"));
bots[1] = new Bot(getWidth()/3, getHeight()/3, 50, 50, getImage("images/beebot.gif"));
}
public void paint(Graphics g) {
bufferGraphics.clearRect(0, 0, dim.width, dim.width);
g.drawImage(offscreen, 0, 0, this);
for(int i=0; i<bots.length; i++) {
bots[i].draw(g);
}
}
public void update(Graphics g) {
paint(g);
}
@Override
public void run() {
while(true) {
long startProcess = System.currentTimeMillis();
//b1.setX(b1.getX() + 1);
repaint();
try {
Thread.sleep(SLEEP_DELAY-(System.currentTimeMillis()-startProcess));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public Image getImage(String image) {
return Toolkit.getDefaultToolkit().getImage(image);
}
}
bots[i].draw(g);
ora tralasciando magari la struttura che a qualcuno piace ad altri no, quando viene chiamato il metodo draw (che disegna l'immagine) le immagini successive alla prima "lampeggiano".
la classe draw è compresa in ogni istanza bot che non metto ma che semplicemente definisce i punti nell'asse x e y e l'altezza e la larghezza dell'immagine:
codice:
bots[0] = new Bot(puntosull'assex, puntosull'assey, altezza, larghezza, getImage(immagine));