COP3252 Advanced Java Programming - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

COP3252 Advanced Java Programming

Description:

Just like with mouse events, we can tell our Java programs to handle events from the keyboard ... Key events are always generated in JAVA ... – PowerPoint PPT presentation

Number of Views:153
Avg rating:3.0/5.0
Slides: 32
Provided by: UoD
Category:

less

Transcript and Presenter's Notes

Title: COP3252 Advanced Java Programming


1
COP3252Advanced Java Programming
  • 20-Mar-08
  • Lecture Set 17
  • Mouse Listeners, Keyboard Listeners, Applets,
    Progress Bars

2
Mouse Event Handling
  • Java includes two built-in interfaces for
    handling mouse events.
  • MouseListener
  • MouseMotionListener
  • Part of the javax.swing.event package
  • Each interface includes a set of methods
    available for working with various mouse events
    (work just like the ActionListener /
    ActionPerformed Interface/Method combo)

3
Mouse Handling Interfaces and Methods
  • Interface MouseListener
  • Methods
  • public void mousePressed(MouseEvent event)
  • Called when a mouse button is pressed while the
    mouse cursor is on a graphical component
  • public void mouseClicked (MouseEvent event)
  • Called when a mouse button is pressed and
    realeased when the mouse remains stationary on a
    component. This event is always preceded by a
    call to mousePressed.
  • public void mouseReleased (MouseEvent event)
  • Called when a mouse button is released after
    being pressed. This event is always preceded by a
    call to mousePressed and one or more calls to
    mouseDragged.
  • public void mouseEntered (MouseEvent event)
  • Called when a mouse cursor enters the bounds of a
    component
  • public void mouseExited (MouseEvent event)
  • Called when a mouse cursor exits the bounds of a
    component.

4
Mouse Handling Interfaces and Methods
  • Interface MouseMotionListener
  • Methods
  • public void mouseDragged (MouseEvent event)
  • Called when the mouse button is pressed while the
    mouse cursor is on a component and the mouse is
    moved while the mouse button remains pressed.
    This event is always preceded by a call to
    mousePressed. All drag events are sent to the
    component on which the user began to drag the
    mouse.
  • public void mouseMoved (MouseEvent event)
  • Called when the mouse is moved when the mouse
    cursor is on a component. All move events are
    sent to the component over which the mouse is
    currently positioned.

5
Mouse Event Handling Example
  • See MouseEvents.java

6
Making a PaintPanel
  • We can use the mouse event handlers to turn out
    JPanel into a PaintPanel
  • See PaintPanelBasic.java
  • Also PaintPanelRect.java for drawing rectangles
    in a ms paint way

7
Handling Keyboard Events
  • Just like with mouse events, we can tell our Java
    programs to handle events from the keyboard
  • The listener for the keyboard is called
    KeyListener
  • Key events are generated anytime a key is pressed
    and released.

8
Implementing a KeyListener
  • When implementing a KeyListener, we have to
    declare three different methods
  • keyPressed
  • Handles a key being pressed
  • keyReleased
  • Handles a key being released
  • keyTyped
  • Handles an aciton key being typed

9
Action Keys
  • The following are considered action keys
  • Arrow keys
  • Home
  • End
  • Page up
  • Page down
  • Function keys
  • Num lick
  • Print screen
  • Scroll lock
  • Caps lock
  • Pause

10
Key Events
  • Key events are always generated in JAVA
  • however, the component that handles the event
    must have the focus
  • In other words, the frame where events are being
    handled must be the currently selected frame in
    your operating system.

11
KeyEvent Class
  • The key event class has several useful functions
    to allow us to pull information about the key
    event.
  • int getKeyCode()
  • Get the key code associated with this event. The
    key code identifies the particular key on the
    keyboard that the user pressed or released. The
    KeyEvent class defines many key code constants
    for commonly seen keys. For example, VK_A
    specifies the key labeled A, and VK_ESCAPE
    specifies the Escape key.
  • String getKeyText(int) and String
    getKeyModifiersText(int)
  • Return text descriptions of the event's key code
    and modifier keys, respectively.
  • boolean isActionKey()
  • Return true if the key firing the event is an
    action key. This information is valid only for
    key-pressed and key-released events.
  • int getKeyLocation()
  • Return the location of the key that fired this
    event. This provides a way to distinguish keys
    that occur more than once on a keyboard, such as
    the two shift keys, for example. The possible
    values are KEY_LOCATION_STANDARD,
    KEY_LOCATION_LEFT, KEY_LOCATION_RIGHT,
    KEY_LOCATION_NUMPAD, or KEY_LOCATION_UNKNOWN.

12
Handling Keyboard events
  • See KeyboardEvents.java

13
Applets
  • An applet is a software component that runs in
    the context of another program
  • In JAVA, we create applets in order to allow our
    programs to be accessible from a webpage.

