Introduction To Applets - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Introduction To Applets

Description:

put in a file called MyApplet.java. The import statement pulls in a ... Java's Coordinate System. x axis. y. a. x. i. s (0, 0) Colors. There is a Color class ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 34
Provided by: jeffch8
Category:

less

Transcript and Presenter's Notes

Title: Introduction To Applets


1
Introduction To Applets
  • (Finally!)

2
Overview
  • Applet Skeleton
  • Callback methods
  • HTML Overview
  • The Graphics class
  • 2D coordinate system
  • Drawing primitive shapes
  • Colors
  • Text
  • Images and Sounds

3
What you should know(before starting applets)
  • Good grasp of classes
  • Accessors
  • Modifiers
  • Calling methods of classes (dot operator)
  • Functions
  • Data Passing

4
Notes about Applets
  • Security
  • Live in a sandbox
  • Cant read/write files
  • No communication over the net
  • Web-based
  • No main to start things off (callback based)

5
The Applet Skeleton
  • import java.awt.
  • public class MyApplet extends java.applet.Applet

6
Remember
  • the class must be a public class!
  • you inherit from the Applet class
  • put in a file called MyApplet.java
  • The import statement pulls in a graphics package
  • Graphics, Buttons, Textfields
  • Applets have attributes too!

7
Callback Methods
  • Event-based programming
  • Mouse clicked
  • Button pressed
  • Mouse moved
  • Window resized
  • Web-browser looks for certain functions in your
    applet to handle certain situations
  • init (used for initializing attributes)
  • paint (guess what this function does)

8
public void init ( )
  • Inherit this method from java.applet.Applet
  • Called exactly once
  • Called at the beginning of the lifetime of the
    applet (a lot like a constructor)
  • Used to initialize attributes
  • Load large files (audio/images)
  • Bring attributes to life
  • Set layout of Buttons, Textfields

9
public void paint (Graphics g)
  • Inherit this method from java.applet.Applet
  • Used any time the applet needs to redraw itself
  • The first time the applet is run
  • The applet is resized, minimized, maximized
  • Graphics object passed as a parameter
  • Tell the graphics object what to do

10
Graphics Methods
  • void drawLine (int x1, int y1, int x2, int y2)
  • void drawOval (int x1, int y1, int width, int
    height)
  • void drawRect (int x1, int y1, int width, int
    height)
  • void setColor (Color c)
  • void setFont (Font f)
  • void drawString (String s, int x, int y)
  • .
  • .
  • .

11
2D Coordinate System(x over, y up)
y a x i s
(, )
(-, )
x axis
(-, -)
(, -)
12
Javas Coordinate System
(0, 0)
x axis
y a x i s
13
Colors
  • There is a Color class
  • Two ways to initialize a color
  • Use new (call to constructor with RGB)
  • Use static colors (Color.green, Color.blue)
  • Constructor has ranges of (0-255) or (0.01.0)
  • Example
  • Color myColor, yourColor
  • myColor Color.red //myColor is red
  • yourColor new Color (255, 0, 0) //yourColor
    is red

14
Example(What does this do?)
  • import java.awt.
  • public class MyApplet extends java.applet.Applet
  • Color myColor // An attribute
  • public void init ( )
  • myColor new Color (255, 0, 255) // purple
  • public void paint (Graphics screen)
  • screen.setColor (myColor)
  • screen.drawLine (0, 0, 200, 200)

15
Output
16
Basic HTML
  • Necessary to learn a little HTML
  • Applets run in web pages, remember?
  • Embed the applet into the web page
  • This is the language you make web pages with
    (HyperText Markup Language)
  • Things between the lt and the gt are called tags
  • There are four tags you need to know

17
The Most Simple Web Page
  • ltHTMLgt // Means begin the web page
  • lt/HTMLgt // Means end the web page
  • This is a pair of tags
  • It is not case-sensitive (unlike Java)
  • lthTMlgt is the same as ltHtMlgt
  • Put things you want in the web page between
    lthtmlgt and lt/htmlgt
  • Save as a .html file

