overriding paint() problems
843807Oct 11 2005 — edited Oct 11 2005Hello, I wonder if anyone could help me with the following problem.
I'm writing a program which tries to simulate balls bouncing around a JPanel. I managed to get the program working correctly, however, I have tried to thread the program - by making each ball a seperate thread. Doing this however, my overwridden paint method is then no longer called within the run() method - even when calling repaint(). I have tried calling paint(g) directly (which i believe is bad coding), which does invoke the overwridden paint method, however, the ball does bot seem to appear on the panel! I've provided my code. If anyone is kind enough to help, it would be greatly appreciated!
public class ballInstance extends JPanel implements Runnable {
Thread newBallThread;
String Direction;
Graphics g;
int startseed, directionseed;
int m = 0;
int PointOneX, PointOneY, initballdirection;
public void paint(Graphics g) {
super.paint(g);
g.fillOval(PointOneX, PointOneY, 10, 10); //Prints Blue
try {
newBallThread.sleep(50);
}
catch (InterruptedException e) {
System.out.println("Thread inturrupted.");
}
}
public ballInstance(Graphics g, int startseed, int directionseed) {
this.g = g;
this.startseed = startseed;
this.directionseed = directionseed;
newBallThread = new Thread(this, "Ball Instance");
newBallThread.start();
}
public void run() {
Random ballstart = new Random(startseed);
Random direction = new Random(directionseed);
PointOneX = ballstart.nextInt(284);
PointOneY = ballstart.nextInt(284);
if (PointOneX < 56) PointOneX = 56;
if (PointOneY < 56) PointOneY = 56;
initballdirection = direction.nextInt(4);
if (initballdirection == 0) Direction = "NORTHEAST";
if (initballdirection == 1) Direction = "NORTHWEST";
if (initballdirection == 2) Direction = "SOUTHEAST";
if (initballdirection == 3) Direction = "SOUTHWEST";
System.out.println(Direction + " " + PointOneX + " " + PointOneY);
while(m < 20) {
if (Direction == "NORTHEAST") {
System.out.println("NORTHEAST");
g.setColor (Color.blue);
repaint();
g.setColor (Color.white);
repaint();
PointOneX = PointOneX + 1;
PointOneY = PointOneY - 1;
if (PointOneX == 285 && PointOneY == 55) Direction = "SOUTHWEST";
if (PointOneX == 285) Direction = "NORTHWEST";
if (PointOneY == 55) Direction = "SOUTHEAST";
}
if (Direction == "SOUTHEAST") {
System.out.println("SOUTHEAST");
g.setColor (Color.blue);
repaint();
g.setColor (Color.white);
repaint();
PointOneX = PointOneX + 1;
PointOneY = PointOneY + 1;
if (PointOneX == 285 && PointOneY == 285) Direction = "NORTHWEST";
if (PointOneX == 285) Direction = "SOUTHWEST";
if (PointOneY == 285) Direction = "NORTHEAST";
}
if (Direction == "SOUTHWEST") {
System.out.println("SOUTHWEST");
g.setColor (Color.blue);
repaint();
g.setColor (Color.white);
repaint();
PointOneX = PointOneX - 1;
PointOneY = PointOneY + 1;
if (PointOneX == 5 && PointOneY == 285) Direction = "NORTHEAST";
if (PointOneX == 5) Direction = "SOUTHEAST";
if (PointOneY == 285) Direction = "NORTHWEST";
}
if (Direction == "NORTHWEST") {
System.out.println("NORTHWEST");
g.setColor (Color.blue);
repaint();
g.setColor (Color.white);
repaint();
PointOneX = PointOneX - 1;
PointOneY = PointOneY - 1;
if (PointOneX == 5 && PointOneY == 55) Direction = "SOUTHEAST";
if (PointOneX == 5) Direction = "NORTHEAST";
if (PointOneY == 55) Direction = "SOUTHWEST";
}
m++;
}
}
}