I am creating a Swing Game GUI, which contains multiple panels that are part of a frame. Each panel contains a number of ImageIcon JButtons. When a JButton is clicked it will randomly position itself on the screen. For this particular panel i am not using a certain LayoutManager(understand the criticism here, but also the frame is not resizable as it fits the entire screen) and I use the randomness of setBounds(x, y, 100, 100) for this image JButton to appear on a random spot inside that panel. My only trouble is to figure it out, how to save the previous position of a certain JButton, so if the user repeatedly clicks it, it will duplicate and will appear on the screen multiple times on a random position, leaving all others displayed.
class ButtonListener implements ActionListener
{
Random randomCoord = new Random();
//random location in on the screen 30-150 x 200-400
int x = randomCoord.nextInt(150-30) + 30;
int y = randomCoord.nextInt(400-200) + 200;
int butnCOUNT = 0;
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == button1)
{
butnCOUNT ++;
JButton[] addButtons = new JButton[butnCount];
for (int i = 0; i < addButtons.length; i++)
{
addButtons[i] = butnCloud; // ImageIcon JButton
addButtons[i].addActionListener(new ButtonListener ());
panel5.add(addButtons[i]);
panel5.setVisible(true);
panel5.setOpaque(false);
panel5.setBounds(x, y, 20, 20);
}
}
}