14
Applets
  • The original Applet class was part of the AWT.
  • A newer class called JApplet is included in
    Swing, but many people still use the older Applet
    class.
  • The JApplet class makes turning an existing GUI
    program into an applet VERY easy

15
JApplet
  • Instead of extending JFrame, you want to extend
    JApplet in your program.
  • JFrame and JApplet are very similar so, almost
    everything will work without modification
  • There are a few exceptions

16
Example Changing MouseEvents.java into an Applet
  • The first step is to take the MouseEvents.java
    program and rename it to something like
    MouseEventsApplet.java
  • Then --- Modify it so your public class extends
    JApplet instead of JFrame

17
MouseEventsApplet.java
  • If you try to compile the program at this point,
    you will get a few errors
  • MouseEventsApplet.java64 cannot find symbol
  • symbol method setDefaultCloseOperation(int)
  • location class MouseEventsApplet
  • setDefaultCloseOperation(JFrame.EXIT_ON_CL
    OSE)
  • MouseEventsApplet.java70 incompatible types
  • found MouseEventsApplet
  • required javax.swing.JFrame
  • JFrame frame new MouseEventsApplet()
  • 2 errors
  • Process completed.
  • If the JFrame had a title, you would also get an
    error that looks like this
  • EXAMPLE.java

18
MouseEventsApplet.java
  • The errors are as follows
  • The JApplet class doest have a constructor that
    takes in a String for a title (because an applet
    has no title) so, you have to remove that.
  • There is no window listener available with
    applets (because there is no close button)
    so, you will have to remove that line also.

19
MouseEventsApplet.java
  • Your program is no longer extending JFrame, it is
    extending JApplet
  • So, we will create a standard JFrame and simply
    add our JApplet to it (to maintain application
    functionality).
  • You will be modifying the main section of your
    program to create and customize the main JFrame.

20
MouseEventsApplet.java
  • Modify the public static void main (String
    args)
  • function so that following appear
  • //Create a JFrame here instead ....
  • JFrame frame new JFrame()
  • //Add the MouseEventsApplet to this frame
  • frame.add(new MouseEventsApplet())

21
MouseEventsApplet.java
  • Now, the program should compile without any
    errors.
  • Next, you will have to move the class files to
    your webserver. Make sure to set the appropriate
    file permissions on them.
  • Finally, you will need to create a webpage to
    display the applet.

22
Webpage for the MouseEventsApplet
  • width500

23
The Applet Webpage
  • See http//www.cs.fsu.edu/cop3252/applet.html
  • The applet should function just as the
    application did.
  • The .class file can still be run as an
    application from a command line using the normal
    java run command.
  • This program is now both and applet and an
    application.

24
The Sandbox Security Model
  • Applets are typically downloaded from non-trusted
    sources on the internet
  • What if the applet is malicious?
  • Java platform runs all applets in a sandbox to
    prevent applets from accessing local resources
    (such as files).

25
The Applet life-cycle
  • 5 methods are called by the Applet container from
    the time the applet is loaded into the browser to
    the time that the applet is terminated.
  • init, start, paint, stop, destroy
  • All but paint have empty bodies by default but
    we can declare these methods in our applet body
    to add functionality.

26
public void init()
  • Called once by the applet container when an
    applet is loaded for execution.
  • Initializes the applet
  • Typical actions
  • Initialize fields
  • Creating GUI components
  • Loading sounds
  • Loading images
  • Creating threads

27
public void start()
  • Called by the applet container after the method
    init completes execution.
  • If user browses to new website and returns, start
    is called again.
  • Performs any tasks that must be completed when
    applet is loading for first time and must be
    performed every time the applets HTML page is
    revisited.
  • Starting an animation, starting other threads of
    execution

28
public void paint (Graphics g)
  • Called by the applet after init and start.
  • Also called when applet needs to be repainted.
  • User covers applet.
  • Typical actions
  • Drawing with the Graphics object g

29
public void stop()
  • Called whenever the user leaves the applets web
    page
  • Performs tasks that might be required to suspend
    applets execution (so that it does not continue
    processing when user is no longer viewing)
  • Example stop animations or execution of threads

30
public void destroy()
  • Called when applet it removed from memory
  • User exits browser session by closing browser
  • Also can occur when browser clears memory
  • Performs any tasks that are required to clean up
    the resources allocated to the applet.

31
Progress Bars
  • (Side note Your instructor just discovered this
    in the API today )
  • http//java.sun.com/javase/6/docs/api/javax/swing/
    JProgressBar.html
  • Example See Progress.java
  • See Also
  • http//java.sun.com/javase/6/docs/api/javax/swing/
    ProgressMonitorInputStream.html
  • http//java.sun.com/javase/6/docs/api/javax/swing/
    ProgressMonitor.html
  • http//java.sun.com/docs/books/tutorial/uiswing/co
    mponents/progress.html
Write a Comment
User Comments (0)
About PowerShow.com