MouseEvents

1 / 24
About This Presentation
Title:

MouseEvents

Description:

Used to detect mouse mouse buttons (Left & Right) Pressed, Released, entered, ... Associated event handler methods are. mousePressed(MouseEvent e) any ... a jar ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 25
Provided by: philli89

less

Transcript and Presenter's Notes

Title: MouseEvents


1
MouseEvents
  • Two Listener Interfaces
  • MouseListenerUsed to detect mouse mouse buttons
    (Left Right)Pressed, Released, entered, exits
    and clicked.Associated event handler methods
    aremousePressed(MouseEvent e) any mouse button
    pressedmouseReleased(MouseEvent e) any mouse
    button releasedmouseClicked(MouseEvent e) any
    mouse button pressed and
    released. mouseEntered(MouseEvent e) mouse on a
    component mouseExit(MouseEvent e) mouse
    leaves a component

2
MouseEvents (2)
  • MouseMotionListenerUsed to detect mouse
    movementAssociated event handler methods
    aremouseMoved(MouseEvent e) when the mouse
    movesmouseDragged(MouseEvent e) when the mouse
    moves with
    the button depressed.

3
Mouse Events (3)
  • The MouseEvent object has several methodspublic
    int getX() returns the X coord of the
    mouse.public int getY() returns the Y coord of
    the mouse.public point getPoint() returns a
    point object which
    contains
    the X and Y position. public int getClickCount()
    returns the number of

    clicks. public boolean isMetaDown() true if
    the right button

    down.Other methods detect the status of some
    other special keys such as Alt, Shit and Control.

4
Keyboard events
  • The KeyListener creates events as each key is
    pressed.Three Event handler methods.keyPressed(K
    eyEvent e) when key is pressedkeyReleased(KeyE
    vent e) when key is releasedkeyTyped(KeyEvent e)
    when key is pressed and released.The
    event object has methods to identify each key
    either by its char or by its unicode
    value.getKeyChar() Returns the char of the key
    pressed.
  • getKeyCode() Returns the unicode value of the
    key pressed.Non printable keys are referenced
    by a constant attached to the object.eg the Tab
    key is VK_TAB.

5
Packages
  • You have used the import statement at the head
    of your programs. This tells the compiler where
    to find classes it needs.
  • Classes are grouped into a package and stored
    together stored in a directory with the same name
    as the package.
  • The core classes of java belong to the java.lang
    package. These are imported by default.
  • Classes are assigned to a package by including
    the package keyword followed by the package name
    at the head of the class.

6
Packages (2)
  • package stats
  • class statistics
  • -----------

Directory ./stats
package stats class graphs -----------
package stats class tests -----------
Files are compiled together using javac .java
7
Packages (3)
  • Future programs can import the stats package
    and have access to its classes.
  • Your programs must be able to find these classes.
    The path to them is held in the environment
    variable CLASSPATH . This is operating system
    dependent.
  • To add c\mypackages to the classpath for
    Windows, in DOS, set CLASSPATHc\myPackagescl
    asspath means retain the existing classpath and
    add to it. To see current settings enter set
    orecho CLASSPATH
  • To add /mypackages to the classpath for Unix
    enter CLASSPATHCLASSPATH/mypackages
  • export CLASSPATHTo see the current settings
    enter env or echo CLASSPATH

8
Encapsulation, Inheritance and Packages
  • There are 4 levels of encapsulation indicated by
    the modifiers public, private and
    protected. The fourth level indicated by no
    modifier is equivalent to the package modifier.

9
Encapsulation, Inheritance and Packages(2)
  • Circles indicate 1. P Classes in the same
    package as our class2. S Subclasses of our
    class.

No Modifiers package
P
S
private
P
S
public
protected
10
Encapsulation in the same directory
  • Packages in the same directory are defined to be
    in the same default package.
  • Using variables and methods with no modifier (
    Package ) makes them accessible to each other.
  • If variables and methods have the protected
    modifier they will remain accessible and other
    classes which are subclasses will also have
    access.

11
What classes should be put into packages?
  • Java classes fall into 3 broad functional
    categories
  • Programs. These are very specific in their
    purpose and involve interaction with the user
    through the keyboard and mouse.
  • Utility classes. These are repositories of
    useful methods. Often they are private classes
    with static methods eg. The Math class.
  • Abstract Data Types. These classes define objects
    that can be used by other classes. There is
    little specific purpose included in them but they
    are used by program classes which provide the
    purpose. These are classes that act as type
    definitions

12
What classes in packages (2)
  • Only the second two should be placed in packages
    for reuse by other programs.
  • Package classes should be general purpose, not
    tied to the requirements of a particular
    application.
  • As utility classes are generally part of the Java
    API we will only discuss user defined Abstract
    Data Types here.

