Program to use MouseListener interface and its methods.


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

public class MouseExercise1 extends Applet implements MouseListener {
 
 int keyCode;
 
 public void init() {
  
  addMouseListener(this);
 }
 
 public void paint(Graphics g) {
  
  if(keyCode == 1) {
   
   g.drawRect(10, 10, 80, 80);
   g.drawString("Mouse left key pressed", 10, 140);
  }
  
  if(keyCode == 2) {
   
   g.drawOval(10, 10, 80, 80);
   g.drawString("Mouse right key pressed", 10, 140);
  }
 }
 
 public void mouseClicked(MouseEvent me) {
  
  if(me.getButton() == MouseEvent.BUTTON1) {
   // Left key clicked
   keyCode = 1;
  }
  
  if(me.getButton() == MouseEvent.BUTTON3) {
   // Right key clicked
   keyCode = 2;
  }
  
  repaint();
 }
 
 public void mouseEntered(MouseEvent me) { }
 
 public void mouseExited(MouseEvent me) { }
 
 public void mousePressed(MouseEvent me) { }
 
 public void mouseReleased(MouseEvent me) { }
}

/*  <applet code="MouseExercise1.class" height="300" width="300">
 </applet>
*/

Video Link : https://www.youtube.com/watch?v=uQdajBV-FIo


Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

First Android App | Android Development | Java

Program to design an applet which draws a circle (having color BLUE) inside a triangle (having color YELLOW)