Archive

Archive for January 28, 2009

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

January 28, 2009 Leave a comment

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):


Categories: Java Tags:

Java Program to write an Applet with Check Boxes handling and display output – Q55

January 28, 2009 Leave a comment

Q55: Java Program to write an applet, which shows a two set of check boxes. The first set is independent and second set belongs to a checkbox group (like radio buttons). Whichever check box is selected by the user, it should be displayed on the screen.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

abstract class Ap_CheckboxGroupDemo extends Applet implements ItemListener{
 	String msg = "";
 	Checkbox Win98, WinNT;
 	CheckboxGroup cbg;
 	Button Win;
 	MyCheckbox myCheckbox1,myCheckbox2,myCheckbox3;

 	public void init(){
 		Win98 = new Checkbox("Windows 98/Xp");
 		WinNT = new Checkbox("Windows NT/2000");
 		
 		add(Win98);
 		add(WinNT);
 		
 		Win98.addItemListener(this);
 		WinNT.addItemListener(this);
 		
 		cbg = new CheckboxGroup();
 		myCheckbox1 = new MyCheckbox("Item 1",cbg,true);
 		add(myCheckbox1);
 		
 		myCheckbox2 = new MyCheckbox("Item 2",cbg,false);
 		add(myCheckbox2);
 		
 		myCheckbox3 = new MyCheckbox("Item 3",cbg,false);
 		add(myCheckbox3);
	}
	
	public void paint(Graphics g){
		msg = "current state: ";
 		g.drawString(msg,6,80);
 		msg = "Windows 98/Xp:" + Win98.getState();
 		g.drawString(msg,6,100);
 		msg = "Windows NT/2000:" + WinNT.getState();
 	}
 }
 		
 


class MyCheckbox extends Checkbox{
 	
 	public MyCheckbox(String label,CheckboxGroup cbg,boolean flag){
 		super(label,cbg,flag);
 		enableEvents(AWTEvent.ITEM_EVENT_MASK);
	}
	
	protected void processItemEvent(ItemEvent ie){
		showStatus("checkbox name/state: " + getLabel() + 
 					"/" + getState());
 		super.processItemEvent(ie);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags: