Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.

Slides:



Advertisements
Similar presentations
Made with love, by Zachary Langley Applets The Graphics Presentation.
Advertisements

Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
TCU CoSc Programming with Java Handling Events.
Event Handling.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Mouse Events and Keyboard Events
Event Handling Events and Listeners Timers and Animation.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
Intermediate Java1 An example that uses inner classes Week Four Continued.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
Event Handling in Java CSE Software Engineering Spring 2000 By Prasad.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Java Implementation Software Construction Lecture 6.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Introduction to Java Programming
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
COSC 4126 User Interaction User Interaction capturing and responding to input events.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
CS Lecture 04 Mice Lynda Thomas
Lecture 18: Events; Cool Applets Yoni Fridman 7/30/01 7/30/01.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Laboratory Study November, Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Events and Event Handling
Chapter 14 Event-Driven Programming
Java Applets.
Applets.
Chapter 12 Event-Driven Programming
More Events.
INFSY 547: WEB-Based Technologies
GUI III IS
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
CSE Software Engineering Fall 1999 Updated by J. Brown
ITEC324 Principle of CS III
Java Applets.
Making Java GUIs Functional
Final project.
ITEC324 Principle of CS III
Chapter 6 Inheritance.
Presentation transcript:

Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java applet Write a basic HTML page that uses a Java applet

Here are some terms that you’ll encounter in your lesson on Events: Interface Listener Overrides Implements Buffer Empty methods

As mentioned in the introduction, an “event” is just something that happens. When you clicked your mouse to get to this slide, we call this a “mouse event”. Tricky, eh?

What other “events” does a computer deal with? Keyboard events Software events Sound events Basically, any interaction with your software.

This lesson will focus on the basic two events, mouse and keyboard. But, a riddle: if a tree falls in the forest with no one to hear it, does it make a sound? Hmm…

What the tree thing means: Something can happen, but does that mean it was heard? For that, Java also created “listeners”. We have one for each type of event.

The next slides will present what we mean by “mouse events” and “mouse listeners”. I bet you never thought you’d listen to a mouse!

Review your program from the Applets lesson, because we’ll be using that to start. We left off with it printing during the four methods of init(), start(), stop() and destroy()

Our goal: make the program listen to the mouse and print out “squeak” when the mouse is clicked. First, we need to make our program “listen”.

Programming used to be “procedural”, and you would have to write ALL of the code to make the program listen. Because Java is object-oriented, we can just “import” the Listener code.

So, first step is to add an import statement: import java.awt.event.MouseListener; The MouseListener class is an “interface”. It allows us to use certain methods to “interface” or “interact” with other things well. We won’t go into much more detail on interfaces here, it’s one of the toughest concepts in programming Java.

After importing MouseListener, we use it by “implementing” it. import java.awt.event.MouseListener; public class SimpleClick extends Applet implements MouseListener { As you can see, we still “extend” Applet (or JApplet) because it’s an object, but we use the word “implement” to use the MouseListener interface.

Next, we have to tell the program when to start “listening”. Remember our init() method? public void init() { addMouseListener(this); buffer = new StringBuffer(); addItem("initializing... "); } We add the listener at the initialization, or beginning, of our program.

public void init() { addMouseListener(this); buffer = new StringBuffer(); addItem("initializing... "); } These other lines in init() do two simple tasks: buffer = new StringBuffer() creates an object to hold the words that we’re going to print, like a sentence that gets words added to it. addItem(“initializing…”) adds the word “initializing” to the buffer for printing.

Okay, so the program is listening! Since it’s listening to the mouse, what can the mouse do? A mouse can: -Click -Press -Release -Enter the applet -Exit the applet (It can also do moving things, but that’s MouseMotionListener)

Just like the Listener, we need to import the code for the MouseEvent: import java.awt.event.MouseEvent; MouseEvent is an object, so we don’t need to “implement” it, like the MouseListener interface. Also, when we added the “addMouseListener()” to init(), that is our connection to mouse events.

We ARE required to write methods for the 5 major mouse events, even if they do nothing! This is a requirement when using an interface, and the MouseListener is listening for the 5 we listed before: Click Press Release Enter the applet Exit the applet

Your code for the methods will look like this: public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } public void mousePressed(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } public void mouseClicked(MouseEvent event) { } The methods are “empty”, there is no code in the curly braces. That’s okay! Interfaces only require that you write the methods, not to fill them! But, we do want our mouseClicked method to do something.

Notice that the methods have one parameter, a MouseEvent: public void mouseEntered(MouseEvent event) { } They call the MouseEvent “event”, but you could call it anything. “event” just seems to make sense.

When we click the mouse, we want it to say “ouch” and add it to the buffer string. It’s just like the other methods you have: public void mouseClicked(MouseEvent event) { addItem(“ouch!... "); } This calls the addItem method that you already have, and it adds “ouch!...” to the buffer.

Now try the “Mouse Lab”. Most of the code is written for you (Java is so cool like that!), you will have to modify it.