0
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.

Emyay
  • 21
  • 3
  • Would be nice to show the declaration / definition of `f` to make sure everyone knows which class we're talking about. Also, did you try to just read the documentation with all the methods, or even just show the list of the methods with intellisense and see if something fits your need? – Matteo NNZ Apr 26 '19 at 18:06
  • 1) Don't handle removal of JComponents yourself, use a [Card Layout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). – Frakcool Apr 26 '19 at 18:10
  • 1
    For [example](https://stackoverflow.com/questions/54042979/how-do-i-draw-on-a-jpanel-from-multiple-outside-classes/54043710#54043710), [example](https://stackoverflow.com/questions/36318078/add-multiple-choice-to-dialog-and-make-it-functioning/36321214#36321214), [example](https://stackoverflow.com/questions/34633999/how-to-add-action-to-a-button/34637817#34637817) and [example](https://stackoverflow.com/questions/49517392/how-to-view-another-jpanel-containing-many-sub-jpanels-from-same-jframe-in-netbe/49519023#49519023) – Frakcool Apr 26 '19 at 18:10
  • 1
    2) Don't use `Thread.sleep(...)` use a [Swing Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) instead, for [example](https://stackoverflow.com/questions/41877789/animated-sprites-with-java-swing/41879444#41879444) and [example](https://stackoverflow.com/questions/42404270/jcomponent-stops-getting-rendered-once-it-goes-off-the-screen/42404808#42404808) otherwise your app will be frozen for as long as `Thread.sleep(...)` runs. Or use a Swing Worker – Frakcool Apr 26 '19 at 18:13
  • 1
    Please don't use the "back button" of your browser to edit your question, as you're breaking the changes made by others – Frakcool Apr 26 '19 at 18:15
  • 3) For your particular case, I would suggest links 1 and 3 in my second comment, those are the ones that are the most similar to your problem. Also, don't manually set the `JFrame`'s size, add a `JPanel` to it, and override its `getPreferredSize()` method instead. Then call `.pack()` method on the `JFrame`. 4) Make your variables descriptive (not single letter ones), so that we know what they are without too much trouble – Frakcool Apr 26 '19 at 18:16
  • oops, sorry! I'm new. Thanks for the help, too. – Emyay Apr 26 '19 at 18:16
  • 1
    If nobody else answers this before, I'll do so after I get home today (~5 hours). We don't know what `carComponent` nor `truckComponent` or even `vehicleComponent` are, please [edit] your question so that it becomes a [mcve] (Check the "example" links to know what I mean, it should be short, but still needs to reproduce your issue, and we should be able to copy-paste-compile without modification). When you do so, reply back (@Frakcool (The `@` is important)), if it's good, I'll upvote the question for the effort. If you have questions on how to do it, ask and I'll be happy to clarify. – Frakcool Apr 26 '19 at 18:24

0 Answers0