Search code examples
javaclonejpanelundo

Exact Copy of a JPanel or Undo.. suggestions


So in my program I have a JPanel ( called squareContainer ) which has JPanels inside of it which act as bubbles in a Bubble Breaker game. Anyway, the idea is that I want the computer to simulate removing bubbles to find out which one to remove is best, but when this happens it actually plays the game and doesn't only do the 'best' move. So i was wondering if it possible to copy the exact state of my squareContainer JPanel including what bubbles are in it etc.. so that when the computer simulates playing it and basically does all the moves, I can simply replace it with the clone so I have my initial state back, as well as the information of which is the best move.

Note that I already have the information of which is the best move, but can't figure out the best way to undo the editing of my JPanels ... any ideas please?


Solution

  • The best thing you can do is separate your data from your UI.

    Your UI should be rendering JPanels based on some internal state of the bubbles still available.

    In theory, you would be able to view the state of this in a text format, say with System.out.println(...). In this manner, you will be able to copy that state and send it to your simulator.