Select Menu

Combined Post

Mag Posts

Theme images by konradlew. Powered by Blogger.

Blogger templates

Blogger news

Facebook

Advertising

Blogroll

Popular Posts

Combined Posts 2

Mag Post 2

2 Column Post

Simple Post

Simple Post 2

New Carousel

Video Posts

» »Unlabelled » A Simple java mouse event applet!


SLVIKI 7:49 AM 0

This is a simple program of an mouse event java application. In this application the coordinates of the current mouse position is display "Mouse entered " when the mouse enter to the application window and display "Mouse Exits" when the mouse exits from the application window. and when the mouse clicked and released it displays as "Down " and "Up". and when the mouse dragged inside the window it displays "*" sign.




Here I used some mouse event handlers in this applet. You can see the code below.


// Demonstrate the mouse event handlers.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="MouseEvents" width=300 height=100>
</applet>
*/

public class MouseEvents extends Applet implements MouseListener, MouseMotionListener {
 
 String msg = "";
 int mouseX = 0, mouseY = 0; // coordinates of mouse

 public void init() {
  addMouseListener(this);
  addMouseMotionListener(this);
 }

 // Handle mouse clicked.
 public void mouseClicked(MouseEvent me) {
  mouseX = 0;
  mouseY = 10;
  msg = "Mouse clicked.";
  repaint();
 }

 // Handle mouse entered.
 public void mouseEntered(MouseEvent me) {
  mouseX = 0;
  mouseY = 10;
  msg = "Mouse entered.";
  repaint();
 }

 // handle mouse exited.
 public void mouseExited(MouseEvent me) {
  mouseX = 0;
  mouseY = 10;
  msg = "Mouse exited.";
  repaint();
 }

 // Handle button pressed.
 public void mousePressed(MouseEvent me) {
  // save coordinates
  mouseX = me.getX();
  mouseY = me.getY();
  msg = "Down";
  repaint();
 }

 // Handle button released.
 public void mouseReleased(MouseEvent me) {
  // save coordinates
  mouseX = me.getX();
  mouseY = me.getY();
  msg = "Up";
  repaint();
 }

 // Handle mouse dragged.
 public void mouseDragged(MouseEvent me) {
  // save coordinates
  mouseX = me.getX();
  mouseY = me.getY();
  msg = "*";
  showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
  repaint();
 }
 
 // Handle mouse moved.
 public void mouseMoved(MouseEvent me) {
  // show status 
  showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
 }

 // Display msg in applet window at current X,Y location.
 public void paint(Graphics g) {
  g.drawString(msg, mouseX, mouseY);
 }
}


Here is the output of the above program.


«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply