Witam!
Chciałbym zrobić aplet, który wyświetli animację po kliknięciu w jego obszarze. Sama animacja działa dobrze, jeśli wprowadzę ją w głównym wątku. Ale jesli próbuje wywołać ją z osobnego wątku, to jest już problem.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class Animacja extends Applet implements Runnable
{
int ktory;
final int ile=5;
Image obrazek[] = new Image[ile];
int i;
public Thread thread;
public void init()
{
thread = null;
}
public void start()
{
if (thread == null)
{
thread = new Thread(this);
thread.start();
}
}
public void stop()
{
if (thread != null)
{
thread = null;
}
}
public void run()
{
for(int i=0; i<ile;i++)
{
String nazwa = i+".jpg";
obrazek[i] = getImage (getDocumentBase(), nazwa); //pobra
}
ktory=0;
for(i=0; i<5; i++)
{
ktory++;
repaint();
try {
Thread.sleep(100);
} catch(InterruptedException e){}
if (ktory == 4) ktory =0;
if (i == 4) i =0;
}
}
public void paint (Graphics gDC)
{
gDC.drawString("Ktory: ", 10, 210);
gDC.drawImage (obrazek[ktory], 10, 10, 100, 100, this);
gDC.drawString("Ktory: "+ ktory, 10, 210);
}
}
public
class obrazyc extends Applet implements MouseListener{
private int x,y;
String str;
int i;
public void init()
{
addMouseListener(this);
Animacja anim = new Animacja();
Thread an = new Thread(anim);
}
public void paint(Graphics gDC)
{
an.start();
}
public void mouseClicked(MouseEvent evt)
{
}
public void mousePressed(MouseEvent evt) {}
public void mouseExited(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {}
public void mouseReleased(MouseEvent evt) {}
}
Z góry dziękuję za wskazówki i pomoc.
Pozdrawiam