Applet Example

35
Applet Example 1

description

Applet Example. 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 - PowerPoint PPT Presentation

Transcript of Applet Example

Applet Example

1

Applets and applications

2

An applet is a Java program that runs on a web pageApplets can be run within any modern browserTo run modern Java applets, old browsers need an

up-to-date Java pluginappletviewer is a program that can run

An application is a Java program that runs all by itself

Applet

3

An applet is a program that comes from server into a client and gets executed at client side and displays the result.

An applet represents byte code embedded in a html page. (applet = bytecode + html) and run with the help of Java enabled browsers such as Internet Explorer.

An applet is a Java program that runs in a browser. Unlike Java applications applets do not have a main () method. To create applet we can use java.applet.Applet .

Applet

4

Packages and classes

5

Java supplies a huge library of pre-written “code,” ready for you to use in your programs

Code is organized into classesClasses are grouped into packagesOne way to use this code is to import itYou can import a single class, or all the

classes in a package

The Applet class

6

To create an applet, you must import the Applet classThis 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

7

Here is the directive that you need: import java.applet.Applet;

import is a keywordjava.applet is the name of the packageA dot ( . ) separates the package from the

classApplet is the name of the classThere is a semicolon ( ; ) at the end

The java.awt package

8

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

Drawing lines and shapesDrawing lettersSetting colorsChoosing fonts

If it’s drawn on the screen, then java.awt is probably involved!

Importing the java.awt package

9

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

10

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

Your applet class

11

public class Drawing extends Applet { … }

Drawing is the name of your classClass names should always be capitalized

extends Applet says that our Drawing is a kind of Applet, but with added capabilitiesJava’s Applet just makes an empty windowWe are going to draw in that window

The only way to make an applet is to extend Applet

The applet so far

12

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

public class Drawing extends Applet {

…we still need to put some code in here...

}

The paint method

13

Our applet is going to have a method to paint some colored rectangles on the screen

This method must be named paintpaint needs to be told where on the screen it

can drawThis will be the only parameter it needs

paint doesn’t return any result

The paint method, part 2

14

public void paint(Graphics g) { … }public says that anyone can use this methodvoid says that it does not return a result

A Graphics (short for “Graphics context”) is an object that holds information about a paintingIt remembers what color you are usingIt remembers what font you are usingYou can “paint” on it (but it doesn’t remember

what you have painted)

The applet so far

15

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

public class Drawing extends Applet {

public void paint(Graphics g) { …we still need to put some code in here… }}

Colors

16

The java.awt package defines a class named ColorThere are 13 predefined colors—here are their fully-

qualified names:

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

Color.BLACK Color.PINK Color.GREENColor.DARK_GRAY Color.RED Color.CYANColor.GRAY Color.ORANGEColor.BLUEColor.LIGHT_GRAY Color.YELLOWColor.WHITE Color.MAGENTA

New colors

17

Every color is a mix of red, green, and blueYou can make your own colors:

new Color( red , green , blue )Amounts range from 0 to 255Black is (0, 0, 0), white is (255, 255, 255)We are mixing lights, not pigmentsYellow is red + green, or (255, 255, 0)

Setting a color

18

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

The paint method so far

19

public void paint(Graphics g) { g.setColor(Color.BLUE); …draw a rectangle,write any string… g.setColor(Color.RED); …draw another rectangle write any string… }}

Pixels

20

A pixel is a picture (pix) elementone pixel is one dot on your screenthere are typically 72 to 90 pixels per inch

java.awt measures everything in pixels

Java’s coordinate system

21

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

(0, 0)

(0, 20)

(50, 0)

(50, 20)

(w-1, h-1)

Drawing rectangles

22

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

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

The complete applet

23

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); }}

Some more java.awt methods

24

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 );

The HTML page

25

You can only run an applet in an HTML pageThe HTML looks something like this:

<html> <body> <h1>DrawingApplet Applet</h1> <applet code="DrawingApplet.class" width="250" height="200"> </applet> </body></html>

BlueJ will create this HTML for you

26

Programme-2

27

import java.applet.*;  import java.awt.*;  public class HelloWorld extends Applet {        public void init() {        resize(150,25);        }//init        public void paint(Graphics g) {        g.setFont(new Font("Helvetica", Font.PLAIN, 8));        g.drawString("Hello world!", 50, 25);        }//paint    }//HelloWorld

Compiling and Executing Program:

javac HelloWorld.java

appletviewer test.HelloWorld.html

Programme-1

28

Programme-1import java.applet.*;

import java.awt.*;

public class DrawingLines extends Applet {

int width, height;

public void init()

{ width = getSize().width;

height = getSize().height;

setBackground( Color.black );

}

public void paint( Graphics g )

{

g.setColor( Color.green );

for ( int i = 0; i < 10; ++i )

{

g.drawLine( width, height, i * width / 10, 0 );

} }}

29

Basic Methods(1/2)

30

Methods for Milestonesinit() – initialize the appletstart() – start the applet’s executionstop() – stop the applet’s executiondestroy() – perform a final cleanup

Basic Methods(2/2)

31

Typical Structure

import java.applet.Applet;import java.awt.Graphics;

public class Simple extends Applet { ... public void init() { ... } public void start() { ... } public void stop() { ... } public void destroy() { ... } public void paint(Graphics g) { ... }}

<Applet Code>

<APPLET CODE=... CODEBASE=... WIDTH=... HEIGHT=...> ...</APPLET>

<HTML Code>

Example

32

Source CodeResult

import java.applet.Applet;import java.awt.Graphics;public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello", 50,20); }}

HelloWorld.java

<APPLET CODE="HelloWorld.class“ WIDTH=200 HEIGHT=140></APPLET>

HelloWorld.htm

Programme- public class SetGraphicsColorExample extends Applet{           public void paint(Graphics g) {                 /*                  * Graphic objects like lines and rectangles uses

current                  * foreground color.                  *                  * To change the current graphic color use                  * void setColor(Color c) method of Graphics Class.                  */                                 //this will create light blue color                 Color customColor = new Color(10,10,255);                                 g.setColor(customColor);                 g.drawLine(10,10,30,30);                                 g.setColor(Color.red);                 g.fillRect(40,40,40,40);                                 g.setColor(Color.green);                 g.fillRect(80,80,40,40);                                 g.draw3DRect(81,81,40,40,true);                         } }

ProgramProgram 1: Write an applet program with a message and display the message in paint () method./* <applet code="MyApplet.class" width = 600 height= 450></applet> */

Program