Con il double buffering si elimina lo sfarfallio
Codice Html
<APPLET CODE="Animazione_2.class" WIDTH="200" HEIGHT="200">...</APPLET>
Codice Java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
public class Animazione_2 extends Applet
implements ActionListener
{
final int ms=100;
Timer t;
final int nFrame=12;
int frame;
Image iFrame[];
final int dx=200;
final int dy=200;
Graphics gOFF;
Image iOFF;
public void init()
{
iOFF=createImage(dx, dy);
gOFF=iOFF.getGraphics();
iFrame=new Image[nFrame];
for(int i=0; i<nFrame; i++)
iFrame[i]=getImage(getCodeBase(), "PT_anim"+i+".gif");
frame=0;
t=new Timer(ms, this);
t.start();
}
public void paint(Graphics g)
{
gOFF.drawImage(iFrame[frame], 0, 0, this);
g.drawImage(iOFF, 0, 0, this);
}
public void actionPerformed(ActionEvent e)
{
frame++;
if(frame==nFrame)
frame=0;
repaint();
}
public void update(Graphics g)
{
paint(g);
}
}
Osserva
-
Image, Graphics, createImage(), getGraphics()
update(), paint()
|