Thursday, 6 September 2012

Java Mouse


Introduction of Java Mouse Tutorial


Programm

Save MyWindowAdapter.java

import java.awt.event.*;
public class MyWindowAdapter extends WindowAdapter
{
	public void windowClosing(WindowEvent we)
	{
		System.exit(0);
	}
}


Save MyFrame.java 
 
import java.awt.*;
public class MyFrame extends Frame implements ActionListener, ItemListener, MouseListener, MouseMotionListener
{
	MenuBar menubar;
	Menu mnuShape, mnuColor;
	CheckboxMenuItem itmLine, itmBox, itmOval;
	CheckboxMenuItem itmRed, itmGreen, itmBlue, itmMagenta, itmYellow, itmSilver, itmBlack;
	MenuItem itmExit;
	String currentShape;
	Color color;
	
	ArrayList alShape;
	
	int x, y, x1, y1, x2, y2, width, height, oldX, oldY, oldWidth, oldHeight;
		
	MyFrame()
	{
		super("Mouse Demo");
		setSize(800,600);
		addWindowListener(new MyWindowAdapter());
		addMouseListener(this);
		addMouseMotionListener(this);
		setResizable(false);
		addMenu();
		setVisible(true);
		currentShape = "line";
		color = Color.BLACK;
		setCursor(Cursor.CROSSHAIR_CURSOR);
		alShape = new ArrayList();
	}
	
	private void addMenu()
	{
		int i;
		menubar = new MenuBar();
		
		mnuShape = new Menu("Shape");
		
		
		itmLine = new CheckboxMenuItem("Line",true);
		itmBox = new CheckboxMenuItem("Box",false);
		itmOval = new CheckboxMenuItem("Oval",false);
		
		itmLine.addItemListener(this);
		itmBox.addItemListener(this);
		itmOval.addItemListener(this);
		
		itmLine.setShortcut(new MenuShortcut(KeyEvent.VK_L));
		itmBox.setShortcut(new MenuShortcut(KeyEvent.VK_B));
		itmOval.setShortcut(new MenuShortcut(KeyEvent.VK_O));
		
		itmExit = new MenuItem("Exit",new MenuShortcut(KeyEvent.VK_X,true));
		itmExit.addActionListener(this);
		
		mnuShape.add(itmLine);
		mnuShape.add(itmBox);
		mnuShape.add(itmOval);
		mnuShape.addSeparator();
		mnuShape.add(itmExit);
		
		menubar.add(mnuShape);
		
		mnuColor = new Menu("Colour");
		
		itmRed = new CheckboxMenuItem("Red",false);
		itmGreen = new CheckboxMenuItem("Green",false);
		itmBlue = new CheckboxMenuItem("Blue",false);
		itmMagenta = new CheckboxMenuItem("Magenta",false);
		itmYellow = new CheckboxMenuItem("Yellow",false);
		itmSilver = new CheckboxMenuItem("Silver",false);
		itmBlack = new CheckboxMenuItem("Black",true);
		
		itmRed.addItemListener(this);
		itmGreen.addItemListener(this);
		itmBlue.addItemListener(this);
		itmMagenta.addItemListener(this);
		itmYellow.addItemListener(this);
		itmSilver.addItemListener(this);
		itmBlack.addItemListener(this);
		
		mnuColor.add(itmRed);
		mnuColor.add(itmGreen);
		mnuColor.add(itmBlue);
		mnuColor.add(itmYellow);
		mnuColor.add(itmMagenta);
		mnuColor.add(itmSilver);
		mnuColor.add(itmBlack);
		
		menubar.add(mnuColor);
		
		setMenuBar(menubar);
	}
	
	private void uncheckAllShapes()
	{
		itmLine.setState(false);
		itmBox.setState(false);
		itmOval.setState(false);
	}
	
	private void uncheckAllColours()
	{
		itmRed.setState(false);
		itmGreen.setState(false);
		itmBlue.setState(false);
		itmMagenta.setState(false);
		itmYellow.setState(false);
		itmSilver.setState(false);
		itmBlack.setState(false);
	}
	
	
	public void itemStateChanged(ItemEvent ie)
	{
		int state = ie.getStateChange();
		if (ie.getSource() == itmLine || ie.getSource() ==itmBox || ie.getSource() ==itmOval)
		{
			uncheckAllShapes();
			if (state == 2)
				currentShape = "";
			else
			{
				if (ie.getSource() == itmLine)			
				{
					itmLine.setState(true);
					currentShape = "line";
				}				
				else if (ie.getSource() == itmBox)
				{
					itmBox.setState(true);
					currentShape = "box";
				}
				else if (ie.getSource() == itmOval)
				{
					itmOval.setState(true);
					currentShape = "oval";
				}				
			}
			if (currentShape.length() == 0)
				setCursor(Cursor.DEFAULT_CURSOR);
			else
				setCursor(Cursor.CROSSHAIR_CURSOR);
		}
		else
		{
			uncheckAllColours();
			if (ie.getSource() == itmRed)
			{
				color = Color.RED;
				itmMagenta.setState(true);
			}
			else if (ie.getSource() == itmGreen)
			{
				color = Color.GREEN;
				itmGreen.setState(true);
			}
			else if (ie.getSource() == itmBlue)
			{
				color = Color.BLUE;
				itmBlue.setState(true);
			}
			else if (ie.getSource() == itmMagenta)
			{
				color = Color.MAGENTA;
				itmMagenta.setState(true);
			}
			else if (ie.getSource() == itmYellow)
			{
				color = Color.YELLOW;
				itmYellow.setState(true);
			}
			else if (ie.getSource() == itmSilver)
			{
				color = Color.GRAY;
				itmSilver.setState(true);
			}
			else if (ie.getSource() == itmBlack)
			{
				color = Color.BLACK;
				itmSilver.setState(true);
			}
			
		}
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		if (ae.getSource() == itmExit)
			System.exit(0);
	}
	
