• Tidak ada hasil yang ditemukan

Programming in Java

N/A
N/A
Protected

Academic year: 2024

Membagikan "Programming in Java"

Copied!
10
0
0

Teks penuh

(1)

Programming in Java

CBCS BHCS03

By Parul Chachra

(2)

Chapter 24

Event Handling

Reference: Book prescribed as per

the syllabus

(3)

Using DelegaFon Event Model

•   Step 1 : Implement the appropriate interface in the listener so that it will receive the type of

event desired

•   Step 2 : Implement code to register and unregister (if necessary) the listener as a recipient for the event noFficaFons

•   NOTE1 : A source may generate several types of events. Each event must be registered separately

•   NOTE2 : an object may register to receive several types of events, but it must implement all of the interfaces that are required to receive these

events

(4)

Handling Mouse Events

•  To handle mouse events, you must implement the MouseListener and the MouseMo4onListener interfaces

•  The following applet displays the current coordinates of the mouse in the applet’s status window

•  Each Fme a buTon is pressed, the word “Down” is displayed at the locaFon of the mouse pointer. Each Fme the buTon is released, the word “Up” is shown. If a buTon is clicked, the message “Mouse

clicked” is displayed in the upper- leX corner of the applet display area.

•  As the mouse enters or exits the applet window, a message is displayed in the upper-leX corner of the applet display area.

•  When dragging the mouse, a * is shown, which tracks with the mouse pointer as it is dragged.

•  NoFce that the two variables, mouseX and mouseY, store the

locaFon of the mouse when a mouse pressed, released, or dragged event occurs.

•  The coordinates are then used by paint( ) to display output at the point of these occurrences.

(5)

Handling Mouse Events

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);

addouseMotionListener(this);

}

// Handle mouse clicked.

public void mouseClicked(MouseEvent me) { // save coordinates

mouseX = 0;

mouseY = 10;

msg = "Mouse clicked.";

repaint();

}

(6)

Handling Mouse Events

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 {

•  MouseEvents class extends Applet and implements both the MouseListener and MouseMo4onListener interfaces

•  These two interfaces contain methods that receive and process the various types of mouse events

•  The applet is both the source and the listener for these events

•  Component class, which supplies the addMouseListener( ) and addMouseMo4onListener( ) methods, is a superclass of Applet

(7)

Handling Mouse Events

String msg = "";

int mouseX = 0, mouseY = 0; // coordinates of mouse public void init() {

addMouseListener(this);

addouseMotionListener(this);

}

•  Inside init( ), the applet registers itself as a listener for mouse events.

•  This is done by using addMouseListener( ) and addMouseMo4onListener( ), which, as menFoned, are members of Component

•  They are shown here:

void addMouseListener(MouseListener ml)

void addMouseMoFonListener(MouseMoFonListener mml)

•  Here, ml is a reference to the object receiving mouse events, and mml is a reference to the object receiving mouse moFon events

•  In this program, the same object is used for both

•  In the following slides, the applet then implements all of the methods defined by the MouseListener and MouseMo4onListener interfaces

•  These are the event handlers for the various mouse events. Each method handles its event and then returns

(8)

Handling Mouse Events

// Handle mouse entered.

public void mouseEntered(MouseEvent me) { // save coordinates

mouseX = 0;

mouseY = 10;

msg = "Mouse entered.";

repaint();

}

// Handle mouse exited.

public void mouseExited(MouseEvent me) { // save coordinates

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();

}

(9)

Handling Mouse Events

// 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);

} }

(10)

Handling Mouse Events

•  Sample Output of the Applet:

Referensi

Dokumen terkait

The type of the event is specified by type, and the component that has been added to or removed from the container is comp • You can obtain a reference to the container that generated

A Simple Swing ApplicaEon • Swing programs differ from both the console-based programs and the AWT-based programs, for example, – They use a different set of components and a different

The MVC Architecture • By separaEng a component into a model, a view, and a controller, the specific implementaEon of each can be changed without affecEng the other two • For

AdjustmentEvent Class • There is an integer constant, ADJUSTMENT_VALUE_CHANGED, that indicates that a change has occurred • Here is one AdjustmentEvent constructor:

Lightweight Containers • Lightweight containers do inherit JComponent • An example of a lightweight container is JPanel, which is a general-purpose container • Lightweight

H Computer Science, Semester II Lab Exercises BHCS03 – Programming in JAVA Attempt the following questions as part of JAVA Lab class today: 1.. Write a program with which you can

• “Object” means a particular item that belongs to a class – Also called an “instance” • Example String s1 = "Hello"; – Here, String is the class, and the variable s1 and the value

Transitiveness – Example Here is an example, which demonstrates the transitive property of inheritance: TransitivenesExample.cs public class TransitivityExample { static void