Space for google add

Create an Applet which displays a message in the center of the screen.Use mouse and keyboard events.

 


 Create an Applet which displays a message in the center of the screen. The message indicates the events taking place on the applet window. Handle events like mouse click, mouse moved, mouse dragged, mouse pressed, and key pressed. The message should update each time an event occurs. The message should give details of the event such as which mouse button was pressed, which key is pressed etc. (Hint: Use repaint(), KeyListener, MouseListener, MouseEvent method getButton, KeyEvent methods getKeyChar)


Ex6seta1.java 

/*

<applet code="mouse.class" width=500 height=400>

</applet>

*/

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import javax.swing.*;



public class mouse extends Applet implements MouseMotionListener,MouseListener,KeyListener

{

String msg="";

int x,y;

public void init()

{

addMouseListener(this);

addMouseMotionListener(this);

addKeyListener(this);

}


public void paint(Graphics g)

{

g.drawString(msg+" "+x+","+y,150,200);

}


public void mousePressed(MouseEvent me)

{

msg="Mouse Pressed: X ="+me.getX() + "Y = "+me.getY();

repaint();

}


public void mouseClicked(MouseEvent me)

{

msg="Mouse Clicked: ";

if(me.getButton()==1)

msg="Left Button";

if(me.getButton()==3)

msg="Right Button";

//msg="Mouse clicked: X ="+me.getX() + "Y ="+me.getY();

repaint();

}


public void mouseReleased(MouseEvent me)

{

msg="Mouse Released: X = "+me.getX() + "Y = " +me.getY();

repaint();

}


public void mouseExited(MouseEvent me)

{

msg="Mouse Exited: X = "+me.getX() + "Y = " +me.getY();

repaint();

}


public void mouseEntered(MouseEvent me)

{

msg="Mouse Entered: X = "+me.getX() + "Y = " +me.getY();

repaint();

}


public void mouseMoved(MouseEvent me)

{

msg="Mouse Moved: X = "+me.getX() + "Y = " +me.getY();

repaint();

}


public void mouseDragged(MouseEvent me)

{

msg="Mouse Dragged: X ="+me.getX() + "Y = "+me.getY();

repaint();

}


public void keyPressed(KeyEvent ke)

{

msg="Key Pressed: "+ke.getKeyChar();

repaint();

}


public void keyTyped(KeyEvent ke)

{

//msg="Key Typed: "+ke.getKeyChar();

//repaint();

}


public void keyReleased(KeyEvent ke)

{

msg="Key Released: "+ke.getKeyChar();

repaint();

}

}


Post a Comment

0 Comments