title: Randomly Moving a object using swing Component
body: <p>I have created a square using swing components that moves right, left, up and down when clicked by buttons assigned to them. Now I am trying to move this object in a random fashion when clicked. This is my code.</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  @SuppressWarnings("serial")
  public class MovingObjects extends JFrame 
      implements ActionListener {
  
  //    GUI code omitted here...
  
      @SuppressWarnings("unused")
      private JButton button, leftButton,rightButton,upButton,downButton;
      private JPanel panel,redPanel;
      int x =10;
      int y=10;       
  
      public static void main (String[] args) {
          MovingObjects frame = new MovingObjects();
          frame.setSize(400, 300);
          frame.createGUI();
          frame.show();
          frame.getContentPane().setBackground(Color.blue);
  
      }
  
      private void createGUI() {
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          Container window = getContentPane();
          window.setLayout(new FlowLayout() );
  
          panel = new JPanel();
          panel.setPreferredSize(new Dimension(300, 200));
          panel.setBackground(Color.black);
          window.add(panel);
  
          redPanel = new JPanel();
          redPanel.setBackground(Color.red);
          redPanel.setLocation(x, y);
          redPanel.setSize(50, 50);
          panel.add(redPanel);
  
  
          leftButton = new JButton("Move Left");
          leftButton.addActionListener(this);
          window.add(leftButton);
  
          rightButton = new JButton("Move Right");
          rightButton.addActionListener(this);
          window.add(rightButton);
  
          upButton = new JButton("Move Up");
          upButton.setLocation(260, 60);
          upButton.setSize(100, 30);
          upButton.addActionListener(this);
          window.add(upButton);
  
  
          downButton = new JButton("Move Down");
          downButton.addActionListener(this);
          window.add(downButton);
  
      }
  
  
      public void actionPerformed(ActionEvent event) {
  
          //Move down
           if (event.getSource() == downButton)
              {
               if (y &lt; 180){
               y=y+20;     
               redPanel.setLocation(x, y);
  
               }
              }
           //Move Up
               if (event.getSource() == upButton)  
               {
              if (y &gt; 10){
               y=y-20;     
               redPanel.setLocation(x, y);
              }
               }
           //Move Left
               if (event.getSource() == leftButton)
               {
               if (x &gt; 10){
               x=x-20;     
               redPanel.setLocation(x, y);
                }
               }
           //Move Right
               if (event.getSource() == rightButton)
               {
               if (x &lt; 280){
               x=x+20;
               redPanel.setLocation(x, y);
                  }
               }
  
      }
  
  
      }
  </code></pre>