title: Adding a shape to a JPanel
body: <p>I have a simple <code>JFrame</code> and I have added a <code>JPanel</code> inside of this frame. How do I go about adding a shape: I.E. a colored square to that panel which I have given a variable name of <code>gamePanel</code>?</p>
  
  <p>Here is the code. Literally nothing in there yet however I want to add squares in the panel. Creating a block collapsing game.</p>
  
  <pre><code>package blockmanagertest;
  
  import java.awt.Color;
  import java.awt.Graphics;
  
  public class BlockGUI extends javax.swing.JFrame {
  
      /**
       * Creates new form BlockGUI
       */
      public BlockGUI() {
          initComponents();
  
  
      }
  
      /**
       * This method is called from within the constructor to initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is always
       * regenerated by the Form Editor.
       */
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
      private void initComponents() {
  
          gamePanel = new javax.swing.JPanel();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          gamePanel.setBackground(new java.awt.Color(38, 38, 38));
  
          javax.swing.GroupLayout gamePanelLayout = new javax.swing.GroupLayout(gamePanel);
          gamePanel.setLayout(gamePanelLayout);
          gamePanelLayout.setHorizontalGroup(
              gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 562, Short.MAX_VALUE)
          );
          gamePanelLayout.setVerticalGroup(
              gamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 738, Short.MAX_VALUE)
          );
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addContainerGap()
                  .addComponent(gamePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addContainerGap(175, Short.MAX_VALUE))
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addContainerGap()
                  .addComponent(gamePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .addContainerGap())
          );
  
          pack();
      }// &lt;/editor-fold&gt;                        
  
      /**
       * @param args the command line arguments
       */
      public static void main(String args[]) {
          /* Set the Nimbus look and feel */
          //&lt;editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "&gt;
          /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
           * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
           */
          try {
              for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                  if ("Nimbus".equals(info.getName())) {
                      javax.swing.UIManager.setLookAndFeel(info.getClassName());
                      break;
                  }
              }
          } catch (ClassNotFoundException ex) {
              java.util.logging.Logger.getLogger(BlockGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex) {
              java.util.logging.Logger.getLogger(BlockGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex) {
              java.util.logging.Logger.getLogger(BlockGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex) {
              java.util.logging.Logger.getLogger(BlockGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
          //&lt;/editor-fold&gt;
  
          /* Create and display the form */
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new BlockGUI().setVisible(true);
              }
          });
      }
  
      // Variables declaration - do not modify                     
      private javax.swing.JPanel gamePanel;
      // End of variables declaration                   
  
  
     public void paintComponent(Graphics g) {
         super.paintComponents(g);
  
         g.setColor(Color.red);
         g.fillRect(150, 150, 150, 150);
  
     }
  
  
  
  
  }
  </code></pre>