18
Our Applet Web Page
  • lthtmlgt
  • ltapplet codeMyApplet.class width300
    height300gt
  • lt/appletgt
  • lt/htmlgt

19
Skeleton of a Web Page
  • lthtmlgt
  • ltapplet codeMyApplet.class width300
    height300gt
  • lt/appletgt
  • lt/htmlgt

20
Include the Applet
  • lthtmlgt
  • ltapplet codeMyApplet.class width300
    height300gt
  • lt/appletgt
  • lt/htmlgt

21
Which code to run
  • lthtmlgt
  • ltapplet codeMyApplet.class width300
    height300gt
  • lt/appletgt
  • lt/htmlgt

22
How Big the Applet Is
  • lthtmlgt
  • ltapplet codeMyApplet.class width300
    height300gt
  • lt/appletgt
  • lt/htmlgt

23
Images and Sounds
  • Very similar in behavior
  • Declare attribute (either Image or AudioClip)
  • Load the information (getImage or getAudioClip)
  • Play or display
  • Remember to load the information in init ( )
    method
  • Keeps from playing/displaying incomplete info
  • init is guaranteed to be called

24
Images
  • Draw the image in the paint method
  • Can draw .jpg and .gif format
  • No .bmp (bitmaps)
  • Two Formats
  • public void paint (Graphics g)
  • // Draw normal size
  • g.drawImage (picture, 10, 10, this)
  • // Force pic to be 100 x 100
  • g.drawImage (picture, 10, 10, 100, 100, this)

25
Audio
  • Need to import java.applet.AudioClip
  • File Formats
  • .wav
  • .au (good for web)
  • .mid
  • Different methods of AudioClip
  • play ( ) // play one time
  • loop( ) // repeat over and over
  • stop ( ) // stop playing
  • Can also use the play( ) command for one-shot
    deal
  • play (getCodeBase( ), mySound.wav)

26
Start With Skeleton
  • import java.awt.
  • public class MediaApplet extends
    java.applet.Applet

27
Put in init( ) and paint( )
  • import java.awt.
  • public class MediaApplet extends
    java.applet.Applet
  • public void init ( )
  • public void paint (Graphics g)

28
Declare Attributes
  • import java.awt.
  • public class MediaApplet extends
    java.applet.Applet
  • Image thePicture
  • AudioClip myClip
  • public void init ( )
  • public void paint (Graphics g)

29
Remember to import!
  • import java.awt.
  • import java.applet.AudioClip
  • public class MediaApplet extends
    java.applet.Applet
  • Image thePicture
  • AudioClip myClip
  • public void init ( )
  • public void paint (Graphics g)

30
Load the Media
  • import java.awt.
  • import java.applet.AudioClip
  • public class MediaApplet extends
    java.applet.Applet
  • Image thePicture
  • AudioClip myClip
  • public void init ( )
  • thePicture getImage (getCodeBase( ),
    thePic.gif)
  • myClip getAudioClip (getCodeBase( ),
    scream.au)
  • public void paint (Graphics g)

31
Produce the Media
  • import java.awt.
  • import java.applet.AudioClip
  • public class MediaApplet extends
    java.applet.Applet
  • Image thePicture
  • AudioClip myClip
  • public void init ( )
  • thePicture getImage (getCodeBase( ),
    thePic.gif)
  • myClip getAudioClip (getCodeBase( ),
    scream.au)
  • public void paint (Graphics g)
  • g.drawImage (thePicture, 10, 10, this)
  • myClip.play ( )

32
Summary
  • Applets have skeletons (go ahead and memorize)
  • Applets have callback functions (no main)
  • The paint method is used for drawing images and
    primitive shapes
  • Applets are very secure
  • Applets run in a web page, so you need to make an
    .html file for them

33
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com