8

Java Program to write an Applet with menu bar containing Shape, Color item and S...

 2 years ago
source link: https://sqlwithmanoj.com/2009/01/28/java-program-to-write-an-applet-with-menu-bar-containing-shape-color-item-and-sub-menu-q56/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Home > Java > Java Program to write an Applet with menu bar containing Shape, Color item and Sub menu – Q56

Java Program to write an Applet with menu bar containing Shape, Color item and Sub menu – Q56

Q56: Java Program to write an applet, which has a menu bar containing Shape, Color item. The Shape menu contains sub-menu containing Line, rectangle, Circle, Ellipse & the Color menu contains sub-menu Red, Green, Blue, Pink. On sellecting the appropriate menu item by the user it should be reflected to the Applet on the screen.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class MenuFrame extends Frame implements ActionListener {
String col;
int shape;
public MenuFrame(String title) {
super(title);
MenuBar mbar=new MenuBar();
setMenuBar(mbar);
Menu shape=new Menu("shape");
MenuItem item1,item2,item3,item4;
shape.add(item1 =new MenuItem("Line"));
shape.add(item2 =new MenuItem("Rectangle"));
shape.add(item3 =new MenuItem("Circle"));
shape.add(item4 =new MenuItem("Ellipse"));
mbar.add(shape);
Menu color=new Menu("color");
MenuItem item5,item6,item7,item8;
color.add(item5= new MenuItem("red"));
color.add(item6= new MenuItem("green"));
color.add(item7= new MenuItem("blue"));
color.add(item8= new MenuItem("pink"));
mbar.add(color);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
item6.addActionListener(this);
item7.addActionListener(this);
item8.addActionListener(this);
}
public void paint(Graphics g){
if (col.equals("red")){
g.setColor(Color.red);
}
else if(col.equals("green")){
g.setColor(Color.green);
}
else if(col.equals("blue")){
g.setColor(Color.blue);
}
else if(col.equals("pink")){
g.setColor(Color.pink);
}
switch(shape){
case 1:
g.drawLine(100,100,200,200);       
break;
case 2 :
g.drawRect(10,10,60,50);
break;
case 3:
g.drawOval(10,10,100,100);
break;     
case 4:
g.drawOval(10,10,100,50);
break;     
}
}
public void actionPerformed(ActionEvent ae){
String msg="You selected";
String arg=(String)ae.getActionCommand();
if(arg.equals("red")){
col="red";
}
else if(arg.equals("blue")){
col="blue";
}
else if(arg.equals("green")){
col="green";
}
else if (arg.equals("pink")){
col="pink";
}        
repaint();
if(arg.equals("Line"))
shape=1;
else if(arg.equals("Rectangle"))
shape=2;
else if(arg.equals("Circle"))
shape=3;
else if(arg.equals("Ellipse"))
shape=4;
repaint();
}
}
public class menushape extends Applet  {
MenuFrame mf;
public void init() {
MenuFrame mf=new MenuFrame("my frame");
mf.setVisible(true);
mf.setSize(200,200);
}
}

… from College notes (BCA/MCA assignments):


Related


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK