Introduction to Java Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Java Programming

Description:

G. Cornell and C. Horstmann, 'Core Java', The Sunsoft Press Java Series, Second Edition, 1997 ... Perkins, 'Teach yourself Java in Caf in 21 days', Sams.net ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 14
Provided by: Chira5
Learn more at: http://www.cse.msu.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Java Programming


1
Introduction to Java Programming
  • An object-oriented, platform independent language
  • Two types of programs in Java
  • Applets (run from web browsers)
  • Command line programs
  • Primary focus of this lab
  • Event Handling mechanisms of Java
  • Developing GUI prototypes in Java

2
To Compile and run Java Applets
  • Include path
  • setenv PATH PATH/usr/java1.2/bin in your
    .personal file
  • Create java program file programfile.java using
    any editor
  • Compile Java program
  • javac programfile.java
  • This creates a file with .class extension,
    programfile.class
  • Create .html file for appletviewer, run
    appletviewer
  • appletviewer programfile.html
  • A sample programfile.html is given on the next
    slide

3
Sample .html file for Appletviewer
  • ltbodygt
  • lthtmlgt
  • ltapplet code programfile.class width200
    height200gt
  • lt/appletgt
  • lt/htmlgt
  • lt/bodygt

4
Event Handling in Java
  • Graphical User Interfaces are event-driven
  • To process a GUI event, a programmer should
  • register an event listener
  • implement an event handler
  • Number of different event classes and
    event-listener interfaces defined in the package
    java.awt.event

5
Abstract Interfaces, Delegation
  • Event-listener interfaces define only abstract
    methods
  • Programmer must define all methods of an
    interface in a class that implements the
    interface
  • Event-listener objects for an event are created
    from the class that implements the required
    interface
  • Event handling model of Java is known as
    delegation event model because event processing
    is delegated to particular objects

6
Delegation Event Model
Register with source
Event Source
Event Listener
Event Object e
Ex Button, Canvas, Text Box
An object of a class that will implement methods
which will respond to events
7
Delegation Event Model
  • Key Components of the delegation event model
  • Event Source (usually GUI object)
  • Event Listener object
  • Event object
  • Event registration

8
Event Listeners
  • Some Event Listener interfaces of java.awt.event
  • ActionListener
  • MouseListener
  • MouseMotionListener
  • Examples of classes to implement interfaces
  • Class mylistener implements ActionListener
  • / The following method is called when a Button
    is
  • pressed. /
  • public void actionPerformed ( ActionEvent e )
    /event handler code goes here /

9
Event Listeners (contd.)
  • Example of a listener for multiple events
  • Class mylistener implements MouseListener
  • / Following method is called when the mouse
    button is clicked /
  • public void mouseClicked(MouseEvent evt)
    .
  • / Called when the mouse button is
    depressed /
  • public void mousePressed(MouseEvent evt)
    ..
  • / Called when the mouse button is released /
  • public void mouseReleased(MouseEvent evt)
    ..

10
Event Objects
  • Event Objects carry information about the event
  • Event Listeners obtain information from Event
    Sources through Event Objects
  • Example MouseEvent Evt
  • Evt.getX() --- gets the X-coordinate of mouse
    click
  • Evt.getY() --- gets the Y-coordinate of mouse
    click
  • Evt.getSource() --- gets the source object of the
    mouse click event (like a button)

11
Event Registration
  • A Listener object has to register itself with an
    Event Source to catch events on the source
  • Example
  • Button1.addActionListener( new mylistener1() )
  • Button1.addMouseListener( new mylistener2() )
  • Multiple Listeners can listen to a single event
    source (for example, a KeyListener and a
    MouseListener could both listen to the same
    source)
  • A single Listener can listen to multiple event
    sources

12
Example -- Simple Action Event
  • public class SimpleEvent extends Applet
  • Button button1 new Button("Press me")
  • public void init()
  • this.add(button1)
  • button1.addActionListener(new mylistener())
  • Class mylistener implements ActionListener
  • public void actionPerformed(ActionEvent e)
  • if(e.getSource() button1)
  • button1.setLabel("Again")

13
References
  • G. Cornell and C. Horstmann, Core Java, The
    Sunsoft Press Java Series, Second Edition, 1997
  • D. Joshi, L. Lemay, and C. Perkins, Teach
    yourself Java in Café in 21 days, Sams.net
    publishing, 1996
  • The Java Tutorial
  • http//java.sun.com/docs/books/tutorial/index.htm
  • D. Geary and A. McClellan, Graphic Java, The
    Sunsoft Press Java Series, 1997
  • Deitel Deitel, Java How To Program Third
    Edition, Prentice Hall Inc.
Write a Comment
User Comments (0)
About PowerShow.com