public class vehicleViewer {
public static void main(String args[]) throws InterruptedException{
JFrame f = new JFrame();
vehicleComponent c = new carComponent();
vehicleComponent t = new truckComponent();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 400);
c.seats();
c.colors();
t.seats();
t.colors();
System.out.println("Randomly generating...");
////////////////////////////////////////// random number gen
Random dice = new Random();
int number;
for(int counter=1; counter<=20;counter++){
number = dice.nextInt(2);
Thread.sleep(1000);
if (number == 0){ //if show 0, draw a car
System.out.print("Car \n");
f.add(c); //show the car
f.remove(t); //remove the truck
}
else if (number == 1){ //if roll 1, draw a truck
System.out.print("Truck \n");
f.add(t); //show the truck
f.remove(c); //remove the car
}
f.setVisible(true);
Thread.sleep(1000);
}
}
}
I have random generator where if it prints 0 or 1, it'll draw a car (0) or a truck (1) on JFrame. After it swaps from one to the other for the first time, it would no longer swap anymore and it would just stay stuck on the second vehicle. The only time it would update is if I would change the screen dimensions manually while running it.