0

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);
              }
         }
    }
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
kokodee
  • 285
  • 1
  • 3
  • 15
  • Should you be updating the properties of `panel5` and instead be updating the properties of the button? – MadProgrammer May 03 '16 at 01:35
  • That's my dilemma, how to update the properties of the panel. The panel was originally declared as JPanel panel5 = new JPanel(new FlowLayout()); However, that won't work, as with each click, the panel expands with randomly displayed JButtons on the screen and no layout manager will be of use to accommodate that. – kokodee May 03 '16 at 01:48
  • You're also defining the x/y position to basically always be the same, shouldn't you randomise those values in the `actionPerformed` method? You're also adding `n` buttons to a single panel, don't know if that's really what you want :P – MadProgrammer May 03 '16 at 01:52
  • You are absolutely right about the x/y position, i need to move those inside my actionPerformed method. And yes, my goal is to add n buttons to a single panel. The other option is to dynamically add a panel with each click of a button, but i think that will make the code excessively big. – kokodee May 03 '16 at 01:57
  • Instead of `setBounds()`, create a `GridLayout` of buttons, as shown [here](http://stackoverflow.com/a/7706684/230513), and change the _appearance_, as suggested [here](http://stackoverflow.com/q/7877576/230513). – trashgod May 03 '16 at 08:20
  • `so if the user repeatedly clicks it, it will duplicate ` repeatedly for how long or how much?, also how much time it will duplicate?, I think then you need timers, and click counters, and a global var to store the last Point, Note that the second click (after the button repositioned) is on the panel not the button. think again about your design, best of luck. – TiyebM May 03 '16 at 13:36

0 Answers0