Creating applets:
|
- Write the source code and save it with a .java extension
-
Compile the program
-
Create an HTML file and embed the .class file with the <applet> tag into it.
-
To execute the applet, open the HTML file in the browser or use the appletviewer utility, which is part of the Java Development Kit.
|
The <applet> tag:
|
Code, width, and height are mandatory attributes of the <applet> tag. Optional attributes include codebase, alt,name, align, vspace, and hspace. The code attribute takes the name of the class file as its value.
- Syntax:
<applet code = “abc.class” height=300 width=300> -
<param name=parameterName1 value= value1>
-
<param name=parameterName2 value= value2>
-
</applet>
|
Using the Appletviewer:
|
Appletviewer.exe is an application found in the BIN folder as part of the JDK. Once an HTML file containing the class file is created (eg. abc.html), type in the command line:
Appletviewer abc.html
|
java.applet.Applet class:
|
Methods of the java.applet.Applet class:
Method | Description |
---|
init() | Invoked by the browser or the applet viewer to inform that the applet has been loaded |
start() | Invoked by the browser or the applet viewer to inform that applet execution has started |
stop() | Invoked by the browser or the applet viewer to inform that applet execution has stopped
|
destroy() | Invoked by the browser or the appletviewer to inform that the applet has been reclaimed by the Garbage Collector |
getAppletContext() | Determines the applet context or the environment in which it runs |
getImage() | Returns an Image object that can be drawn on the applet window |
getDocumentBase() | Returns the URL of the HTML page that loads the applet |
getCodeBase() | Returns the URL of the applet’s class file |
getParameter() | Returns the value of a named applet parameter as a string |
showStatus() | Displays the argument string on the applet’s status
|
|
java.awt.Graphics class:
|
The Graphics class is an abstract class that contains all the essential drawing methods like drawLine(), drawOval(), drawRect() and so on. A Graphics reference is passed as an argument to the paint() method that belongs to the java.awt.Component class.
Methods of the Graphics class:
Method | Description |
---|
drawLine() | Draws a line between (x1,y1) and (x2,y2) passed as parameters |
drawRect()/fillRect() | Draws a rectangle of specified width and height at a specified location |
drawOval()/fillOval() | Draws a circle or an ellipse that fills within a rectangle of specified coordinates |
drawString() | Draws the text given as a specified string |
drawImage() | Draws the specified image onto the screen |
drawPolygon() / fillPolygon() | Draws a closed polygon defined by arrays of x and y coordinates |
setColor() | Sets the specified color of the graphics context |
setFont() | Sets the specified font of the graphics context
|
|
java.awt.Component class:
|
The Component class is an abstract class that is a superclass of all AWT components. A component has a graphical representation that a user can interact with. For instance, Button, Checkbox, TextField, and TextArea.
Methods of the Component class:
Method | Description |
---|
paint(Graphics g) | Paints the component. The Graphics context g is used for painting. |
setBackground() | Sets the background color of the component |
setForeground() | Sets the foreground color of the component |
SetSize() | Resizes the component |
setLocation() | Moves the component to a new location |
setBounds() | Moves the component to specified location and resizes it to the specified size |
addFocusListener() | Registers a FocusListener object to receive focus events from the component |
addMouseListener() | Registers a MouseListener object to receive mouse events from the component |
addKeyListener() | Registers a KeyListener object to receive key events from the component |
getGraphics() | Returns the graphics context of this component |
update(Graphics g) | Updates the component. Calls the paint() method to redraw the component.
|
|
Click for Next Quick Reference
|
|
|
|
About Author
|
Author Name : Mr. R.Franklin Bourgia Singh
Country : INDIA
Working In : Aryans Infoway (P) Ltd.
Email : admin@completetutorials.co.cc
Author Home Page : http://www.completetutorials.googlepages.com
|
Reach me!
|
|
Send your feed-back and query to admin@completetutorials.co.cc
©2008 Bourgia
|