title: Fill in a shape with color in java
body: <p>I want to be able to make a program with three shapes: a circle, a square and a triangle. It needs to have buttons with name of the colors. If I click the red button for example and then click the circle, then the circle will turn into red.</p>
  
  <p>My code is based on something I found that randomizes the color of the shapes with every click. I managed do a layout on the buttons but they won't appear with the shapes. Also the triangle isn't there. The actionlisteners to the buttons are commented out because I couldn't get them to work and I didn't want to erase them.</p>
  
  <p>I'm very new to shapes and I can't get the thinking down. Thanks in advance!</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.awt.geom.*;
  import java.util.List;
  import java.util.*;
  import javax.swing.*;
  
  public class DemoShapes {
         public static final Color DEFAULT_COLOR = Color.BLACK;
  
  JFrame frame = new JFrame("Shapes");
  Container contentPane;
  JButton Red = new JButton("Red");
  JButton Orange = new JButton("Orange");
  JButton Yellow = new JButton ("Yellow");
  JButton Green = new JButton ("Green");
  JButton Blue = new JButton ("Blue");
  JButton Pink = new JButton ("Pink");
  JButton White = new JButton ("White");
  JButton Gray = new JButton ("Gray");
  JButton Black = new JButton ("Black");
  JButton Back = new JButton ("Back to Menu");
  JPanel panel1 = new JPanel();
  
  public DemoShapes() {
  
  
      ArrayList&lt;ShapeItem&gt; shapes = new ArrayList&lt;ShapeItem&gt;();
  
      shapes.add(new ShapeItem(new Ellipse2D.Double(1, 1, 100, 100),
              DEFAULT_COLOR));
      shapes.add(new ShapeItem(new Rectangle2D.Double(110, 1, 100, 100),
              DEFAULT_COLOR));
  Polygon triangle = new Polygon();
  
  contentPane = frame.getContentPane();
  
  
  Red.setBounds (150, 380, 110, 30);
  Orange.setBounds (290, 380, 110, 30);
  Yellow.setBounds (430, 380, 110, 30);
  Green.setBounds (150, 430, 110, 30);
  Blue.setBounds (290, 430, 110, 30);
  Pink.setBounds (430, 430, 110, 30);
  White.setBounds (150, 480, 110, 30);
  Gray.setBounds (290, 480, 110, 30);
  Black.setBounds (430, 480, 110, 30);
  Back.setBounds (5, 5, 170, 30);
  
  
  panel1.add(Red);
  //Red.addActionListener(this);
  panel1.add(Orange);
  //Orange.addActionListener(this);
  panel1.add(Yellow);
  //Yellow.addActionListener(this);
  panel1.add(Green);
  //Green.addActionListener(this);
  panel1.add(Blue);
  //Blue.addActionListener(this);
  panel1.add(Pink);
  //Pink.addActionListener(this);
  panel1.add(White);
  //White.addActionListener(this);
  panel1.add(Gray);
  //Gray.addActionListener(this);
  panel1.add(Black);
  //Black.addActionListener(this);
  panel1.add(Back);
  //Back.addActionListener(this);
  
      ShapesPanel panel = new ShapesPanel(shapes);
  panel1.setLayout(null);
      frame.add(panel);
  //frame.add(panel1);    
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(700,600);
  frame.setResizable(false);
      frame.setVisible(true);
  }
  
  class ShapeItem {
      private Shape shape;
      private Color color;
  
      public ShapeItem(Shape shape, Color color) {
          super();
          this.shape = shape;
          this.color = color;
      }
  
      public Shape getShape() {
          return shape;
      }
  
      public void setShape(Shape shape) {
          this.shape = shape;
      }
  
      public Color getColor() {
          return color;
      }
  
      public void setColor(Color color) {
          this.color = color;
      }
  }
  
  
  class ShapesPanel extends JPanel {
      private List&lt;ShapeItem&gt; shapes;
      private Random rand = new Random();
  
      public ShapesPanel(List&lt;ShapeItem&gt; shapesList) {
          this.shapes = shapesList;
  
          addMouseListener(new MouseAdapter() {
  
              public void mouseClicked(MouseEvent e) {
  
                  Color color = getRandomColor();
                  for (ShapeItem item : shapes) {
                      if (item.getShape().contains(e.getPoint())) {
                          item.setColor(color);
                      }
                  }
                  repaint();
              }
          });
      }
  
  
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
  
          Graphics2D g2 = (Graphics2D) g.create();
  
          for (ShapeItem item : shapes) {
              g2.setColor(item.getColor());
              g2.fill(item.getShape());
          }
  
          g2.dispose();
      }
  
  
      public Dimension getPreferredSize() {
          return new Dimension(300, 300);
      }
  
      private Color getRandomColor() {
          return new Color(rand.nextFloat(), rand.nextFloat(),
              rand.nextFloat());
      }
  }
  
      public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
       public void run() {
                  new DemoShapes();
              }
          });
      }
  }
  </code></pre>