Event listening mechanism in Java

Three aspects of mouse event monitoring mechanism:

1. Event Source Object:

The event source object is the object that can produce actions.All container and element components in the Java language are event source objects in event listening.Java distinguishes different event source objects based on the action of the event. On which component the action occurs, the component is the event source object

2. Event monitoring methods:

addMouseListener(MouseListener ml); this method is mainly used to capture mouse release, press, click, enter and leave actions; after the corresponding actions are captured, the event processing class (which implements the MouseListener interface) handles them.

addActionListener(ActionListener al); captures mouse clicks on button-like components or keyboard carriage returns on input box components; and then handles the actions and related information by the appropriate methods of event handling classes (which implement the ActionListener interface).

addMouseMotionListener(MouseMotionListener mml); captures mouse movements and drag actions; and then processes the actions and related information by the appropriate methods of the event processing class (which implements the MouseMotionListener interface).

addKeyListener(KeyListener kl); captures press, release, and tap actions of keyboard keys on the event source object; and then handles the actions and related information by communicating the corresponding methods of the event processing class (which implements the KeyListener interface).

3. Event interfaces (event processing classes, that is, classes that implement event interfaces):

MouseListener mouse event interface, event handling methods for press, release, click, enter and leave

ActionListener Action Event Interface, event handling method corresponding to action

MouseMotionListener Mouse Move Event Interface, Event Handling with Move and Drag

KeyLisetener keyboard event interface, event handling methods for release, press and tap

Once you understand these three aspects of event monitoring, it's easy to add event monitoring to your components.Here is a small example:

Objective: To create a simple drawing board that can press the appropriate function to achieve different drawing purposes.

Analysis: 1. Preferred requires a simple interface for a drawing board, defining a Draw class to initialize the interface using Java Swing components.

2. Then define the event interface DrawListener class so that it implements the above interfaces and overrides the methods in the interfaces for its own purposes.

3. Instantiate DrawListener class objects in Draw.

4. Add an event listening method to the components in the Draw class and specify the event handling class as DrawListener.

Code implementation:

Draw Class

package com.cbs;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * Draw Class for initialization of interfaces
 * 
 * @author CBS
 * 
 */
public class Draw {

    public static void main(String[] args) {
        Draw t = new Draw();
        t.showUI();

    }

    // Interface Initialization Method
    public void showUI() {
        JFrame jf = new JFrame();
        jf.setTitle("Drawing");
        jf.setSize(700, 700);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
        jf.setLayout(layout);
        JButton drawLine = new JButton("Draw a Line");
        jf.add(drawLine);
        JButton drawOval = new JButton("Draw an ellipse");
        jf.add(drawOval);
        JButton drawArc = new JButton("Draw a Curve");
        jf.add(drawArc);
        JButton drawPolygon = new JButton("Triangle");
        jf.add(drawPolygon);
        JButton jb1 = new JButton();
        jb1.setBackground(Color.RED);
        jf.add(jb1);
        jb1.setPreferredSize(new Dimension(30, 30));
        JButton jb2 = new JButton();
        jb2.setBackground(Color.GREEN);
        jf.add(jb2);
        jb2.setPreferredSize(new Dimension(30, 30));

        jf.setVisible(true);

        Graphics g = jf.getGraphics();// Get the current brush
        DrawListener dl = new DrawListener(g);// instantiation DrawListener Object of class
        jf.addMouseListener(dl);// Add a mouse event listening method to the form
        jf.addMouseMotionListener(dl);// Add a mouse movement event listening method to the form

        // Add action monitoring for buttons
        drawLine.addActionListener(dl);
        drawOval.addActionListener(dl);
        jb1.addActionListener(dl);
        jb2.addActionListener(dl);
        drawArc.addActionListener(dl);
        drawPolygon.addActionListener(dl);

    }

}

DrawListener class

package com.cbs;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 * Event Processing Class
 * 
 * @author CBS
 * 
 */

public class DrawListener implements MouseListener, MouseMotionListener,
        ActionListener {

    private int x1, y1, x2, y2;// Record coordinates of two mouse clicks
    private Graphics g;// Getting canvas objects from the interface
    private String str;// Record the information of the current button to distinguish between different buttons
    private Color color;// Record the color information of the brush
    private int f = 1;// Control variable to update coordinates

    public DrawListener(Graphics g) {
        this.g = g;
    }

    //Processing method when mouse is pressed
    public void mousePressed(MouseEvent e) {
        // Record the location of the first click; by object e obtain
        if (f == 1) {
            x1 = e.getX();
            y1 = e.getY();
        }
    }

    //Processing method of mouse click
    public void mouseClicked(MouseEvent e) {
        if ("Triangle".equals(str)) {
            System.out.println("sanjaioxing");
            int x, y;
            x = e.getX();
            y = e.getY();
            g.setColor(color);
            g.drawLine(x, y, x1, y1);
            g.drawLine(x2, y2, x, y);
            f = 1;

        }

    }

    // Processing method for mouse release
    public void mouseReleased(MouseEvent e) {
        // Record coordinates when mouse is released
        if (f == 1) {
            x2 = e.getX();
            y2 = e.getY();
        }
        // The two coordinates are obtained and can be used to draw straight lines, calling the canvas object g Method, draw a line on the interface
        if ("Draw a Line".equals(str)) {
            g.setColor(color);
            g.drawLine(x1, y1, x2, y2);
        }
        if ("Draw an ellipse".equals(str)) {
            g.setColor(color);
            g.drawOval(x1, y1, x2, y2);
        }
        if ("Triangle".equals(str) && f == 1) {
            g.setColor(color);
            g.drawLine(x1, y1, x2, y2);
            f++;
        }
    }

    // Processing method for mouse entry
    public void mouseEntered(MouseEvent e) {

    }

    // Processing method on mouse exit
    public void mouseExited(MouseEvent e) {

    }

    public void actionPerformed(ActionEvent e) {
        if ("".equals(e.getActionCommand())) {
            JButton jb = (JButton) e.getSource();
            color = jb.getBackground();
        } else {
            str = e.getActionCommand();
        }
    }

    // Processing method for mouse dragging
    public void mouseDragged(MouseEvent e) {
        if ("Draw a Curve".equals(str)) {
            int x, y;
            x = e.getX();
            y = e.getY();
            g.setColor(color);
            g.drawLine(x1, y1, x, y);
            x1 = x;
            y1 = y;
        }

    }

    // Moving method on mouse release
    public void mouseMoved(MouseEvent e) {
    
    }

}

ps: This is a hole in the hole. Please correct it.

Keywords: Java

Added by phpstuck on Wed, 19 Jun 2019 20:34:53 +0300