0% found this document useful (0 votes)
28 views

TCS2044 Chapter6 Applet & HTML Week12

This document discusses Java applets and their structure. It explains that all applets must extend the Applet or JApplet class and override certain methods like init(), start(), stop(), and destroy() that are called by the browser. It also discusses how to compile and run applets using a web browser or the appletviewer tool. Additionally, it covers how to use graphics methods to draw on applets, add GUI components like buttons and text fields, and handle events.

Uploaded by

Nazfar Abdullah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

TCS2044 Chapter6 Applet & HTML Week12

This document discusses Java applets and their structure. It explains that all applets must extend the Applet or JApplet class and override certain methods like init(), start(), stop(), and destroy() that are called by the browser. It also discusses how to compile and run applets using a web browser or the appletviewer tool. Additionally, it covers how to use graphics methods to draw on applets, add GUI components like buttons and text fields, and handle events.

Uploaded by

Nazfar Abdullah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Applet Basic Structure


Image format

All Java applet must extends java.applet.Applet Java applet does not have a main() method. Applets depend on the web browser to call the

class or javax.swing.Japplet class.


methods.

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Applet structure:
class MyApplet extends java.applet.Applet { . public void init(){ } public void start(){.}

public void stop(){.}


public void destroy(){.} // other method if necessary }

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Applet Methods

The browser controls the applets using the init(),


start(), stop() and destroy() methods. By default these methods do nothing.

Figure below show how the browser calls init(),


start(), stop() and destroy() methods.

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Invoked after the init() method. Also is called whenever the applet becomes active again after a period of activity.

init()

Method invoked when the applet is first loaded and again if it is loaded common functions : creating new threads, loading image, setting up userInterface components.

Return to page

start()
Go to another page

stop()

Invokded when the browser exits normally to inform the applet that is no longer needed and that it should release any resources it has allocated. Always is called before the destroy() method.

destroy()

Opposite of the start(). Called when the user moves back to the page containing the applet

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

paint() method

Always used when the component needs to be displayed or redisplayed. Java system automatically creates a default graphics context, an object of the Graphics class, and passes it as a parameter to the paint() methods. This object is local to the method and it cannot be used outside of this method.

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Compile and Run Applet

To compile applet program, same as java application

syntax : javac filename.java

To run applet, can choose one of below:

1.Use web browser (Internet Explorer, Mozilla, etc) or 2. Use appletviewer syntax : appletviewer filename.html

( * HTML file must be created first)

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Edit text, color, size and font


Color
Color class can be used to set color. Colors are made of a Red, a Green and a Blue component; each of which is represented by a byte value to describe the colors intensity, ranging from 0 (darkest shade) to 255 (lighest shade) known as the RGB model.

Syntax to create a color object: Color c1 =new Color(r, g, b); Alternatively, we can use one of 13 standard colors(BLACK, BLUE, CYAN, DARKGRAY, GRAY, GREEN, LIGHTGRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW).

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Using Color class Example

Output of the program

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Size and Font

To set a font, we need to create a font object from the Font class. Syntax : Font myFont = new Font (name, style, size);

can choose the can choose from appropriate size TimesRoman, Courier, Helvetica, Symbol, or Dialog. can choose a style from Font.PLAIN, Font.BOLD and Font.ITALIC ( Can be combined)
MANAGEMENT & SCIENCE UNIVERSITY 9

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Using Font class Example

Output of the program

MANAGEMENT & SCIENCE UNIVERSITY

10

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Graphics

and

Applet

Java provides a set of methods in the Graphics class that make it easy to draw geometric figures which is contained in the Graphics class. Java coordinate system has x in the horizontal axis and y in the vertical axis with the origin ( 0,0) at the upper-left of the screen.
(0,0) x

All measurement are made in pixels y

MANAGEMENT & SCIENCE UNIVERSITY

11

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Using graphics method Example

Output of the program

MANAGEMENT & SCIENCE UNIVERSITY

12

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Create a simple object


We can create another object or picture according to the basic method in Graphics class. This method can be combined to create an object depend on the figure that object required. The coordinate must be considered to make sure the object appear as what we want. We should be creative to produce a good and attractive object.

MANAGEMENT & SCIENCE UNIVERSITY

13

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Question :
Create a simple picture or object from combination of several method in Graphics class.

Example 1

import java.awt.* ; public class house extends java.applet.Applet {Font a =new Font(" Arial" Font.BOLD, 20); , Color c1=new Color(250,1 80,200); public void paint(Graphics g) { int x[] ={ 50,1 2} 40,1 75,1 ; int y[] ={ 30,30,55,55} ; g.setColor(c1 ); g.fillPolygon(x,y,x.length); g.setColor(Color.magenta); g.fillRect(40,55,1 0,60); 1 g.setColor(Color.green); g.fillRect(80,75,30,40); g.setColor(Color.blue); g.fillRect(50,60,20,20); g.fillRect(1 20,60,20,20); } }

MANAGEMENT & SCIENCE UNIVERSITY

14

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Example 2

import java.awt.* ; public class culex extends java.applet.Applet { public void paint(Graphics g) { g.drawArc(50,30,50,40,28,-200); g.drawArc(55,30,45,20,0,-1 83); g.drawArc(35,30,20,25,1 00,-272); g.drawArc(28,25,1 8,1 5,1 84,-275); g.drawLine(29,35,1 0,55); g.drawLine(1 0,55,32,38); g.drawArc(32,36,4,4,1 80,-272); g.drawLine(34,40,32,45); g.drawOval(30,30,2,2); g.drawOval(33,32,2,2); g.drawOval(50,32,95,1 0); g.drawOval(49,25,90,1 0); g.drawArc(52,24,60,40,1 80,95); g.drawArc(26,1 8,20,60,0,-45); g.drawArc(37,29,20,45,1 80,45); g.drawArc(35,28,20,45,1 80,45); } }

MANAGEMENT & SCIENCE UNIVERSITY

15

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Using GUI in applet


Adding Button Example

Output

Java applet source

HTML

MANAGEMENT & SCIENCE UNIVERSITY

16

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Adding Label and textfield Example

Output

MANAGEMENT & SCIENCE UNIVERSITY

17

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Adding textarea Example

Output

Java applet source

HTML

MANAGEMENT & SCIENCE UNIVERSITY

18

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

Handling events in applet


Example

Continue .
MANAGEMENT & SCIENCE UNIVERSITY 19

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

.continue

Continue .
MANAGEMENT & SCIENCE UNIVERSITY 20

TCS 2044 JAVA PROGRAMMING

CHAPTER 6 APPLET & HTML (Week 12)

.continue

Output of the program

HTML

MANAGEMENT & SCIENCE UNIVERSITY

21

You might also like