Applet Methods - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Applet Methods

Description:

A event is an object that represents some occurance which we ... private Image up, down, right, left, currentImage; private AudioClip bonk; private int x, y; ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 40
Provided by: valued54
Category:

less

Transcript and Presenter's Notes

Title: Applet Methods


1
Applet Methods
  • public void init()
  • public void start()
  • public void stop()
  • public void destroy()
  • public URL getCodeBase()
  • public URL getDocumentBase()
  • public AudioClip getAudioClip(URL url,
  • String name)
  • public Image getImage(URL url,
  • String name)

2
Events and Listeners
  • A event is an object that represents some
    occurance which we may want to handle.
  • Often generated by user actions keyboard, mouse,
    etc.
  • Can also can be generated by other programs.
  • There are different types of events represented
    by different classes MouseEvent, ActionEvent,
    etc.

3
Events and Listeners
  • A listener is an object that waits for and
    responses to events.
  • There are different types of listeners
    represented by different listener interfaces
    MouseListener, ActionListener, etc.
  • A listener class should implement one of the
    listener interfaces.

4
Events and Listeners
When an event occurs, the generator calls the
appropriate method of the listener, passing an
object that describes the event
5
Interface MouseListener
  • void mousePressed(MouseEvent event)
  • void mouseReleased(MouseEvent event)
  • void mouseClicked(MouseEvent event)
  • void mouseEntered(MouseEvent event)
  • void mouseExited(MouseEvent event)

6
Class MouseEvent
  • Point getPoint()
  • int getX()
  • int getY()
  • int getClickCont()

7
Example Dots.java
import java.applet.Applet import
java.awt. public class Dots extends Applet
private final int APPLET_WIDTH 200 private
final int APPLET_HEIGHT 100 private final
int RADIUS 6 private Point clickPoint
null public void init()
DotsMouseListener listener new
DotsMouseListener(this) addMouseListener(list
ener) setBackground(Color.black)
setSize(APPLET_WIDTH, APPLET_HEIGHT)
8
Example Dots.java
public void paint(Graphics page)
page.setColor (Color.green) if (clickPoint
! null) page.fillOval(clickPoint.x -
RADIUS, clickPoint.y -
RADIUS, RADIUS 2, RADIUS
2) public void setPoint(Point point)
clickPoint point
9
Example DotsMouseListener.java
import java.applet.Applet import
java.awt. import java.awt.event. class
DotsMouseListener implements MouseListener
private Dots applet public DotsMouseListener(Do
ts applet) this.applet applet
public void mouseClicked(MouseEvent event)
Point clickPoint event.getPoint()
applet.setPoint (clickPoint)
applet.repaint() public void
mousePressed(MouseEvent event) public void
mouseReleased(MouseEvent event) public void
mouseEntered(MouseEvent event) public void
mouseExited(MouseEvent event)
10
Interface MouseMotionListener
  • void mouseMoved(MouseEvent event)
  • void mouseDragged(MouseEvent event)

11
Example RubberLines.java
import java.applet.Applet import
java.awt. import java.awt.event. public class
RubberLines extends Applet implements
MouseListener, MouseMotionListener private
final int APPLET_WIDTH 200 private final int
APPLET_HEIGHT 200 private Point point1
null private Point point2 null public
void init() addMouseListener(this)
addMouseMotionListener(this)
setBackground(Color.black)
setSize(APPLET_WIDTH, APPLET_HEIGHT)
12
Example RubberLines.java

public void paint(Graphics page)
page.setColor (Color.green) if (point1 !
null point2 ! null) page.drawLine
(point1.x, point1.y,
point2.x, point2.y) public void
mousePressed(MouseEvent event) point1
event.getPoint() public void
mouseDragged(MouseEvent event) point2
event.getPoint() repaint()
13
Example RubberLines.java
public void mouseClicked(MouseEvent event)
public void mouseReleased(MouseEvent event)
public void mouseEntered(MouseEvent event)
public void mouseExited(MouseEvent event)
public void mouseMoved(MouseEvent event)
14
Handling Key Presses
  • Interface KeyListener
  • void keyPressed(KeyEvent event)
  • void keyReleased(KeyEvent event)
  • void keyTyped(KeyEvent event)
  • Class KeyEvent
  • int getKeyCode()
  • constants for all keys

