Commit 1f3188e5 authored by Keiko Katsuragawa's avatar Keiko Katsuragawa

add 2-5-EventBinding

parent 711430f7
/*
* CS 349 Java Code Examples
*
* AdapterEvents.java Binding events by creating a child listener
* adapter object.
*
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.*;
public class AdapterEvents extends JPanel {
public static void main(String[] args) {
AdapterEvents panel = new AdapterEvents();
JFrame f = new JFrame("AdapterEvents");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 200);
f.setContentPane(panel);
f.setVisible(true);
}
AdapterEvents() {
this.addMouseMotionListener(new MyMouseMotionListener());
}
class MyMouseMotionListener extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
}
int x;
int y;
int size = 50;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
g2.fillOval(x - size/2, y - size/2, size, size);
}
}
\ No newline at end of file
/*
* CS 349 Java Code Examples
*
* EventLoop.Java Binding events by tapping into the low-level
* java event queue.
*
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
public class EventLoop extends JPanel {
EventLoop()
throws InterruptedException, InvocationTargetException {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
// replace the current event queue with MyEventQueue
eq.push(new MyEventQueue());
System.out.println("Run");
}
});
}
public static void main(String[] args)
throws InterruptedException, InvocationTargetException {
EventLoop panel = new EventLoop();
JFrame f = new JFrame("EventLoop");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 200);
f.setContentPane(panel);
f.setVisible(true);
}
int x;
int y;
int size = 50;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
g2.fillOval(x - size/2, y - size/2, size, size);
}
// kind of like an event loop
private class MyEventQueue extends EventQueue {
// mouse events come in here
public void dispatchEvent(AWTEvent e) {
//System.out.println("dispatchEvent " + e.getID() );
if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
MouseEvent me = (MouseEvent)e;
x = me.getX();
y = me.getY();
System.out.println("(" + x + "," + y + ")");
repaint();
}
super.dispatchEvent(e);
}
}
}
\ No newline at end of file
/*
* CS 349 Java Code Examples
*
* InheritanceEvents.java Binding events by extending base class with
* event callback methods.
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.*;
public class InheritanceEvents extends JPanel {
public static void main(String[] args) {
InheritanceEvents panel = new InheritanceEvents();
JFrame f = new JFrame("InheritanceEvents");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 200);
f.setContentPane(panel);
f.setVisible(true);
// enable events for this JPanel
panel.enableEvents(MouseEvent.MOUSE_MOTION_EVENT_MASK);
}
protected void processMouseMotionEvent(MouseEvent e)
{
// only detects when button is down WHILE also moving
if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
x = e.getX();
y = e.getY();
repaint();
}
}
int x;
int y;
int size = 50;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
g2.fillOval(x - size/2, y - size/2, size, size);
}
}
\ No newline at end of file
/*
* CS 349 Java Code Examples
*
* InterfaceEvents.java Binding events by implementing event listener
* interface.
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.*;
public class InterfaceEvents extends JPanel implements MouseMotionListener {
InterfaceEvents() {
// need to add the listener to this panel
// (panel and listener are the same in this case)
this.addMouseMotionListener(this);
}
public static void main(String[] args) {
InterfaceEvents panel = new InterfaceEvents();
JFrame f = new JFrame("InterfaceEvents");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 200);
f.setContentPane(panel);
f.setVisible(true);
}
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) { /* no-op */ }
int x;
int y;
int size = 50;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
g2.fillOval(x - size/2, y - size/2, size, size);
}
}
\ No newline at end of file
/*
* CS 349 Java Code Examples
*
* ListenerEvents.java Binding events by creating a child object
* which implements the listener interface.
*
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.*;
public class ListenerEvents extends JPanel {
public static void main(String[] args) {
ListenerEvents panel = new ListenerEvents();
JFrame f = new JFrame("ListenerEvents");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 200);
f.setContentPane(panel);
f.setVisible(true);
}
ListenerEvents() {
// need to add the listener to this panel
this.addMouseMotionListener(new MyMouseMotionListener());
}
// inner class listener
class MyMouseMotionListener implements MouseMotionListener {
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) { /* no-op */ }
}
int x;
int y;
int size = 50;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
g2.fillOval(x - size/2, y - size/2, size, size);
}
}
\ No newline at end of file
# super simple makefile
# call it using 'make NAME=name_of_code_file_without_extension'
# (assumes a .java extension)
NAME = "EventLoop"
all:
@echo "Compiling..."
javac *.java
run: all
@echo "Running..."
java $(NAME)
clean:
rm -rf *.class
## Java event demos
* `EventLoop.java` Binding events by tapping into the low-level java event queue
* `InheritanceEvents.java` Binding events by extending base class with event callback methods
* `InterfaceEvents.java` Binding events by implementing event listener interface
* `ListenerEvents.java` Binding events by creating a child object which implements the listener interface
* `AdapterEvents.java` Binding events by creating a child listener adapter object
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment