Course: CMPT 101/104 E.100 Thursday, November 23, 2000

Similar documents
Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter 1 GUI Applications

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

G51PRG: Introduction to Programming Second semester Applets and graphics

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

User interfaces and Swing

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

PIC 20A GUI with swing

Graphical Interfaces

Unit 7: Event driven programming

BM214E Object Oriented Programming Lecture 13

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

The AWT Event Model 9

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Programming Mobile Devices J2SE GUI

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

Swing from A to Z Using Focus in Swing, Part 2. Preface

Introduction to the JAVA UI classes Advanced HCI IAT351

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Graphical Interfaces

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Swing from A to Z Some Simple Components. Preface

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Graphic User Interfaces. - GUI concepts - Swing - AWT

H212 Introduction to Software Systems Honors

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Programming Language Concepts: Lecture 8

Advanced Java Programming (17625) Event Handling. 20 Marks

Programming graphics

Programming Languages and Techniques (CIS120e)

Lecture 3: Java Graphics & Events

Window Interfaces Using Swing Objects

Lecture 5: Java Graphics

Class 16: The Swing Event Model

Graphical User Interfaces 2

Handling Mouse and Keyboard Events

GUI Event Handlers (Part I)

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

PESIT Bangalore South Campus

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

CS 2113 Software Engineering

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Graphical User Interfaces 2

GUI Programming: Swing and Event Handling

Window Interfaces Using Swing Objects

AP CS Unit 11: Graphics and Events

(Incomplete) History of GUIs

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60

Lecture 19 GUI Events

Graphical User Interfaces 2

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Windows and Events. created originally by Brian Bailey

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

CSE 331 Software Design & Implementation

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CS 11 java track: lecture 3

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

Object-oriented programming in Java (2)

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

COMPSCI 230. Software Design and Construction. Swing

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical User Interfaces (GUIs)

GUI in Java TalentHome Solutions

Programming Languages and Techniques (CIS120)

CS11 Java. Fall Lecture 3

Part 3: Graphical User Interface (GUI) & Java Applets

Assignment 2. Application Development

UCLA PIC 20A Java Programming

Graphical User Interfaces. Comp 152

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

AP CS Unit 12: Drawing and Mouse Events

Programming Languages and Techniques (CIS120)

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

CS11 Java. Fall Lecture 4

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

MIT AITI Swing Event Model Lecture 17

Interacción con GUIs

TYPES OF INTERACTORS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill

Java Programming Lecture 6

Java Interfaces Part 1 - Events Version 1.1

Graphical User Interface

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Using the API: Introductory Graphics Java Programming 1 Lesson 8

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Final Examination Semester 2 / Year 2012

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

UI Software Organization

Cheng, CSE870. More Frameworks. Overview. Recap on OOP. Acknowledgements:

Transcription:

Course: CMPT 101/104 E.100 Thursday, November 23, 2000 Lecture Overview: Week 12 Announcements Assignment 6 Expectations Understand Events and the Java Event Model Event Handlers Get mouse and text input in applications Frame Windows Adding interface component to frame windows To Do: Review Chapter 10 Read ahead: Chapter 12 1

Events DOS / Unix is kind of comforting but those days are over. With this kind of program the user can only do 1 thing in practice: type in commands or data. This is easy for a programmer to deal with. You can basically force the user to follow a pre-determined path of execution, step by step. For example, it is easy to enforce typical program logic for the Bank programs such as this. Start If the user enters 1, create a new account Else, if the user enters 7, Quit the program Else the input is invalid. Display error message. You can see that the user has one and only one option at any given time: keyboard input. Under these circumstances the programmer has it easy. 2

A typical window application is much more complex: There are multiple controls, each behaving in a different fashion. Some of the controls should only be enabled at certain times. Some of the controls should be populated with data at runtime. Which control does the user push, select, click, or activate? In what order? You probably cannot appreciate how much more complicated this sort of application is than the former, or how much design and testing that must go into these, nevertheless; the days of DOS are pretty well gone. The windows application is the majority of the work out there. 3

There are many different types of events: user types in data user clicks (double-clicks) on a mouse key: (left, right middle?) user presses a special key user resizes window user closes / minimizes window user tabs in or out of an text box etc The operating system will send messages to your program when an event occurs, though most events are generally ignored. To handle an event you need to create an event handler object from a special class. Before considering the complexities let us look at the Event Class Hierarchy: (p. 397): EventObject AWTEvent ActionEvent ComponentEvent InputEvent WindowEvent MouseEvent KeyEvent As you can see, one size does not fit all. If you want to handle events from a particular source you must create an object of the appropriate type. 4