15
Example Direction.java
import java.applet. import java.awt. import
java.awt.event. public class Direction extends
Applet private final int APPLET_WIDTH 200
private final int APPLET_HEIGHT 200 private
final int JUMP 5 // increment for image
movement private final int IMAGE_SIZE 31
private Image up, down, right, left,
currentImage private AudioClip bonk private
int x, y
16
Example Direction.java
public void init() requestFocus()
// make sure the applet has the keyboard focus
addKeyListener(new DirectionKeyListener())
x y 0 up getImage (getCodeBase(),
"cyanUp.gif") down getImage
(getCodeBase(), "cyanDown.gif") left
getImage (getCodeBase(), "cyanLeft.gif")
right getImage (getCodeBase(),
"cyanRight.gif") currentImage right
bonk getAudioClip (getCodeBase(), "bonk.au")
setBackground (Color.black) setSize
(APPLET_WIDTH, APPLET_HEIGHT) public void
paint (Graphics page) page.drawImage
(currentImage, x, y, this)
17
Example Direction.java
private class DirectionKeyListener
implements KeyListener public void
keyPressed (KeyEvent event) switch
(event.getKeyCode()) case
KeyEvent.VK_UP currentImage up
if (y gt 0) y - JUMP break case
KeyEvent.VK_DOWN currentImage down
if (y lt APPLET_HEIGHT-IMAGE_SIZE)
y JUMP break case
KeyEvent.VK_LEFT currentImage left
if (x gt 0) x - JUMP break
18
Example Direction.java
case KeyEvent.VK_RIGHT
currentImage right if (x lt
APPLET_WIDTH-IMAGE_SIZE) x JUMP
break default bonk.play()
repaint() public void
keyTyped (KeyEvent event) public void
keyReleased (KeyEvent event)
19
Animation
  • An animation is a constantly changing series of
    pictures or images that create the illusion of
    movement
  • We can create animations in Java by changing a
    picture slightly over time
  • The speed of a Java animation is usually
    controlled by a Timer object
  • Timer class
  • Timer(int delay, ActionListener listener)
  • void addActionListener(ActionListener listener)
  • boolean isRunning()
  • void start()
  • void stop()

20
Example Rebound.java
import java.applet.Applet import
java.awt. import java.awt.event. import
javax.swing.Timer public class Rebound extends
Applet private final int APPLET_WIDTH 200
private final int APPLET_HEIGHT 100 private
final int IMAGE_SIZE 35 private final int
DELAY 20 private Timer timer private
Image image private int x, y, moveX, moveY
21
Example Rebound.java
public void init() addMouseListener(new
ReboundMouseListener()) timer new
Timer(DELAY, new
ReboundActionListener()) timer.start()
x 0 y 40 moveX moveY 3
image getImage(getCodeBase(),
"happyFace.gif") setBackground(Color.black)
setSize(APPLET_WIDTH, APPLET_HEIGHT)
public void paint(Graphics page)
page.drawImage(image, x, y, this)
22
Example Rebound.java
private class ReboundMouseListener
implements MouseListener public void
mouseClicked(MouseEvent event) if
(timer.isRunning()) timer.stop()
else timer.start() public void
mouseEntered(MouseEvent event) public void
mouseExited(MouseEvent event) public void
mousePressed(MouseEvent event) public void
mouseReleased(MouseEvent event)
23
Example Rebound.java
private class ReboundActionListener
implements ActionListener public void
actionPerformed(ActionEvent event) x
moveX y moveY if (x lt 0 x gt
APPLET_WIDTH-IMAGE_SIZE) moveX moveX
-1 if (y lt 0 y gt APPLET_HEIGHT-IMAGE_S
IZE) moveY moveY -1
repaint()
24
Arrays
  • An array is an ordered list of values, which are
    of the same type. That type can be primitive
    types or reference types (class or interface).
  • Arrays are objects. Array types are reference
    types.
  • Arrays are of fixed size and are always bound
    checked.
  • The length of array ia.length
  • Index starts from 0. An array of size N is
    indexed from 0 to N-1

25
Array Examples
int ia new int3 ia0 1 ia1 2
ia2 3 int ia new int3 ia0 1
ia1 2 ia2 3 int ia 1, 2, 3
float mat new float44 for (int y
0 y lt mat.length y)   for (int x 0 x lt
maty.length x)     matyx 0.0
26
Arrays as Parameters
  • An entire array can be passed to a method as a
    parameter
  • Like any other object, the reference to the array
    is passed, making the formal and actual
    parameters aliases of each other
  • Changing an array element in the method changes
    the original