13
Abstract Data Types, ADTs
  • When we write ADTs we are continuing the work of
    the Java developers by extending the API.
  • If we want to publish these for use by others
    then we must follow standards that preserve the
    quality and accessibility characteristic of other
    java ADTS.

14
Abstract Data Types (2)
  • Principles for ADTs
  • Encapsulation. Hide all non-essential data and
    methods using modifiers. Use getter and setter
    methods when necessary
  • Handle Exceptions. Define and use exception
    classes and throw them when errors occur.
  • Avoid IO. Minimise input and output to screen and
    files.
  • Modularity. Make the class as independent of
    other user-defined classes as is possible
  • Generality. Avoid hard coding parameters, make
    them configurable through the constructors.
  • Documentation. Use the javadoc utility to
    produce html documentation in the Java API format.

15
ADTs and Packages
  • Place ADTs that share a common theme and may have
    mutual dependencies in the same package.

16
Deploying projects
  • A Java Project may consist of a number of classes
    along with ancillary files such as images, text
    files and Html files.
  • When making this suite of programs available to
    others it can become difficult to deploy and
    support if it depends on number of disparate
    files that could be moved or deleted.
  • Java provides a utility to bundle a project into
    a single file which can be distributed and
    executed.

17
Jar Files
  • The Java jar utility (java archive) is modelled
    on the Unix tar (tape archive) utility.
  • It allows us to collect a number of files into a
    single compressed file for deployment.
  • Jar files can be viewed using Winzip
  • The jar file contains a manifest file which
    records information about the contents.
  • To create a jar file
  • jar cf MvReg.jar MvReg.class Vehicle.class
    MotBike.class Comm.class Private.class

18
Jar Files (2)
  • The file stored in the .jar file include a
    manifest file. This must be edited to nominate
    which of the classes contains the main method.
  • Create a text file mm.txt which contains two
    lines Main-class MvReg Sealed true
  • Update the manifest by entering jar uvmf
    mm.txt MvReg.jar
  • The .jar file can be executed as follows java
    jar MvReg.jar

19
Three more layout managers
  • 1.CardLayoutThis arranges components onto a
    series of cards that overlay each other. Only one
    card can be seen at a time. Methods are provided
    to flip through the cards.
  • Constructor - a simple default constructor.
    CardLayout cl new CardLayout()
  • Components are added using the add
    method.cl.add(new Jpanel(), Card1)Each card is
    given a name to identify it with. Here it is
    Card1.
  • Displaying cards. These methods are provided to
    display cards.first(cl) Shows the first card
    in the container cllast(cl) Shows the last
    card in the containernext(cl) Shows the next
    card in the containerprevious(cl) Shows the
    previous card in the container show(cl,Card1)
    Shows the named card in the container
  • Can be used for simple animation

20
CardLayout
Card1
Card1
Card1
Card1
Card1
Load
Exit
21
GridBag Layout
  • Like a GridLayout but components can occupy more
    than one grid cell

Rows and Columns are dynamically determined based
on the preferred sizes of components placed in
the layout
22
GridBag (2)
  • Each component has a GridBagConstraints object
    associated with it as it is added to the
    container.
  • The GridBagConstraints (GBC) object has
    properties that determine where and how the
    component is added.The GBC properties can be
    changed before each component is added to tailor
    settings for individual components. GBC
    properties aregridx, gridy The row and col of
    the cell in which the
    component will be placed. gridwidth,
    gridheight The number of cells
    across and down reserved for the
    component.

23
GridBag Layout
  • GBC Properties continued.
  • Fill GridBagConstraints.NONE or
    VERTICAL, HORIZONTAL
  • Sets how the component will
    resize if it is smaller than the
    grids reserved for it (display area).
  • Anchor Sets the location of a component in its
    display area when it is smaller
    than the area and has a fill of
    NONE. Constants CENTER,NORTH,NORTHEA
    ST, EAST etc.These properties are Class
    variables so any changes apply to all GBCs on all
    components.

24
No Layout
  • Components are placed in Absolute pixel
    positions. These do not change when frames are
    re-sized.
  • Each component has a setBounds() method which is
    used to set its top left coordinates and height
    and width in pixels.
  • To use we must first specify no layout for the
    container c using c.setLayout(null)then
    declare and add the component and setBounds.
  • JButton b new
    JButton(Exit)
  • c.add(b)
    b.setBounds(100,120,50,20)
  • Components retain their sizes when the frame
    resizes. Part of the component is obscured when
    the screen is too small.
Write a Comment
User Comments (0)