Java Mini Application

/*
Develop a mini project for any application using Java concepts
*/

 

// Swing Menu Component
// Graphics
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
 
class SwingMenu implements ActionListener
{
     JFrame jf;
     JMenuBar mb;
     JMenu m1, m2, m3;
     JMenuItem f1, f2, s1,s2,s3;
     JRadioButtonMenuItem r1, r2, r3;
    
     SwingMenu()
     {
          jf = new JFrame();
         
          jf.setSize(500,500);
          jf.setTitle(“Swing Menu”);
             
          mb = new JMenuBar();
         
          m1 = new JMenu(“File”);
          m2 = new JMenu(“Shape”);
          m3 = new JMenu(“Color”);
         
          f1 = new JMenuItem(“New”, KeyEvent.VK_N);
          f2 = new JMenuItem(“Exit”,KeyEvent.VK_X );
         
          s1 = new JMenuItem(“Line”,KeyEvent.VK_L);
          s2 = new JMenuItem(“Rect”,KeyEvent.VK_R);
          s3 = new JMenuItem(“Circle”,KeyEvent.VK_C);
         
         f1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_DOWN_MASK));
                  
         f2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_DOWN_MASK));
         
         s1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,InputEvent.CTRL_DOWN_MASK));
         
         s2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_DOWN_MASK));
         
         s3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_DOWN_MASK));
         
          r1 = new JRadioButtonMenuItem(“Red”,true);
          r2 = new JRadioButtonMenuItem(“Green”);
          r3 = new JRadioButtonMenuItem(“Blue”);
         
          ButtonGroup bg = new ButtonGroup();
          bg.add(r1);
          bg.add(r2);
          bg.add(r3);
         
          m1.add(f1);
          m1.add(f2);
         
          m2.add(s1);
          m2.add(s2);
          m2.add(s3);
                  
          m3.add(r1);
          m3.add(r2);
          m3.add(r3);
         
          m2.addSeparator();
          m2.add(m3);
         
          mb.add(m1);
          mb.add(m2);
         
          f1.addActionListener(this);
          f2.addActionListener(this);
          s1.addActionListener(this);
          s2.addActionListener(this);
          s3.addActionListener(this);
          r1.addActionListener(this);
          r2.addActionListener(this);
          r3.addActionListener(this);
         
          jf.setJMenuBar(mb);
         
          jf.setVisible(true);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
    
     public void actionPerformed(ActionEvent ae)
     {
          Graphics g = jf.getGraphics();
          String cmd = ae.getActionCommand();
          Color c1 = Color.black;
                  
          if(r1.isSelected())
              c1 = Color.red;
          else if(r2.isSelected())
              c1 = Color.green;
          else if(r3.isSelected())
              c1 = Color.blue;
                                          
          if(cmd.equals(“New”))
          {
              jf.getContentPane().invalidate();
              jf.getContentPane().validate();
              jf.getContentPane().repaint();
          }
          if(cmd.equals(“Exit”))
              System.exit(0);
         
         
          int x1 = ((int)(Math.random()*1000))%250;
          int y1 = ((int)(Math.random()*1000))%250;
    
          if(cmd.equals(“Line”))
          {  
              int x2 = ((int)(Math.random()*1000))%500;
              int y2 = ((int)(Math.random()*1000))%500;
              g.setColor(c1);
              g.drawLine(x1,y1,x2,y2);
          }
          if(cmd.equals(“Rect”))
          {  
              int w = ((int)(Math.random()*1000))%200;
              int h = (int)(w/2);
              g.setColor(c1);
              g.drawRect(x1,y1,w,h);
          }
          if(cmd.equals(“Circle”))
          {  
              int w = ((int)(Math.random()*200));
              g.setColor(c1);
              g.drawOval(x1,y1,w,w);
          }
     }
    
     public static void main(String as[])
     {
          new SwingMenu();
     }
}