Use MouseListener in Processing

What is MouseListener?

java.awt.event.MouseListener (Source: [MouseListener (Java Platform SE 8)]" (https://docs.oracle.com/javase/jp/8/docs/api/java/awt/event/MouseListener) .html))

A listener interface for receiving "related" mouse events (press, release, click, get focus, lose focus) on a component. Use MouseMotionListener to track mouse movements and drags.

To summarize briefly, it is an interface that detects mouse clicks and drags.

Implementation method

Classes related to handling mouse events either implement this interface (and all the methods it contains) or extend the abstract class MouseAdapter (overriding only the relevant methods).

Listener objects created from that class are registered with the component using the component's addMouseListener method.

  1. Implement the MouseListener interface or extend the MouseAdapter.
  2. Register with the component using the ʻaddMouseListener` method.

The process in 2. is written as "registered", but you actually have to write it yourself.

MouseListener in Java

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class EventTest extends Applet implements MouseListener{
  public void init(){
    addMouseListener(this);
  }

  public void mouseEntered(MouseEvent e){
  }

  public void mouseExited(MouseEvent e){
  }

  public void mousePressed(MouseEvent e){
  }

  public void mouseReleased(MouseEvent e){
  }

  public void mouseClicked(MouseEvent e){
  }
}

Source: Event processing method in Java --Event processing --Introduction to Java applets --JavaDrive

With this as a reference, let's create a class that implements the MouseListener interface in Processing as well.

ʻAddMouseListener is a method of java.awt.Component`.

public void addMouseListener(MouseListener l)

Adds the specified mouse listener to receive mouse events from this component.

Source: Component (Java Platform SE 8)

Let's write with Processing

this.surface.getNative () returns an instance of processing.awt.PSurfaceAWT.SmoothCanvas. This is a subclass of java.awt.Component, so you can do ʻaddMouseListener`.

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Component;

void setup() {
  size(300, 300);
  Component component = (Component) this.surface.getNative();
  component.addMouseListener(new MyMouseListener());
}

void draw() {
}

class MyMouseListener implements MouseListener {
  @Override
  public void mouseClicked(MouseEvent e) {
    println("Clicked");
  }

  @Override
  public void mouseEntered(MouseEvent e) {
    println("Entered");
  }

  @Override
  public void mouseExited(MouseEvent e) {
    println("Exited");
  }

  @Override
  public void mousePressed(MouseEvent e) {
    println("Pressed");
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    println("Released");
  }
}

Recommended Posts

Use MouseListener in Processing
Use java.time in Jackson
Use Interceptor in Spring
Use OpenCV in Java
Use images in Rails
Use PostgreSQL in Scala
Use PreparedStatement in Java
Use Extend (Concerns) in Rails to standardize Controller processing.
Write Processing in IntelliJ IDEA
Measured parallel processing in Java
Mazume judgment processing in fishing
Use Redis Stream in Java
Use multiple checkboxes in Rails6!
Simultaneous key press in Processing
How to use Lombok in Spring
Try implementing asynchronous processing in Azure
Use constructor with arguments in cucumber-picocontainer
Date processing in Java (LocalDate: Initialization)
Let's use Twilio in Java! (Introduction)
[Rails] Use cookies in API mode
[Java] Do not use "+" in append!
Use composite keys in Java Map.
Use the Findbugs plugin in Eclipse
[Processing × Java] How to use variables
Implementation of asynchronous processing in Tomcat
How to use InjectorHolder in OpenAM
Self-pipe technique useful in IO.select processing
How to use classes in Java?
Use completion in Eclipse on mac
About file copy processing in Java
Order of processing in the program
Address already in use workaround (Windows)
[Processing × Java] How to use arrays
Do you use Stream in Java?
[Cloud9] Address already in use [Solution]
Multilingual Locale in Java How to use Locale
Use OpenCV_Contrib (ArUco) in Java! (Part 2-Programming)
How to use custom helpers in rails
Unclear error encountered in annotation processing * Unresolved
[Docker] Use environment variables in Nginx conf
How to use named volume in docker-compose.yml
Use inequality comparison operators in MyBatis SQL
Implementation of multi-tenant asynchronous processing in Tomcat
Use docker in proxy environment on ubuntu 20.04.1
[Java] Use cryptography in the standard library
[Processing × Java] How to use the loop
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
Use "Rhino" which runs JavaScript in Java
Use selenium (Firefox) in Ruby in WSL environment
Use DynamoDB query method in Spring Boot
About UI thread processing in Android asynchronous
[Processing × Java] How to use the class
[Processing × Java] How to use the function
Use selenium (Chrome) in Ruby in WSL environment
How to use environment variables in RubyOnRails
I want to use @Autowired in Servlet
How to implement asynchronous processing in Outsystems
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
Steps to Use JConsole in Cognos Analytics