Threads - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Threads

stricksstricks Posts: 33Member
Hi,

Ok so I have two classes. Class1 and Class2.

Class 2 contains

[code]
public class Class2 implements Runnable {

int min = 0;
int max = 300;
int x = 0;
int y = 0;

public void run(){
System.out.println("run() (Class2)");
moveBall();
}

public void moveBall(){
System.out.println("moveBall()");

int x_cord = (int) (Math.random() * (max - min + 1) ) + min;
int y_cord = (int) (Math.random() * (max - min + 1) ) + min;

if((x < x_cord && y < y_cord)){
while((x < x_cord && y < y_cord)){

if(x != max){
x++;
}
if(y != max){
y++;
}
System.out.println("("+x+","+y+")");
try {
Thread.sleep(Class1.DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}

}

[/code]

Class1 Contains

[code]
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class Class1 extends Applet implements Runnable{

Thread class2, class1;
Image[] images = new Image[10];
Class2 updateXY = new Class2();
static final int DELAY
= 100;

public void init(){
System.out.println("init()");

images[0] = getImage(getDocumentBase(), "Pictures/imageBlue.gif");
resize(300,300);
}

public void run(){
System.out.println("run() (Class1)");
repaint();
}

public void start(){
System.out.println("start()");
class2 = new Thread(new Class2());
class1= new Thread(new Class1());
class1.start();
class2.start();

}

public void paint(Graphics g){
System.out.println("paint()");
g.drawRect(updateXY.x, updateXY.y, 15, 15);

}

}

[/code]

All that happens is that the Applet stays in the While loop code. What I want to do is switch between that and the run() in Class1 so that it updates the image everytime the x/y values update.

Any help?

thanks
Sign In or Register to comment.