Przeanalizuj przykład
import javax.swing.*;
import java.awt.*;
public class AroundAyers extends JApplet implements Runnable
{
Image tlo;
Image animacja[]=new Image[4];
int index=0;
int pauza=100;
int obroty=5;
Thread animator;
//------------------------
public void init()
{
try
{
pauza=Integer.parseInt(getParameter("delay"));
obroty=Integer.parseInt(getParameter("rotation"));
}
catch (Exception e)
{
}
tlo=getImage(getCodeBase(),"images/ayers.jpg");
animacja[0]=getImage(getCodeBase(),"images/forward.gif");
animacja[1]=getImage(getCodeBase(),"images/down.gif");
animacja[2]=getImage(getCodeBase(),"images/back.gif");
animacja[3]=getImage(getCodeBase(),"images/up.gif");
}
//------------------------
// Wątek animacji.
public void start()
{
animator=new Thread(this);
animator.start();
}
//------------------------
public void stop()
{
animator=null;
}
//------------------------
public void run()
{
Thread ct=Thread.currentThread();
while (animator==ct)
{
try
{
Thread.sleep(pauza);
}
catch (InterruptedException e)
{
break;
}
synchronized (this)
{
index++;
if (index>88)
{
index=1;
obroty--;
}
if (obroty==0)
{
stop();
index=-1;
}
repaint();
}
}
}
//------------------------
public void update(Graphics g)
{
paint(g);
}
//------------------------
public void paint(Graphics g)
{
g.drawImage(tlo,0,0,this);
if (index>=1 && index<=30)
{
g.drawImage(animacja[0],15+(index-1)*24,12,this);
}
if (index>=31 && index<=44)
{
g.drawImage(animacja[1],15+(30-1)*24,12+(index-30)*24,this);
}
if (index>=45 && index<=74)
{
g.drawImage(animacja[2],711-(index-45)*24,372,this);
}
if (index>=75 && index<=88)
{
g.drawImage(animacja[3],15,348-(index-75)*24,this);
}
}
}
pozdrawiam