import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DrawShapesApplication extends JFrame
{
	
	 public JButton choices[];
     public String names[] = { "Line", "Rectangle", "Oval" };
     public JPanel buttonPanel;
     public DrawPanel drawingArea;
     public int width = 500, height = 500;
     
     
     //constructor
     
     public DrawShapesApplication()
     {
     	
     	setTitle("Draw Shapes Application");
     	setSize(width,height);
     	setLocation(100,200);
     drawingArea = new DrawPanel( width, height );
      choices = new JButton[ names.length ];
      buttonPanel = new JPanel();
      buttonPanel.setLayout(
         new GridLayout( 1, choices.length ) );
      ButtonHandler handler = new ButtonHandler();

      for ( int i = 0; i < choices.length; i++ ) {
         choices[ i ] = new JButton( names[ i ] );
         buttonPanel.add( choices[ i ] );
         choices[ i ].addActionListener( handler );
      }

      Container c = getContentPane();
      c.add( buttonPanel, BorderLayout.NORTH );
      c.add( drawingArea, BorderLayout.CENTER );
     	
     	
     }
        
   
    
    private class ButtonHandler implements ActionListener
      {
      public void actionPerformed( ActionEvent e )
      {
         for ( int i = 0; i < choices.length; i++ )
            if ( e.getSource() == choices[ i ] ) {
               drawingArea.setCurrentChoice( i );
               break;
            }
      }
   }

    
    
     
     
     public static void main(String [] args)

     {
     	DrawShapesApplication w = new DrawShapesApplication();
        w.setVisible(true);
     	
     }
     
          	
	
}

 



class DrawPanel extends JPanel
 {
   private int currentChoice = -1;  // don't draw first time
   private int width = 100, height = 100;

   public DrawPanel( int w, int h )
   {
      width = ( w >= 0 ? w : 100 );
      height = ( h >= 0 ? h : 100 );
   }
   public void paintComponent( Graphics g )
   {
      super.paintComponent( g );
      switch( currentChoice ) {
         case 0:
            g.drawLine( randomX(), randomY(),randomX(), randomY() );
            break;
         case 1:
            g.drawRect( randomX(), randomY(),randomX(), randomY() );
            break;
         case 2:
            g.drawOval( randomX(), randomY(),randomX(), randomY() );
            break;
      }
   }
   public void setCurrentChoice( int c )
   {
      currentChoice = c;
      repaint();
   }
   private int randomX()
      { return (int) ( Math.random() * width ); }
   private int randomY()
      { return (int) ( Math.random() * height ); }
}


