Académique Documents
Professionnel Documents
Culture Documents
Java Applets
Budditha Hettige Department of Statistics and Computer Science
Budditha Hettige
Applets contd.
Program that runs in
appletviewer (test utility for applets) Web browser (IE, Communicator)
Executes when HTML (Hypertext Markup Language) document containing applet is opened and downloaded Applications run in command windows
java.awt.Panel
java.applet.Applet javax.swing.JApplet
Budditha Hettige
Budditha Hettige
Sample Program
import java.awt.Graphics; import javax.swing.JApplet; // import class Graphics // import class JApplet public class WelcomeApplet extends JApplet { // draw text on applets background public void paint( Graphics g ) { // call superclass version of method paint super.paint( g ); // draw a String at x-coordinate 25 and y-coordinate g.drawString( "Welcome to Java Programming!", 25, 25 ); } // end method paint } // end class WelcomeApplet
To execute an applet
Create an HTML file indicating which applet the browser (or appletviewer) should load and execute 5
Budditha Hettige
Budditha Hettige
Execute an applet
appletviewer only understands <applet> tags
Ignores everything else Minimal browser
2 3 4
<html> <applet code = "WelcomeApplet.class" width = "300" height = "45"> </applet> </html>
Lines 1 and 4 - begin and end the HTML tags Line 2 - begins <applet> tag
Specifies code to use for applet Specifies width and height of display area in pixels
Budditha Hettige
Budditha Hettige
Colors
The java.awt package defines a class named Color There are 13 predefined colorshere are their fully-qualified names:
Color.BLACK Color.DARK_GRAY Color.GRAY Color.LIGHT_GRAY Color.WHITE Color.PINK Color.RED Color.ORANGE Color.YELLOW Color.MAGENTA Color.GREEN Color.CYAN Color.BLUE
Budditha Hettige
10
Colors
New Colors
Every color is a mix of red, green, and blue You can make your own colors: new Color( red , green , blue ) Amounts range from 0 to 255 Black is (0, 0, 0), white is (255, 255, 255) We are mixing lights, not pigments Yellow is red + green, or (255, 255, 0)
Pixels
A pixel is a picture (pix) element
one pixel is one dot on your screen there are typically 72 to 90 pixels per inch
Setting a color
To use a color, we tell our Graphics g what color we want:
g.setColor(Color.RED);
g will remember this color and use it for everything until we tell it some different color
Budditha Hettige
11
Budditha Hettige
12
Drawing rectangles
There are two ways to draw rectangles:
g.drawRect( left , top , width , height );
(w-1, h-1)
Java uses an (x, y) coordinate system (0, 0) is the top left corner (50, 0) is 50 pixels to the right of (0, 0) (0, 20) is 20 pixels down from (0, 0) (w - 1, h - 1) is just inside the bottom right corner, where w is the width of the window and h is its height
Budditha Hettige
13
Budditha Hettige
14
Sample Applet
import java.applet.Applet; import java.awt.*; public class Drawing extends Applet { public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillRect(20, 20, 50, 30); g.setColor(Color.RED); g.fillRect(50, 30, 50, 30); } }
Budditha Hettige
g.drawString( string , x , y );
15
Budditha Hettige
16
Applet Methods
public public public public public public public public public void void void void void init () start () stop () destroy () paint (Graphics)
java .a wt.Pa nel
} }
Budditha Hettige
17
Budditha Hettige
18
init()
z
z
Called exactly once in an applets life. Called when applet is first loaded, which is after object creation, e.g., when the browser visits the web page for the first time. Used to read applet parameters, start downloading any other images or media files, etc.
Called at least once. Called when an applet is started or restarted, i.e., whenever the browser visits the web page. Called at least once. Called when the browser leaves the web page.
Budditha Hettige
destroy()
z z z
Called exactly once. Called when the browser unloads the applet. Used to perform any final clean-up.
init
start()
z z
start
stop()
z z
stop
destroy
start
19
Budditha Hettige
20
Applet on NetBeans
Budditha Hettige
21
Budditha Hettige
22
Sample Programs
C:/Program Files/Java/jdk1.6.0_23/demo/applets/
Budditha Hettige
23