import java.awt.*; import java.applet.*; import java.util.*; class Position extends Observable { private int x; private int y; private int ancx; private int ancy; private int mv; private int[][] mouvements = new int[1001][2]; public void incx() { inc("x"); } public void incy() { inc("y"); } public int ancx() { return ancx; } public int ancy() { return ancy; } public int x() { return x ; } public int y() { return y ; } public int[][] mouvements() { return (this.mouvements); } public int nbMouvements() { return (mv); } Position () { x=0; y=0; ancx=x; ancy=y; mv=0; mouvements[0][0]=x; mouvements[0][1]=y; } synchronized private void inc(String lettre) { ancx=x ; ancy=y ; if (lettre=="x") x++; else y++; maj(lettre) ; } private void maj(String lettre) { // System.out.println (mv+ " ("+ancx+","+ancy+")-->("+x+","+y+") par "+lettre) ; mv++; mouvements[mv][0]=x; mouvements[mv][1]=y; setChanged(); notifyObservers(); } } class testThread extends Thread { private String nom ; private Position pos ; public testThread (String nom, Position pos) { this.nom=nom ; this.pos=pos ; } public void run() { if (nom=="x") { for (int i=0;i<500;) { if ( (long)(Math.random() * 10) <1 ) try { sleep((long)(Math.random() * 100)); } catch (InterruptedException e) {} pos.incx(); i++; } } else { for (int i=0;i<500;) { try { sleep((long)(Math.random() * 10)); } catch (InterruptedException e) {} pos.incy(); i++; } } } } public class TestLine extends Applet implements Observer { private Position position; private Thread un; private Thread deux; private int[][] mouvements; int x ; int y ; int xx ; int yy ; public void init() { x=0 ; position = new Position() ; mouvements = position.mouvements() ; position.addObserver(this) ; un = new testThread("x", position) ; deux = new testThread("y", position) ; un.start() ; deux.start() ; } public void update(Observable o, Object arg) { Position pos = (Position)o ; x=pos.ancx() ; y=pos.ancy() ; xx=pos.x() ; yy=pos.y() ; repaint() ; } public void paint (Graphics G) { for (int i=1;i