27
Arrays of Objects
  • The elements of an array can be object references
  • The following declaration reserves space to store
    25 references to String objects
  •  String words new String25  
  • It does NOT create the String objects themselves
  • Each object stored in an array must be
    instantiated separately

28
Example GradeRange.java
public class GradeRange public static void
main (String args) String grades
"A", "A-", "B", "B", "B-",
"C", "C", "C-", "D",
"D", "D-", "F" int cutoff 95, 90, 87,
83, 80, 77, 73, 70,
67, 63, 60, 0 for (int
level 0 level lt cutoff.length level)
System.out.println (gradeslevel "\t"
cutofflevel)
29
Command Line Arguments
public class CommandLineArguments public
static void main(String args) int length
args.length System.out.println("args.lengt
h" length) for (int i 0 i lt length
i) System.out.println("args" i
"" argsi)
30
Command Line ArgumentsOutput
C\Examplesgtjava CommandLineArguments
args.length0 C\Examplesgtjava
CommandLineArguments Hello World! args.length2
args0Hello args1World!
31
Selection Sort
public class Sorts public static void
selectionSort(int numbers) int min,
temp for (int index 0 index lt
numbers.length-1 index) min
index for (int scan index1 scan lt
numbers.length scan) if
(numbersscan lt numbersmin) min
scan // Swap the values temp
numbersmin numbersmin
numbersindex numbersindex temp
//
32
Example SortGrades.java
public class SortGrades public static void
main (String args) int grades 89,
94, 69, 80, 97, 85, 73, 91,
77, 85, 93 Sorts.selectionSort(grades)
for (int index 0 index lt grades.length
index) System.out.print (gradesindex
" ")
33
Sorting Arrays of Objects
public class Sorts public static void
insertionSort(Comparable objects) for
(int index 1 index lt objects.length
index) Comparable key
objectsindex int position index
// shift larger values to the right while
(position gt 0 objectsposition-1
.compareTo(key) gt 0) objectsposition
objectsposition-1 position--
objectsposition key
//
34
Example Contact.java
class Contact implements Comparable private
String firstName, lastName, phone public
Contact(String firstName, String lastName,
String phone) this.firstName
firstName this.lastName lastName
this.phone phone public String toString
() return lastName ", " firstName
"\t" phone
35
Example Contact.java
public int compareTo (Object other) int
result if (lastName.equals(((Contact)other).l
astName)) result
firstName.compareTo(((Contact)other).firstName)
else result
lastName.compareTo(((Contact)other).lastName)
return result
36
Example SortPhoneList.java
public class SortPhoneList public static void
main (String args) Contact friends
new Contact7 friends0 new
Contact("John", "Smith", "610-555-7384")
friends1 new Contact() friends2 new
Contact() friends3 new Contact()
friends4 new Contact() friends5 new
Contact() friends6 new
Contact("Marsha", "Grant", "243-555-2837")
Sorts.insertionSort(friends) for (int index
0 index lt friends.length index)
System.out.println (friendsindex)
37
Vector Class
  • A varaible-sized list of objects.
  • Methods
  • void addElement(Object obj)
  • void insertElementAt(Object obj, int i)
  • void setElementAt(Object obj, int i)
  • Object remove(int i)
  • boolean removeElement(Object obj)
  • void removeElementAt(int i)
  • void clear()
  • boolean contains(Object obj)
  • int indexOf(Object obj)
  • Object elementAt(int i)
  • boolean isEmpty()
  • int size()

38
Example Beatles.java
import java.util.Vector public class Beatles
public static void main (String args)
Vector band new Vector() band.addElement
("Paul") band.addElement ("Pete")
band.addElement ("John") band.addElement
("George") System.out.println (band)
band.removeElement ("Pete")
System.out.println (band) System.out.println
("At index 1 "
band.elementAt(1)) band.insertElementAt
("Ringo", 2) System.out.println (band)
System.out.println ("Size of the band "
band.size())
39
Example Beatles.javaOutput
C\Examplesgtjava Beatles Paul, Pete, John,
George Paul, John, George At index 1
John Paul, John, Ringo, George Size of the
band 4
Write a Comment
User Comments (0)
About PowerShow.com