Important alert: (current site time 7/15/2013 3:32:53 PM EDT)
 

VB icon

Event handlig with Menu

Email
Submitted on: 10/15/2006 6:57:47 AM
By: Vikrant Kumar Upadhyay 
Level: Beginner
User Rating: By 1 Users
Compatibility: Java (JDK 1.4), Java (JDK 1.5)
Views: 6269
author picture
(About the author)
 
     with the help of this code JAVA menu is easy for beginers.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Event handlig with Menu
// Description:with the help of this code JAVA menu is easy for beginers.
// By: Vikrant Kumar Upadhyay
//
// Inputs:no
//
// Returns:no
//
// Assumes:no
//
// Side Effects:no
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5433&lngWId=2//for details.//**************************************

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 public class event6 extends Frame implements MouseListener
{
MenuBar mb;
Menu m1,m2;
MenuItem mi1,mi2,mi3;
CheckboxMenuItem cmi1,cmi2,cmi3;
int dr;
Color c;
int x1,x2,y1,y2;
public event6()
{
	super("VIKRANT MCA-5");
	mb=new MenuBar();
	m1=new Menu("SHAPE");
	m2=new Menu("COLOR");
	mi1=new MenuItem("RED");
	mi2=new MenuItem("GREEN");
	mi3=new MenuItem("BLUE");
	cmi1=new CheckboxMenuItem("Line");
	cmi2=new CheckboxMenuItem("RECT");
	cmi3=new CheckboxMenuItem("Oval");
	mb.add(m1);
	mb.add(m2);
	m1.add(cmi1);
	m1.add(cmi2);
	m1.add(cmi3);
	m2.add(mi1);
	m2.add(mi2);
	m2.add(mi3);
 setMenuBar(mb);
	myl1 o1=new myl1();
	myl2 o2=new myl2();
	cmi1.addItemListener(o1);
	cmi2.addItemListener(o1);
	cmi3.addItemListener(o1);
	mi1.addActionListener(o2);
	mi2.addActionListener(o2);
	mi3.addActionListener(o2);
	addMouseListener(this);
	setSize(400,400);
	setVisible(true);
 dr=0;
	c=Color.GREEN;
}
public class myl1 implements ItemListener
{
	public void itemStateChanged(ItemEvent e)
	{
		if(e.getSource()==cmi1)
 			dr=1;
		if(e.getSource()==cmi2)
 			dr=2;
		if(e.getSource()==cmi3)
 			dr=3;
 	}
}
 class myl2 implements ActionListener
 {
 	public void actionPerformed(ActionEvent e)
 	{
 		if(e.getSource()==mi1)
 	{
			c=Color.RED;
		}
 		if(e.getSource()==mi2)
 		c=Color.GREEN;
 		if(e.getSource()==mi3)
 		c=Color.BLUE;
 }
 }
 public void mousePressed(MouseEvent e)
 {
 if(e.getButton()==1)
 {
 x1=e.getX();
 y1=e.getY();
 }
 }
 public void mouseReleased(MouseEvent e)
 {
 if(e.getButton()==1)
 {
x2=e.getX();
y2=e.getY();
repaint();
 }
 }
 public void mouseClicked(MouseEvent e)
 {
 }
 public void mouseExited(MouseEvent e)
 {
 }
 public void mouseEntered(MouseEvent e)
 {
 }
 public void update(Graphics g)
 {
paint(g);
 }
 public void paint(Graphics g)
 {
 	g.setColor(c);
 	switch(dr)
 	{
 		case 1:
 		 {
 		g.drawLine(x1,y1,x2,y2);
			 break;
			 }
 		case 2:
 		{
 		g.drawRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
			 break;
			}
 		case 3:
 		{
 		g.drawOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x1-x2),Math.abs(y1-y2));
			}
}
 }
 public static void main(String args[])
 {
	 new event6();
 }
 }


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.