title: Draw Randomly-Sized Shape from User JList Selection of Shapes in Java GUI
body: <p>I have to produce a randomly-sized shape (from 50 to 300 pixels) in a java GUI. There is a JList of 3 shapes (rectangle, square, circle) that the user can choose from, and when they choose one, a randomly-sized rectangle, square, or circle should appear in the GUI. </p>
  
  <p>I guess I'm just having trouble figuring out where and how to implement the list selection listeners. </p>
  
  <p>Here is my code so far: </p>
  
  <pre><code>import javax.swing.*;
  import javax.swing.event.ListSelectionEvent;
  import javax.swing.event.ListSelectionListener;
  
  import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Graphics;
  
  public class ShapeSelectionWindow extends JPanel implements ListSelectionListener{
  
      public void paintComponent(Graphics g) {
          int x, y, width, height;
          super.paintComponent(g);
  
          width = (int)Math.floor(Math.random()*250) + 50;
          height = (int)Math.floor(Math.random()*250) + 50;
          x = (int)Math.floor((615 - width) / 2);
          y = (int)Math.floor((661 - height) / 2);
  
          g.fillRect(x, y, width, height);
      }
  
      public static void main(String[] args) {
          ShapeSelectionWindow ssw = new ShapeSelectionWindow();
          JFrame jf = new JFrame();
          JPanel shapeListPanel = new JPanel();
          shapeListPanel.setBackground(Color.WHITE);
          DefaultListModel&lt;String&gt; dlm = new DefaultListModel&lt;String&gt;();
          dlm.addElement("Rectangle");
          dlm.addElement("Square");
          dlm.addElement("Circle");
          JList&lt;String&gt; shapeList = new JList&lt;String&gt;(dlm);
          shapeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          shapeListPanel.add(shapeList);
  
          jf.getContentPane().setLayout(new BorderLayout());
          jf.getContentPane().add(ssw, BorderLayout.CENTER);
          jf.getContentPane().add(shapeListPanel, BorderLayout.EAST);
          jf.setTitle("Simple Drawing GUI");
          jf.setSize(700, 700);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          jf.setVisible(true);
      }
  
      @Override
      public void valueChanged(ListSelectionEvent e) {
          // TODO Auto-generated method stub
  
      }
  }
  </code></pre>
  
  <p>The current paintComponent method is for painting a rectangle. Like I said, I'm not sure how to implement the list selection listeners for this project in order to produce different shapes based on the user's selection.</p>