title: How to Create a JPanel as the main class
body: <p>Here is the Subclass which extends from the Jpanel. At the moment I don't have a main driver program which is the Jpanel. May you help me create one?</p>
  
  <p>Here is the subclass of the program that shows a pair of dice that are rolled when the user clicks the applet.</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  /**
   * Shows a pair of dice that are rolled when the user clicks on the
   * applet.  It is assumed that the panel is 100-by-100 pixels.
   */
  public class DicePanel extends JPanel {
  
      private int die1 = 4;  // The values shown on the dice.
      private int die2 = 3;
  
      /**
       *  The constructor adds a mouse listener to the panel.  The listener
       *  will roll the dice when the user clicks the panel.  Also, the
       *  background color and the preferred size of the panel are set.
       */
      public DicePanel() {
         setPreferredSize( new Dimension(100,100) );
         setBackground( new Color(200,200,255) );  // light blue
         addMouseListener( new MouseAdapter() {
             public void mousePressed(MouseEvent evt) {
                 roll();
             }
         });
      }
  
      /**
       * Draw a die with upper left corner at (x,y).  The die is
       * 35 by 35 pixels in size.  The val parameter gives the
       * value showing on the die (that is, the number of dots).
       */
      private void drawDie(Graphics g, int val, int x, int y) {
         g.setColor(Color.white);
         g.fillRect(x, y, 35, 35);
         g.setColor(Color.black);
         g.drawRect(x, y, 34, 34);
         if (val &gt; 1)  // upper left dot
            g.fillOval(x+3, y+3, 9, 9);
         if (val &gt; 3)  // upper right dot
            g.fillOval(x+23, y+3, 9, 9);
         if (val == 6) // middle left dot
            g.fillOval(x+3, y+13, 9, 9);
         if (val % 2 == 1) // middle dot (for odd-numbered val's)
            g.fillOval(x+13, y+13, 9, 9);
         if (val == 6) // middle right dot
            g.fillOval(x+23, y+13, 9, 9);
         if (val &gt; 3)  // bottom left dot
            g.fillOval(x+3, y+23, 9, 9);
         if (val &gt; 1)  // bottom right dot
            g.fillOval(x+23, y+23, 9,9);
      }
  
  
      /**
       * Roll the dice by randomizing their values.  Tell the
       * system to repaint the applet, to show the new values.
       */
      void roll() {
         die1 = (int)(Math.random()*6) + 1;
         die2 = (int)(Math.random()*6) + 1;
         repaint();
      }
  
  
      /**
       * The paintComponent method just draws the two dice and draws
       * a one-pixel wide blue border around the panel.
       */
      public void paintComponent(Graphics g) {
         super.paintComponent(g);  // fill with background color.
         g.setColor( Color.BLUE );
         g.drawRect(0,0,99,99);
         g.drawRect(1,1,97,97);
         drawDie(g, die1, 10, 10);
         drawDie(g, die2, 55, 55);
      }
  
  } // end class DicePanel
  </code></pre>