	//methods in MouseListener interface
	public void mouseClicked(MouseEvent me){}
	public void mouseEntered(MouseEvent me){/*setBackground(Color.RED);*/}
	public void mouseExited(MouseEvent me){/*setBackground(Color.WHITE);*/}
	public void mousePressed(MouseEvent me)
	{
		if (currentShape.equals("line"))
		{
			if (me.getButton() == MouseEvent.BUTTON1)
			{
				x1 = me.getX();
				y1 = me.getY();
				oldX = oldY = -10;
			}
		}
		else if(currentShape.equals("box") || currentShape.equals("oval"))
		{
			if (me.getButton() == MouseEvent.BUTTON1)
			{
				x1 = me.getX();
				y1 = me.getY();
				oldWidth = oldHeight = 0;
			}
		}

	}
	public void mouseReleased(MouseEvent me)
	{
		if (currentShape.equals("line"))
		{
			x2 = me.getX();
			y2 = me.getY();			
			MyLine ml = new MyLine(x1,y1,x2,y2,color);
			alShape.add(ml);
		}
		else if (currentShape.equals("box"))
		{
			MyBox mb = new MyBox(x,y,width,height,color);
			oldX = oldY = -10;
			oldWidth = oldHeight = 0;
			alShape.add(mb);
		}
		else if (currentShape.equals("oval"))
		{
			MyOval mo = new MyOval(x,y,width,height,color);
			oldX = oldY = -10;
			oldWidth = oldHeight = 0;
			alShape.add(mo);
		}
		repaint();
	}
	
	
	
	//methods in MouseMotionListener interface
	public void mouseDragged(MouseEvent me)
	{
		Graphics g;
		g = getGraphics();
		g.setColor(color);
		g.setXORMode(getBackground());
		
		if (currentShape.equals("line"))	
		{
			//first erase the old line
			if (oldX != -10 && oldY != -10)
				g.drawLine(x1,y1,oldX,oldY);
			
			//draw the new line
			x2 = me.getX();
			y2 = me.getY();
			g.drawLine(x1,y1,x2,y2);
			
			
			//store the current coordinates
			oldX = x2;
			oldY = y2;
		}
		else if (currentShape.equals("box") || currentShape.equals("oval"))
		{
			
			x2 = me.getX();
			y2 = me.getY();
			
			if (x1 <= x2)
			{
				width = x2 - x1;
				x = x1;
			}
			else
			{
				width = x1 - x2;
				x = x1 - width;
			}
			
			if (y1 <= y2)
			{
				height = y2 - y1;
				y = y1;
			}
			else
			{
				height = y1 - y2;
				y = y1 - height;
			}
			
			//first erase the old box
			if (currentShape.equals("box"))
			{
				if (oldWidth != 0 && oldHeight != 0)
					g.drawRect(oldX,oldY,oldWidth,oldHeight);
			}
			else 
			{
				if (oldWidth != 0 && oldHeight != 0)
					g.drawOval(oldX,oldY,oldWidth,oldHeight);
			}					
				
			//draw the new box
			if (currentShape.equals("box"))
				g.drawRect(x,y,width,height);
			else
				g.drawOval(x,y,width,height);
			
			//store the x, y, widht and height
			oldX = x;
			oldY = y;
			oldWidth = width;
			oldHeight = height;
				
		}
	}
	
	public void mouseMoved(MouseEvent me){}
	
	public void paint(Graphics g)
	{
		int i;
		for (i=0;i<alShape.size();i++)
		{
			MyShape ms = (MyShape)alShape.get(i);
			ms.draw(g);
		}
	}
}

Save MyShape.java

import java.awt.*;

abstract class MyShape
{
	private String shapeName;
	private Color color;
	
	MyShape(String shapeName){this.shapeName = shapeName;}
	
	void setShapeName(String shapeName){this.shapeName = shapeName;}
	
	String getShapeName(){return shapeName;}
	
	void setColor(Color color){this.color = color;}
	Color getColor(){return color;}
	
	abstract void draw(Graphics g);
}

Save MyOval.java

import java.awt.*;

class MyOval extends MyShape
{
	private int x, y, width, height;
		
	MyOval(int x, int y, int width, int height, Color color)
	{
		super("Oval");
		setColor(color);
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	void draw(Graphics g)
	{
		Color currentColor = g.getColor();
		g.setColor(getColor());
		g.drawOval(x,y,width,height);
		g.setColor(currentColor);
	}
}

Save MyLine.java

import java.awt.*;

class MyLine extends MyShape
{
	private int x1, y1, x2, y2;
	
	MyLine(int x1, int y1, int x2, int y2, Color color)
	{
		super("line");
		setColor(color);
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
	}
	
	void draw(Graphics g)
	{
		Color currentColor = g.getColor();
		g.setColor(getColor());
		g.drawLine(x1,y1,x2,y2);
		g.setColor(currentColor);
	}
}

Save MyBox.java

import java.awt.*;

class MyBox extends MyShape
{
	private int x, y, width, height;
		
	MyBox(int x, int y, int width, int height, Color color)
	{
		super("Box");
		setColor(color);
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	void draw(Graphics g)
	{
		Color currentColor = g.getColor();
		g.setColor(getColor());
		g.drawRect(x,y,width,height);
		g.setColor(currentColor);
	}
}

Save GUI1.java

public class GUI1
{
	public static void main(String args[])
	{
		MyFrame mf = new MyFrame();
	}
}

No comments:

Post a Comment