J ava Applet
  
 Introduction

An applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a web-page and runs in the context of the browser. An applet must be a subclass of the java.applet.Applet class, which provides the standard interface between the applet and the browser environment.

Swing provides a special subclass of Applet, called javax.swing.JApplet, which should be used for all applets that use Swing components to construct their GUIs.

Your Ad Here

 
 Creating applets:

  1. Write the source code and save it with a .java extension
  2. Compile the program
  3. Create an HTML file and embed the .class file with the <applet> tag into it.
  4. 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:

MethodDescription
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:
MethodDescription
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:
MethodDescription
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
PreviousNext



Your Ad Here


 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!
   My Web URL : http://www.completetutorials.googlepages.com
   If you like this article and/or code, make a small donation to my account,
Till we meet next time BYE     

  Send your feed-back and query to admin@completetutorials.co.cc
©2008 Bourgia