You are on page 1of 31

JAVA Applets

Applets and applications

An applet is a Java program that runs on a web page

Applets can be run within any modern browser To run modern Java applets, old browsers need an up-to-date Java plugin appletviewer is a program that can run java applets.

Packages and classes

Java supplies a huge library of pre-written code, ready for you to use in your programs Code is organized into classes Classes are grouped into packages One way to use this code is to import it You can import a single class, or all the classes in a package

The Applet class

To create an applet, you must import the Applet class

This class is in the java.applet package

The Applet class contains code that works with a browser to create a display window Capitalization matters!

applet and Applet are different names

Importing the Applet class

Here is the directive that you need:


import java.applet.Applet;

The java.awt package

awt stands for Abstract Window Toolkit The java.awt package includes classes for:

Drawing lines and shapes Drawing letters Setting colors Choosing fonts

If its drawn on the screen, then java.awt is probably involved!

Importing the java.awt package

Since you may want to use many classes from the java.awt package, simply import them all:
import java.awt.*;

The asterisk, or star (*), means all classes The import directives can go in any order, but must be the first lines in your program

The applet so far


import java.applet.Applet; import java.awt.*;

Your applet class


public class Drawing extends Applet { } Drawing is the name of your class

Class names should always be capitalized

extends Applet says that our Drawing is a kind of Applet, but with added capabilities

Javas Applet just makes an empty window We are going to draw in that window

The only way to make an applet is to extend Applet

The applet so far


import java.applet.Applet; import java.awt.*; public class Drawing extends Applet { we still need to put some code in here... }

10

The paint method

Our applet is going to have a method to paint some colored rectangles on the screen This method must be named paint paint needs to be told where on the screen it can draw

This will be the only parameter it needs

paint doesnt return any result

11

The paint method, part 2

public void paint(Graphics g) { }


public says that anyone can use this method void says that it does not return a result

A Graphics (short for Graphics context) is an object that holds information about a painting

It remembers what color you are using It remembers what font you are using You can paint on it (but it doesnt remember what you have painted)

12

The applet so far


import java.applet.Applet; import java.awt.*; // CIT 591 example public class Drawing extends Applet { public void paint(Graphics g) { we still need to put some code in here } }

13

Colors

The java.awt package defines a class named Color There are 13 predefined colorshere are their fullyqualified names:
Color.PINK Color.RED Color.ORANGE Color.YELLOW Color.MAGENTA Color.GREEN Color.CYAN Color.BLUE

Color.BLACK Color.DARK_GRAY Color.GRAY Color.LIGHT_GRAY Color.WHITE

For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color.black, Color.darkGray, etc.

14

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) Yellow is red + green, or (255, 255, 0)

15

Using Colors
Color Constructors
Color(int red, int green, int blue) Color(int rgb) Color(float r, float g, float b)
import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; /* <applet code="BlueString" width=300 height=100> </applet> */ public class BlueString extends Applet {

static int HSBtoRGB(float h, float s, float b) static float[] RGBtoHSB(int r, int g, int b, float hsb[]) Color brighter() Color darker() static Color decode(String str) throws NumberFormatExcepti on boolean equals(Object obj) int getBlue() int getGreen() int getRGB() int getRed()

public void paint(Graphics g) { g.setColor(Color.blue); g.drawString("Blue String", 100, 50); }

http://java.sun.com/javase/6/docs/api/java/awt/Color.html

16

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

17

The paint method so far


public void paint(Graphics g) { g.setColor(Color.BLUE); draw a rectangle g.setColor(Color.RED); draw another rectangle } }

18

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

java.awt measures everything in pixels

19

Javas coordinate system


(0, 0) (0, 20) (50, 0) (50, 20)

