Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.

Similar presentations


Presentation on theme: "Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01."— Presentation transcript:

1 Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01

2 OutlineOutline ä Events ä Event actions ä Firing an event ä Listening to an event ä Mouse events ä Mouse motion events ä Components ä Cool applets ä Events ä Event actions ä Firing an event ä Listening to an event ä Mouse events ä Mouse motion events ä Components ä Cool applets

3 EventsEvents ä An event is an action triggered by the user, such as a mouse click or a key press. ä When an event occurs, an object is created that contains information about that event. ä There are several different classes of events. ä For example, if the mouse is clicked, an object of the class MouseEvent is created. ä This object contains the x and y coordinates of where the mouse was when it was clicked. ä But how is an event object created? ä An event is an action triggered by the user, such as a mouse click or a key press. ä When an event occurs, an object is created that contains information about that event. ä There are several different classes of events. ä For example, if the mouse is clicked, an object of the class MouseEvent is created. ä This object contains the x and y coordinates of where the mouse was when it was clicked. ä But how is an event object created?

4 EventsEvents ä Certain objects (like applets, buttons, menus, etc.) generate event objects when an event occurs. We say that they fire an event. ä Other objects, called listeners, wait for an event to be fired and then respond. ä Certain objects (like applets, buttons, menus, etc.) generate event objects when an event occurs. We say that they fire an event. ä Other objects, called listeners, wait for an event to be fired and then respond. Generator This object fires an event Listener This object waits for and responds to an event The generator passes the event object to the listener Event

5 Mouse Events ä There are five events of the MouseEvent class: ä Mouse pressed – the mouse button is pressed down. ä Mouse released – the mouse button is let go of. ä Mouse clicked – the mouse button is pressed and then released. ä Mouse entered – the mouse pointer is moved over a component. ä Mouse exited – the mouse pointer is moved off of a component.  To use these events, we have to add implements MouseListener to our class definition. ä There’s a method to listen for each of these events – we must provide a definition for each, even if it has an empty body, and pass it a MouseEvent argument. ä There are five events of the MouseEvent class: ä Mouse pressed – the mouse button is pressed down. ä Mouse released – the mouse button is let go of. ä Mouse clicked – the mouse button is pressed and then released. ä Mouse entered – the mouse pointer is moved over a component. ä Mouse exited – the mouse pointer is moved off of a component.  To use these events, we have to add implements MouseListener to our class definition. ä There’s a method to listen for each of these events – we must provide a definition for each, even if it has an empty body, and pass it a MouseEvent argument.

6 Mouse Motion Events ä There are two events of the MouseMotionEvent class: ä Mouse moved – the mouse is moved. ä Mouse dragged – the mouse is moved while the mouse button is held down.  To use these events, we have to add implements MouseMotionListener to our class definition.  If we use both mouse events and mouse motion events, we add implements MouseListener, MouseMotionListener. ä The methods for these events also take an argument of type MouseEvent. ä There are two events of the MouseMotionEvent class: ä Mouse moved – the mouse is moved. ä Mouse dragged – the mouse is moved while the mouse button is held down.  To use these events, we have to add implements MouseMotionListener to our class definition.  If we use both mouse events and mouse motion events, we add implements MouseListener, MouseMotionListener. ä The methods for these events also take an argument of type MouseEvent.

7 ComponentsComponents ä In Java, a component is a visual object that reacts to user input. ä Button ä Check box ä Scroll bar ä Button ä Check box ä Scroll bar ä List ä Choice ä List ä Choice

8 ComponentsComponents ä To add a component to your applet you first have to create a panel that will hold all the components: ä Panel myPanel = new Panel(); ä Then create the component: ä Button playButton = new Button(“Play”); ä Then set the component’s characteristics: ä playButton.setForeground(Color.black); ä playButton.setBackground(Color.lightGray); ä And finally add the component to the panel: ä panel.add(playButton); ä To add a component to your applet you first have to create a panel that will hold all the components: ä Panel myPanel = new Panel(); ä Then create the component: ä Button playButton = new Button(“Play”); ä Then set the component’s characteristics: ä playButton.setForeground(Color.black); ä playButton.setBackground(Color.lightGray); ä And finally add the component to the panel: ä panel.add(playButton);

9 HomeworkHomework ä None officially, but start studying for the final if you haven’t yet.


Download ppt "Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01."

Similar presentations


Ads by Google