1 / 22

Events

Events. Objectives. 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. Vocabulary.

kareem
Download Presentation

Events

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Events

  2. Objectives • 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

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

  4. An Event • 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?

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

  6. An Event • 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…

  7. Listeners • 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.

  8. Listeners • 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!

  9. Interaction • 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()

  10. Interaction • 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”.

  11. Add Listener • 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.

  12. Add Listener • 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.

  13. Add Listener • 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.

  14. Add Listener • 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.

  15. Add Listener • 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.

  16. Add Events • 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)

  17. Add Events • 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.

  18. Add 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

  19. Add Events • 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.

  20. Add Events • 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.

  21. Add Events • 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.

  22. Java - Assignments 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.

More Related