0% found this document useful (0 votes)
44 views9 pages

Menus Using Frames: Source Code

The document describes a Java program that creates a menu bar with options to draw different shapes and colors. The program uses menus to select a shape type and fill color, which are then drawn to the screen. When a menu item is selected, an action event calls the paint method, which draws the selected shape in the chosen color. The program demonstrates using menus and events to trigger graphics drawing in Java.

Uploaded by

Mehala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views9 pages

Menus Using Frames: Source Code

The document describes a Java program that creates a menu bar with options to draw different shapes and colors. The program uses menus to select a shape type and fill color, which are then drawn to the screen. When a menu item is selected, an action event calls the paint method, which draws the selected shape in the chosen color. The program demonstrates using menus and events to trigger graphics drawing in Java.

Uploaded by

Mehala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MENUS USING FRAMES

SOURCE CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class menu extends Frame implements ActionListener
{
String msg="";
int x=0,x1=0;
Menu sh=new Menu("Shapes");
Menu f=new Menu("Fill color");
Menu c=new Menu("Color");
Menu ex=new Menu("Exit");

MenuItem cr=new MenuItem("Circle");


MenuItem s=new MenuItem("Square");
MenuItem l=new MenuItem("Line");
MenuItem o=new MenuItem("Oval");
MenuItem r=new MenuItem("Rectangle");

MenuItem fc=new MenuItem("fCircle");


MenuItem fs=new MenuItem("fSquare");
MenuItem fo=new MenuItem("foval");
MenuItem fr=new MenuItem("fRectangle");

MenuItem R=new MenuItem("Red");


MenuItem b=new MenuItem("Blue");
MenuItem m=new MenuItem("green");
MenuItem p=new MenuItem("Pink");

MenuItem e=new MenuItem("Exit");

public menu()
{
MenuBar mb=new MenuBar();
setMenuBar(mb);

sh.add(cr);
sh.add(s);
sh.add(l);
sh.add(r);

f.add(fc);
f.add(fs);
f.add(fr);

c.add(R);
c.add(b);
c.add(m);
c.add(p);

ex.add(e);
mb.add(sh);
mb.add(f);
mb.add(c);
mb.add(ex);

cr.addActionListener(this);
s.addActionListener(this);
l.addActionListener(this);
r.addActionListener(this);

fc.addActionListener(this);
fs.addActionListener(this);
fr.addActionListener(this);

R.addActionListener(this);
b.addActionListener(this);
m.addActionListener(this);
p.addActionListener(this);

e.addActionListener(this);

}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Circle"))
x=1;
if(e.getActionCommand().equals("Square"))
x=2;
if(e.getActionCommand().equals("line"))
x=3;
if(e.getActionCommand().equals("oval"))
x=4;
if(e.getActionCommand().equals("Rectangle"))
x=5;
if(e.getActionCommand().equals("fCircle"))
x=6;
if(e.getActionCommand().equals("fSquare"))
x=7;
if(e.getActionCommand().equals("foval"))
x=8;
if(e.getActionCommand().equals("fRectangle"))
x=9;
if(e.getActionCommand().equals("Red"))
x1=10;
if(e.getActionCommand().equals("Blue"))
x1=11;
if(e.getActionCommand().equals("green"))
x1=12;
if(e.getActionCommand().equals("Pink"))
x1=13;

if(e.getActionCommand().equals("Exit"))
{
System.exit(0);
}
repaint();

}
class WindowClose extends WindowAdapter
{
public void WindowClose(WindowEvent e)
{
System.exit(0);
}
}
public void paint(Graphics g)
{
if(x1==10)
g.setColor(Color.red);
if(x1==11)
g.setColor(Color.blue);
if(x1==12)
g.setColor(Color.green);
if(x1==13)
g.setColor(Color.pink);

if(x==1)
g.drawOval(50,100,120,120);
if(x==2)
g.drawRect(100,100,150,150);
if(x==3)
g.drawLine(30,100,200,100);
if(x==4)
g.drawOval(150,200,100,50);
if(x==5)
g.drawRect(200,300,100,60);

if(x==6)
g.fillOval(50,100,120,120);
if(x==7)
g.fillRect(100,100,150,150);
if(x==8)
g.fillOval(150,200,100,50);
if(x==9)
g.fillRect(200,300,100,60);
}
}
class menucolor
{
public static void main(String args[])
{
Frame f=new menu();
f.setSize(5000,5000);
f.setTitle("menu");
f.setVisible(true);
}
}
OUTPUT:

You might also like