23-Events-And-Event-Handlers-1

23-Events-And-Event-Handlers-1 - CS106A Handout 23 April...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106A Handout 23 Spring 2011 April 27 th , 2011 Event and Event Handlers This particular handout addresses some of the material taught in Chapters 9 and 10 (the exact sections are identified in Handout 02). The overarching goal of the two chapters is to advance your understanding of the ACM Graphics Package we use in CS106A, and to get you to start thinking about event driven (as opposed to procedurally driven) programs. Event driven programs are those that are permitted to make progress only when the user triggers some event—a mouse click, mouse movement, scrollbar movement, a key press, or something else. All of the programs we’ve written thus far have been procedurally driven in the sense that the run method runs in sequence through the outline of statements and helper method calls in order to realize some goal. An event-driven program, however, is much more representative of how a huge number of modern Macintosh, Windows, and mobile applications work—they typically do nothing or do very little until they detect that user has interacted with the program is some meaningful way. Mouse Click Events Here is a short program illustrating how mouse clicks might wholly dictate how the program executes. The program is initialized (welcome, init method) to listen for mouse clicks, and the mouseClicked method is (by protocol, the method needs to be called exactly that) invoked every single time the mouse is pressed and releases without movements. Each mouse click is programmatically captured in an object of type MouseEvent , and that event object is passed to the mouseClicked method so the click location can influence what happens. (We also say that the mouseClicked method handles the click event, and that’s why it’s sometimes called an event handler .) public class MouseDrivenPainter extends GraphicsProgram { public void init() { addMouseListeners(); } public void mouseClicked(MouseEvent me) { addCircle(me.getX(), me.getY()); } private void addCircle(int cx, int cy) { GOval circle = new GOval(cx - CIRLCE_RADIUS, cy - CIRLCE_RADIUS, 2 * CIRLCE_RADIUS, 2 * CIRLCE_RADIUS); Color color = rgen.nextColor(); circle.setFilled(true); circle.setFillColor(color); add(circle); } private RandomGenerator rgen = RandomGenerator.getInstance(); private static final int CIRLCE_RADIUS = 10; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The window above represents a sample run of the previous program, where each circle is present because I clicked there at some point. There are some features of this program that are new and worth calling out: There is no run method, but there is an init method. init is new for us, and is designed to hold the sequence of instructions that are associated with program configuration and initialization. In this case, we call a built-in method—it comes with the GraphicsProgram class—called initMouseListeners , which instructs the program to wire all mouse clicks to trigger execution of its own mouseClicked method. Programs can have both init and run (and init is called before
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

23-Events-And-Event-Handlers-1 - CS106A Handout 23 April...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online