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.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative