TCS2044 Chapter6 Applet & HTML Week12
TCS2044 Chapter6 Applet & HTML Week12
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
methods.
Applet structure:
class MyApplet extends java.applet.Applet { . public void init(){ } public void start(){.}
Applet Methods
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
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.
1.Use web browser (Internet Explorer, Mozilla, etc) or 2. Use appletviewer syntax : appletviewer filename.html
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).
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
10
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
11
12
13
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); } }
14
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); } }
15
Output
HTML
16
Output
17
Output
HTML
18
Continue .
MANAGEMENT & SCIENCE UNIVERSITY 19
.continue
Continue .
MANAGEMENT & SCIENCE UNIVERSITY 20
.continue
HTML
21