(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
20

Drawing rectangles

There are two ways to draw rectangles: g.drawRect( left , top , width , height );

g.fillRect(left , top , width , height );

21

The complete applet


import java.applet.Applet; import java.awt.*; // CIT 591 example 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); } }
22

Some more java.awt methods

g.drawLine( x1 , y1 , x2 , y2 ); g.drawOval( left , top , width , height ); g.fillOval( left , top , width , height ); g.drawRoundRect( left , top , width , height ); g.fillRoundRect( left , top , width , height ); g.drawArc( left , top , width , height , startAngle , arcAngle ); g.drawString( string , x , y );

23

The Graphics Class


Methods of Graphics Class
abstract void drawArc(int x, int y, int w, int h, int degreesO, i nt degrees1) abstract boolean drawImage(Image img, int x, int y, Image Observer io) abstract void drawLine(int x0, int y0, int x, int y1) abstract void drawOval(int x, int y, int w, int h) abstract void drawPolygon(int x[], int u[], int n) abstract void drawPolyline(int x[], int y[], int n) void drawRect(int x, int y, int w, int h) abstract void drawString(String str, int x, int y) abstract void fillArc(int x, int y, int w, int h, int degree0, int degree1) abstract void fillOval(int x ,int y, int w, int h) abstract void fillPolygon(int x[], int y[], int n) void fillRect(int x, int y, int w, int h) abstract Color getColor() abstract Font getFont() abstract FontMetrics getFontMetrics() abstract void setColor(Color c) abstract void setFont(Font f)
import java.applet.Applet; import java.awt.Graphics; /* <applet code="DrawArc" width=200 height=200> </applet> */ pulic class DrawArc extends Applet { public void paint(Graphics g) { g.drawArc(20, 20, 160, 160, 0, 135); }

http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html

24

Displaying Text
Font Constructor
Font(String name, int style, int ps)

setFont() Method
abstract void setFont(Font font)

FontMetrics Constructor
FontMetrics(Font font)

http://java.sun.com/javase/6/docs/api/java/awt/Font.html

25

Using Applet Dimensions


getSize() Method
Dimension getSize()

Dimension Constructors
Dimension() Dimension(Dimension d) Dimension(int w, int h)

http://java.sun.com/javase/6/docs/api/java/awt/Dimension.html

26

Using Applets in a Web Page


Applet HTML Tag
<applet [codebase=url] [alt=text] [name=appName] width=wpixels height=hpixels [align=alignment] [vspace=vspixels] [hspace=hspixels] > [<param name=pname1 value=value1>] [<param name=pname2 value=value2>] . [<param name=pnameN value=valueN>] </applet>
import java.applet.*; import java.awt.*; /* <applet code="AppletParameters" width=300 height=300> <param name="background" value="0xffffff"> <param name="foreground" value="0x000000"> <param name="message" value="Testing Applet Parameters"> </applet> */ public class AppletParameters extends Applet { public void paint(Graphics g) { String background = getParameter("background"); String foreground = getParameter("foreground"); String message = getParameter("message"); setBackground(Color.decode(background)); setForeground(Color.decode(foreground)); Font font = getFont(); FontMetrics fm = getFontMetrics(font); Dimension d = getSize(); int x = (d.width - fm.stringWidth(message)) / 2; int y = d.height / 2; g.drawString(message, x, y); }

27

The Applet Class


java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet Applet and its superclasses
28
} import java.applet.*; import java.awt.*; /* <applet code="BackgroundForeground" width=200 height=200> </applet> */ public class BackgroundForeground extends Applet { public void paint(Graphics g) { setBackground(Color.yellow); setForeground(Color.blue); g.drawLine(0, 0, 200, 200); g.fillRect(100, 40, 50, 50); }

The Life Cycle of an Applet


import java.applet.Applet; import java.awt.Graphics; /* <applet code="AppletLifecycle" width=300 height=50> </applet> */ public class AppletLifecycle extends Applet { String str = ""; public void init() { str += "init; "; } public void start() { str += "start; "; } public void stop() { str += "stop; "; } public void destroy() { System.out.println("destroy"); } public void paint(Graphics g) { g.drawString(str, 10, 25); }

init() : called only when the applet begins execution. start() : executed after init() method. Called by the applet viewer or Web browser. stop() : when applet viewer is minimized. destroy() : called by the applet viewer or Web browser before the applet is terminated.

Action /Event Listeners


Now that you know how to display things on screen you need a way to responds to the user actions. This is done with the EventListeners. An Event is a user interaction. Reacting on GUI cmponents is done by adding ActionListeners: MouseListener MouseMotionListener KeyboardListener (but it is not a common class you will use.)

30

Thank you!

31

You might also like