

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















© 2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/ Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
2
Originals of slides and source code for examples: http://courses.coreservlets.com/Course-Materials/java.html Also see the Java 8 tutorial – http://www.coreservlets.com/java-8-tutorial/ and customized Java training courses (onsite or at public venues) – http://courses.coreservlets.com/java-training.html
© 2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/ Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
For live Java-related training, email [email protected]
Topics in This Section
4
Techniques are Widely
Applicable
Handling Events with a Separate
Listener: Simple Case
8
Separate Listener: Simple Case
(Continued)
Generalizing Simple Case
10
Handling Events with Separate
Listener: General Case
© 2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/ Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
14
public interface Shape { public double getArea(); // No body, just specification }
public class Circle implements Shape { public double getArea() { some real code } }
Review of Interfaces: Benefits
16
Source Code for MouseListener
and MouseAdapter (Simplified)
public interface MouseListener { public void mouseClicked(MouseEvent e); public void mousePressed(MouseEvent e); public void mouseReleased(MouseEvent e); public void mouseEntered(MouseEvent e); public void mouseExited(MouseEvent e); }
public abstract class MouseAdapter implements MouseListener { public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }
Adapters vs. Interfaces: Method Signature Errors
20
@Override Examples
public class CircleDrawer1 extends Applet { @Override public void init() { setForeground(Color.BLUE); addMouseListener(new CircleListener()); } }
public class CircleListener extends MouseAdapter { private int radius = 25; @Override public void mousePressed(MouseEvent event) { Applet app = (Applet)event.getSource(); Graphics g = app.getGraphics(); g.fillOval(event.getX()-radius, event.getY()-radius, 2radius, 2radius); } }
© 2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/ Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
22
public void foo(…) { InnerClass inner = new InnerClass(); inner.bar(); }
private class InnerClass { public void bar() { doSomethingWith(count); } }
23 }
public class CircleDrawer4 extends Applet { public void init() { setForeground(Color.BLUE); addMouseListener (new MouseAdapter() { private int radius = 25;
public void mousePressed(MouseEvent event) { Graphics g = getGraphics(); g.fillOval(event.getX()-radius, event.getY()-radius, 2radius, 2radius); } }); } (^26) }
© 2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/ Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Event Handling Strategies:
Pros and Cons
Event Handling Strategies:
Pros and Cons (Continued)
Standard AWT Event Listeners
(Summary)
32
Adapter Class Listener (If Any) Registration Method ActionListener addActionListener AdjustmentListener addAdjustmentListener ComponentListener ComponentAdapter addComponentListener ContainerListener ContainerAdapter addContainerListener FocusListener FocusAdapter addFocusListener ItemListener addItemListener KeyListener KeyAdapter addKeyListener MouseListener MouseAdapter addMouseListener MouseMotionListener MouseMotionAdapter addMouseMotionListener TextListener addTextListener WindowListener WindowAdapter addWindowListener
Standard AWT Event Listeners
(Details)
Standard AWT Event Listeners
(Details Continued)
34
Standard AWT Event Listeners
(Details Continued)
Example: Simple Whiteboard
import java.applet.Applet; import java.awt.; import java.awt.event.;
public class SimpleWhiteboard extends Applet { protected int lastX=0, lastY=0;
public void init() { setBackground(Color.WHITE); setForeground(Color.BLUE); addMouseListener(new PositionRecorder()); addMouseMotionListener(new LineDrawer()); }
protected void record(int x, int y) { lastX = x; lastY = y; (^38) }
Simple Whiteboard (Continued)
private class PositionRecorder extends MouseAdapter { public void mouseEntered(MouseEvent event) { requestFocus(); // Plan ahead for typing record(event.getX(), event.getY()); }
public void mousePressed(MouseEvent event) { record(event.getX(), event.getY()); } } ...
Simple Whiteboard (Continued)
private class LineDrawer extends MouseMotionAdapter { public void mouseDragged(MouseEvent event) { int x = event.getX(); int y = event.getY(); Graphics g = getGraphics(); g.drawLine(lastX, lastY, x, y); record(x, y); } } }
40
Simple Whiteboard (Results)