Insert Addendum here. 5

Implementing the Interface The key to handling events is implementing the appropriate listener interface. For example, If you want to receive notifications about mouse events you need to implement the MouseListener interface. The Java Runtime sends you notification of the mouse events by calling the methods that are defined in the MouseListener Interface. That is why you must implement the interface: so that runtime can send you notification of mouse events by calling the methods that you have implemented. Here is that interface definition: public interface MousListener { // p 398 void mouseclicked(mouseevent event); void mouseentered(mouseevent event); void mouseexisted(mouseevent event); void mousepressed(mouseevent event); void mousereleased(mouseevent event); // You can probably guess what these are for. Notice: A parameter object of the MouseEvent class is sent to you with each method. This class has a set of methods that will make your life easier, such as the getx() and gety() methods that will allow you to determine where the mouse cursor is pointing at a given time. In order to get a feel for how these interfaces work, let us consider the applet created in the book: 6

You will see that this applet does little more than implement the mouse interface. import java.applet.applet; import java.awt.event.mouseevent; import java.awt.event.mouselistener; public class MouseSpyApplet extends Applet { public MouseSpyApplet() { // applet constructor MouseSpy listener = new MouseSpy(); addmouselistener(listener); // this tells the runtime where to send the event notifications class MouseSpy implements MouseListener { public void mouseclicked(mouseevent event) { System.out.println("Mouse clicked: x = " + event.getx() + " y = " + event.gety()); public void mouseentered(mouseevent event) { System.out.println("Mouse entered: x = " + event.getx() + " y = " + event.gety()); public void mouseexited(mouseevent event) { System.out.println("Mouse exited: x = " + event.getx() + " y = " + event.gety()); public void mousepressed(mouseevent event) { System.out.println("Mouse pressed: x = " + event.getx() + " y = " + event.gety()); public void mousereleased(mouseevent event) { System.out.println("Mouse released: x = " + event.getx() + " y = " + event.gety()); 7

Overriding Those Methods Overriding interface methods can be tedious, and they are always abstract, which means that you must override them all. Most of the time you don't want your class to do anything with a given method. For example, you generally won't want to do anything special when the mouse enters the area of the applet window, so your implementation would look something like this: public void mouseentered(mouseevent event) { // do nothing Because you rarely want to do anything with all or most of the methods of a given interface, some friendly soul at Sun created a special class that implements every listener interface with more than 1 method: the Adaptor classes. For example, there is a MouseAdapter class that implements that MouseListener interface with methods that do nothing. By creating a subclass of the appropriate Adaptor class (instead of implementing the listener interface from scratch) you only need to override the methods that you actually want to use, rather than having to override many unneeded methods as would be the case normally. // here is what the MouseAdaptor class looks like public class MouseAdapter implements MouseListener { public void mouseclicked(mouseevent event) { public void mouseentered(mouseevent event) { public void mouseexited(mouseevent event) { public void mousepressed(mouseevent event) { public void mousereleased(mouseevent event) { 8

Listeners as Inner Classes You can now create a class that receives event notifications from the Java Runtime. So what? In order to do something useful and interesting, listener objects need to interact with other objects, and those objects are usually private. This poses a problem. In the following example you will see that the listener class and the EggApplet class are completely separate. The listener object cannot change the location of the ellipse that is the egg because it is private: public class EggApplet extends Applet{ // Constants, variables, etc. private Ellipse2D.Double egg; MouseSpy listener = new MouseSpy(); addmouselistener(listener); // etc.. class MouseClickListener extends MouseAdapter { // MouseClickListener objects have not access // to the egg! 9

Source Code Solution A // Put the listener class inside the EggApplet class import java.applet.applet; import java.awt.graphics; import java.graphics2d; import java.awt.geom.ellipse2d; import java.awt.event.mouseevent; import java.awt.event. MouseAdapter; // create an EggApplet class public class EggApplet extends Applet{ // Constants, variables, etc. private Ellipse2D.Double egg; private static final double EDD_WIDTH = 15; private static final double EDD_HEIGHT = 25; // constructor public EggApplet() { egg = new Ellipse2D.Double(0,0,EGG_WIDTH, EGG_HEIGHT); MouseClickListener listener = new MouseClickListener(); addmouselistener(listener); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; g2.draw(egg); // The lister class is declared inside the applet. private class MouseClickListener extends MouseAdapter { // only override one method public void mouseclicked(mouseevent event) { int mousex = event.getx(); int mousey = event.gety(); egg.setframe(mousex EGG_WIDTH/2, mousey EGG_HEIGHT/2, EGG_WIDTH, EGG_HEIGHT); repaint(); 10

Notes from the Applet The constructor of the applet creates an instance of the inner class. The inner class will remember the object in which it was constructed. MouseClickListener listener = new MouseClickListener(); The inner class has no variable named egg, so it looks in the outer class for the egg variable and finds it. Because the MouseClickListener object is an object of the inner class it can freely access the private variable egg. egg.setframe(mousex EGG_WIDTH/2, mousey EGG_HEIGHT/2, EGG_WIDTH, EGG_HEIGHT); setframe() is an ellipse method that updates the position and size of the ellipse. repaint() tells the Java Runtime to tell the Window manager to call the paint() method so that the ellipse can be redrawn in its new position. You could call the paint() method directly yourself since it is a public method of the applet, but this you should never do. Other activities are probably going on while you are running your applet. Let the Window Manager paint() the applet again when it is convenient. The repaint() method belongs to the applet class. It does not show up in the previous definition because we did not override it. Note: objects created from inner classes are like instance variables of the outer class. They can see and use all methods and variables of both themselves and the outer class. You will use inner classes most frequently for event listeners. 11

Frame Windows Let's create a windows application that is not an applet. Such an application does not run inside a browser such as Netscape, but deals with the Operating System through the Java Runtime. Here is the code that would create 2 simple windows: // filename: FrameTest1.java import javax.swing.jframe; public class FrameTest1 { public static void main(string [] args) { EmptyFrame frame1 = new EmptyFrame(); frame1.settitle("frame Test"); frame1.show(); EmptyFrame frame2 = new EmptyFrame(); frame2.settitle("second Frame"); frame2.show(); System.out.println("\n\nMain method is terminating, but Window remains."); // System.exit(0); // This is the simple window class class EmptyFrame extends JFrame { public EmptyFrame(){ // constructor final int DEFAULT_FRAME_WIDTH = 300; // pixels final int DEFAULT_FRAME_HEIGHT = 300; // pixels setsize(default_frame_width, DEFAULT_FRAME_HEIGHT); 12

Notes about FrameTest1.java You will notice that the main method ends but the two windows continue to exist. When any window is created it receives it's own thread. A thread is like a unit of execution in the operating system. You will also notice that the program does not stop running until I press CTRL-C. While the main() method thread has finished the thread that display the windows to you must be manually killed. You will notice that the window has a border and a title bar. All Java Frames have these two things, though this is not necessarily the case for other programming languages. The applet has a border but no title bar. (The title bar of the applet viewer is what you see when you use that program.) This program uses the javax.swing package. A few years ago you used components from the AWT package to create windows, but this created problems. The AWT used frames, buttons, and other components from the native platforms it ran on. The frames, buttons, entry fields, and other components that Microsoft, Apple, and Unix offered where similar to eachother in most respects, but just different enough from eachother to cause lots of headaches for programmers. 'Write once run everywhere' became 'write once, debug everywhere'. The javax (Java extensions) package literally paints every component that it offers from scratch. This makes it a bit slow but very consistent. 13

Killing your program The System.exit(0); call will terminate your program and the runtime, but when should you call this? frame.show(); System.exit(0); Putting this command at the end of the main method will cause the windows to show for a split second and then close as the runtime exits. The ideal solution is to only have the program terminate when both windows are closed. How do we accomplish this? Step 1: Implement the WindowListener interface Just as applets can receive event notifications, so can windows if they implement the WindowListener interface. The abstract methods of the above interface are given below: public interface WindowListener { void windowopened(windowevent e); void windowclosed(windowevent e); void windowactivated(windowevent e); void windowdeactivated(windowevent e); void windowiconified(windowevent e); void windowdeiconified(windowevent e); void windowclosing(windowevent e); Just as there is a MouseAdaptor class that implements the MouseListener methods for you, there is a WindowAdaptor class that does the same thing. 14

Closing the Program 1) Suppose we add a class variable to the EmptyFrame class that keeps track of how many windows are open at a given time: private static int window_count = 0; 2) We increment that variable in the constructor of the EmptyFrame class: window_count++; 3) Suppose we create an event handler that decrements the count and kills the program if the count reaches 0. We will make that class an inner class of the EmptyFrame class: 4) Notice that you need to create an event handler for each window that you open. 5) One problem of using adaptor classes is that minor typos can lead to strange problems. For example, if you type the windowclosing method as WindowClosing you will have created a new method that does not receive the window event notification. 15

FrameTest2.java import javax.swing.jframe; import java.awt.event.windowevent; import java.awt.event.windowadapter; public class FrameTest2 { public static void main(string [] args) { EmptyFrame frame1 = new EmptyFrame(); frame1.settitle("frame Test"); frame1.show(); EmptyFrame frame2 = new EmptyFrame(); frame2.settitle("second Frame"); frame2.show(); // This is the slightly modified simple window class class EmptyFrame extends JFrame { private static int window_count = 0; // Note 1 public EmptyFrame(){ // constructor final int DEFAULT_FRAME_WIDTH = 300; // pixels final int DEFAULT_FRAME_HEIGHT = 300; // pixels setsize(default_frame_width, DEFAULT_FRAME_HEIGHT); WindowCloser listener = new WindowCloser(); addwindowlistener(listener); // Note 4 System.out.println("Added Window Listener"); window_count++; // Note 2 System.out.println("New window #" + window_count); // this is the event handler private class WindowCloser extends WindowAdapter { public void windowclosing(windowevent e){ // Note 3, 5 window_count--; System.out.println("Window #" + window_count + " closed"); if (window_count < 1) { System.exit(0); 16

Adding Components to JFrames JFrame Components are more complex than applet frames. With applets you draw right onto the applet frame. JFrames are actually composed of several layers: Titlebar Menu bar (optional) JFrame Root Pane Holds the glass pane and the content pane together Layered Pane The Content Pane and the Menu bar are mounted on the Layered Pane. Glass Pane The primary purpose of the glass pane is capture mouse events. The glass pane is transparent. Content Pane This one holds the components that you will put on the window 17

Adding a Component to the Window To add a component to a window you need to create a panel and place it on the Content Pane. // filename FrameTest3 import javax.swing.jframe; import javax.swing.jpanel; import java.awt.component; import java.awt.container; import java.awt.graphics; import java.awt.graphics2d; public class FrameTest3 { public static void main(string [] args) { MyFrame frame = new MyFrame(); frame.settitle("one Centered Panel"); frame.show(); class MyPanel extends JPanel { // This is the panel that we will put on the window public void paintcomponent(graphics g) { super.paintcomponent(g); Graphics2D g2 = (Graphics2D)g; g2.drawstring("hi there!", 50, 50); class MyFrame extends JFrame { public MyFrame() { // constructor MyPanel panel = new MyPanel(); Container contentpane = getcontentpane(); contentpane.add(panel, "Center"); final int DEFAULT_FRAME_WIDTH = 300; // pixels final int DEFAULT_FRAME_HEIGHT = 300; // pixels setsize(default_frame_width, DEFAULT_FRAME_HEIGHT); 18

Notes: FrameTest3.java Notice that while the applet has a paint method, the JPanel has a paintcomponent method. public void paintcomponent(graphics g) { super.paintcomponent(g); Also notice the call to the superclass method: paintcomponent( ). Every javax.swing component inherits this method and should call the superclass method. The superclass paintcomponent method will erase the previous contents of the panel, insure that the borders of the panel are drawn and the attributes of the graphics object g are set correctly. Panel Positioning: contentpane.add(panel, "Center"); Notice that a panel can only be positioning loosely on the Content Pane. The only options available are North, Center, South, East and West. (see page 416) 19

Reading in Text Input The final example of the chapter deals with reading in text from a JTextField control. These are very common and worth considering in some detail. Notes: The program has the typical JFrame component. The JFrame component has a private object instance: that of a JTextField. import javax.swing.jframe; import javax.swing.jtextfield; class MyFrame extends JFrame { // various declarations private JTextField textfield; // etc.. This text field is added to the window (MyFrame) at the bottom of the window: Container contentpane = getcontentpane(); contentpane.add(textfield, "South"); 20

Action Events Mouse events are more complex than the events that can occur in a text box, particularly in Java. While some languages allow you receive an event for every time the user presses a key while in the text box, the JTextField component only generates an event when the returns presses ENTER. This results in an Action event. To handle this event you will need to implement the ActionListener interface. This interface only has a single method: public interface ActionListener { public void actionperformed(actionevent event); The ActionEvent object contains various bits of information, such as which component generated the event, and what kind of event occured. We will look at this class in more detail next week, however; for now we know that we only have the JTextField that will be generating events. Here is the class that implements the interface. Notice the gettext() and the settext() methods that are part of the JTextField class... private class TextFieldListener implements ActionListener { public void actionperformed(actionevent event) { String input = textfield.gettext(); // do some stuff textfield.settext(""); 21

Implementation Let's walk through the code. This program allows you to enter data (an integer) into a textbox, then paints that many eggs (ellipses) at random locations in the window. // filename: Eggs.java /* Note: you need a lot of classes, so the import section is long. This is typical of modern software programs: no one person writes the program, but rather teams of people do. Even this simple program requires the work of the developers at Sun. */ import java.awt.container; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.windowadapter; import java.awt.event.windowevent; import java.awt.geom.ellipse2d; import java.util.random; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; /* Question 1: do you understand what role each of these classes will play? It is important to understand that each of the above classes represents an Entity. You must accept the notion that each of the above items has an individual nature. Each has attributes and behaviour (variables and methods) associated with it that you do not know about. */ 22

/* Notice how simple the Eggs class is. All it does is provide a place for the program to start, creates an EggFrame object, sets an attribute, and displays it to the user. Then the main() method ends. The EggFrame object does all the rest of the work itself. It truly acts as an independent entity with a life of its own. This is quite a departure from the practice of procedural programming where the main method is the source of most every command and action. */ public class Eggs { public static void main(string[] args) { EggFrame frame = new EggFrame(); frame.settitle("enter a number of eggs"); frame.show(); // end of Eggs class /* Here is the EggFrame class. This class is used to create the window object that will be displayed. This class contains classes of its own. An EggFrame object will contain other objects. An EggFrame object will handle it's own events. */ class EggFrame extends JFrame { // This frame class will contain a text field object mounted on // a panel object. private JTextField textfield; private EggPanel panel; 23

/* Event Handler class Notice how this class has one purpose only: it kills (exits, closes, destroys, etc.) this program when it receives the windowclosing() event. (How does it receive this event?) This is a very simple, inner class that belongs to the EggFrame class. It is used by the EggFrame class to close the program when the EggFrame window is closed by the user. */ private class WindowCloser extends WindowAdapter { public void windowclosing(windowevent event){ System.exit(0); // default constructor public EggFrame() { final int DEFAULT_FRAME_WIDTH = 300; final int DEFAULT_FRAME_HEIGHT = 300; /* NOTE: you always find these specifications laid out as constants. Magic Numbers are the kiss of death in the real world of programming. */ // This method is inherited. setsize(default_frame_width, DEFAULT_FRAME_HEIGHT); //WindowCloser closer = new WindowCloser(); //addwindowlistener(closer); addwindowlistener(new WindowCloser()); /* Notice the above method call. The statement new WindowCloser() returns a reference to a object that implements the WindowListener interface. You pass this reference to the addwindowlistener method. Notice that an an un-named object now exists as our Window Event handler, but the handler is simple so we don't care to keep track of it. */ panel = new EggPanel(); contentpane.add(panel, "Center"); textfield = new JTextField(); textfield.addactionlistener(new TextFieldListener()); contentpane.add(textfield,"south"); 24

/* Text Field event handler: another inner class We will use this class to handle events from the the text field. Actually, we could use this class for any text field. We know that an event occurs when the user enters text and presses ENTER. */ private class TextFieldListener implements ActionListener { // How does an ActionListener differ from a MouseListener? public void actionperformed(actionevent event) { String user_input = textfield.gettext(); int number = Integer.parseInt(user_input); // could an Exception be thrown? // If so, why doesn't the compiler complain? panel.seteggcount(number); textfield.settext(""); // where did this method come from? What does it do? // Why can the textfield be referenced here? 25

/* Here is the class that we draw eggs (Ellipses) on. */ class EggPanel extends JPanel { private int eggcount = 0; private static final double EGG_WIDTH = 30; private static final double EGG_HEIGHT = 50; // What is gained here in using constants? public void paintcomponent(graphics g) { super.paintcomponent(g); // why call the superclass' method? Graphics2D g2 = (Graphics2D)g; // what is happening here? Why do it? Random generator = new Random(); for (int ii = 0; ii < eggcount; ii++) { double x = getwidth() * generator.nextdouble(); double y = getheight() * generator.nextdouble(); Ellipse2D.Double egg = new Ellipse2D.Double(x,y, EGG_WIDTH, EGG_HEIGHT); g2.draw(egg); /* This method provides a way for the private variable eggcount to be set. */ public void seteggcount(int count) { eggcount = count; repaint(); // why not call paintcomponent directly? // end EggPanel class // end EggFrame class Note: this is a long, gritty program if you are new to the world of UI and components. I recommend that you type it in and compile it yourself. 26