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.
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.
MouseListener
interface or extend the MouseAdapter
.The process in 2. is written as "registered", but you actually have to write it yourself.
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)
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