understand the difference between applets and applications identify meaningful applet resources for...

22

Upload: grant-mckinney

Post on 13-Dec-2015

221 views

Category:

Documents


0 download

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 eventsSoftware eventsSound 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.