class Painter extends Thread{
private Vector listeners = new Vector();
private int w=-1;
public void run(){
if (w==-1){
w=i;
}
while (true){
System.out.println(w);
xCr[w]=(rnd.nextInt(300));
yCr[w]=(rnd.nextInt(300));
System.out.println(xCr[w]+" "+ yCr[w]);
this.fireDotDrawn(350);
repaint();
try {
sleep(1000);
} catch(InterruptedException e) {}
}
}
public synchronized void addDotDrawnListener(DotDrawnListener ddl) {
listeners.addElement(ddl);
}
public synchronized void removeDotDrawnListener(DotDrawnListener ddl) {
listeners.removeElement(ddl);
}
protected void fireDotDrawn(int Dot) {
Vector l;
DotDrawnEvent dde = new DotDrawnEvent(this, Dot);
synchronized (this) {
l = (Vector) listeners.clone();}
// Оповестим подписчиков о наступлении события
for (int i = 0; i < l.size(); i++) {
((DotDrawnListener)l.elementAt(i)).DotDrawn(dde);
}
}
}
class Butreac implements ActionListener{
public void actionPerformed(ActionEvent ae) {
i++;
yCr = new int[i+1];
xCr = new int[i+1];
thDot = new Painter[i+1];
thDot[i] = new Painter();
thDot[i].addDotDrawnListener(mylis);
thDot[i].start();
}
}
interface DotDrawnListener extends java.util.EventListener {
void DotDrawn(DotDrawnEvent dde);
}
public class DotDrawnEvent extends java.util.EventObject {
protected transient int DotCode;
DotDrawnEvent(java.lang.Object source, int Dot) {
super(source);
DotCode = Dot;
}
public int getDotDrawn() {
return DotCode;
}
}
public class MyListener implements DotDrawnListener {
public void DotDrawn(DotDrawnEvent dde) {
//Вот тут хочу писать dde.Dot, но такого варианта нет в списке.
System.out.println("Good!");
}
}