http://www.developer.com/open/article.php/791151/Java-Applet-Basics.htm
Introduction This tutorial assumes that you know fundamentals of Java
application programming. However, I'll offer some important
notes for the beginners among you. To compile and execute Java programs, you should install
Java Development Kit (JDK) Version 1.2 or higher (recommended).
You can use older versions of the JDK, but this tutorial is being prepared
for use with JDK 1.2 or higher. You can download the
JDK from Sun's Website free of cost.
You can also get the JDK through some Java textBooks. Just
double-click the setup file and proceed with the installation. After installing the JDK, you have to set the correct path
in order to work with Java programs. For setting the path,
execute the MSCONFIG.exe (System Configuration utility) Program from the start-run menu.
Press the autoexec.bat tab and click on the New button.
Type in the following command: Anyway, you can also use the above command everytime you
practice Java programming, but it is recommended to follow the above
procedure. Also supply the Doskey command (in autoexec.bat) so that you have to type
in each command at the command prompt only once. Just press the up and down
arrow keys to retrieve your earlier command (earlier in the sense of the current session). You can write code using Notepad or MSDOS Editor (supply EDIT Command at the DOS
prompt). However, Notepad is the most preferred editor among most programmers.
Save your files with .java extensions. Compile the program using javac Overview What are Applets What you need to execute an Applet Features of an Applet Your First Applet Type in the following code using any text editor or in DOS
editor. Save the file as hello.java and
compile it by using javac. Now, type in the following HTML code in
your editor and save the file as hello.html
Execute the HTML file by giving
appletviewer hello.html. Another way to run the applet is to give
the above HTML coding as a comment after the two import
statements. Execute the applet as appletviewer hello.java
Concepts and Explanation Facts
Life Cycle of an Applet The Parameter Passing Graphics Class Methods Font Class The Font class in the java.awt package
contains methods for displaying fonts. Sample Color Class Methods
The Class Hierachy Usage of Label Usage of Button Usage of Usage of CheckboxGroup Usage of TextField Usage of Text Area Usage of Choice Usage of Lists All these above components together form a GUI Interface. You
can create any type of user-friendly applications you want by making
use of the above components. In the next section, we will take a
look at Layout Mangers in Java, with which you can
dynamically place the above components at your desired location.
You can place the components according to your
taste and position by using Layout Managers. The basic Layout
Managers include Flow Layout, Border Layout, and Grid Layout. Find the Layout Manager and
Instantiate it by using their Constructors Associate the manager with the components
in the Container. The Layout Manager is set by If no layout manager is specified, then
the default manager is taken. Flow
Layout Default Layout Manager works like a
typical word processor from Left to right. Border
Layout Each Component can be placed on the
border of a container. With Border Layout the placement of the
component is specified as North, South, East, and West. Grid
Layout Lays out component in a two-dimensional
grid Card
Layout Unique among other Layout managers Each layout is a separate index card in a
deck that can be shuffled so that any card is on the
top at a given time. The Panel is also a container. The Panel can contain UI Components and
other Containers. The general Constructor is The GUI: A graphical user interface. It is an interface
to the Windows operating system. It includes user friendly
controls, like Buttons, textfields for entering text, message boxes, etc. AWT: The Abstract Windowing Toolkit. It is a package in JavaAPI and
consists of Graphics, Font, color, Image, etc. classes.
You have to call this package by using the import keyword (import LABEL: It denotes uneditable text. BUTTON: It is a standard control found in Windows. CHECKBOX: It consist of a set of items. Users can select one or more items
at a time. CHECKBOXGROUP: It denotes a radio (small, black circle shape) button.
Users can select only one item at a time. TEXTFIELD: Users can enter information in the the boxes. You can enter in one
single line. TEXTAREA: By using this control, users can enter multiple lines of text. CHOICE: This component is the same as combo box. When a user clicks on the
dropdown arrow, she will get a list of items, only one item is selectable at a time. LIST: This is a variation of the above component. Users can select multiple items
at a time.
Java Applet Basics
June 25, 2001
Life Cycle,Graphics, etc.
User Interface Components
Layout Managers
Part I
Introduction
set path=c:\jdk1.2\bin;%path%.
You have to substitute the correct version of the drive and
the JDK version number. I'll assume that the JDK is in C:\ Drive and the
version is 1.2.
import java.awt.*
import java.applet.*;
public classhello extends Applet {
public void paint(Graphics g) {
g.drawString("Welcome to Java Applets",20,20); } }
<applet code =
"classhello.class" width = 200 height =
150></applet>
Paint() method is defined by AWT Component class. It
takes one parameter of type Graphics.
main() method. However,
it is possible to execute the applets by using the Java
interpreter (by using the main() method) if you extend your
class with Frame. Part II
Life Cycle, Graphics, Fonts, Colors
Method
Class
Description
init()Applet
First method to be called,
initialize variables in this method
start()Applet
Called when restarted after
being stopped.occurs after
init()
stop()Applet
Called when the applet leaves
the webpage
destroy()Applet
Called when the applet wants
to be removed out of memory
paint()Component
Called when the applet needs
to be drawn
repaint() method - If the applet wants to be repainted again, then
this method is called. Useful for animation purposes.
getParameter() inside the init() method,
which takes one argument (i.e., the string representing the
name of the parameter being looked for). Give this name a
value in the HTML coding.
drawString(String str,int X,int Y)
method, where str is the name of the string and X and Y
are the coordinates for where the string is to be printed.drawLine(int x1,int y1,int x2,int y2),
where x1 and y1 are the starting point coordinates
and x2 and y2 are the ending point coordinates.drawRect(int x1,int y1,int
width,int height), where x1 and y1 are the starting
point coordinates and width and height are the
measurements for rectangledrawRoundRect(int x1,int y1,int
width,int height,width1,height1), where x1 and y1
are the starting point coordinates and width and height
are the measurements for rectangle and width1 and height1
are the angles of the corners.drawOval(int x1,int y1,int width,int
height), where x1 and y1 are the coordinates of the top
corners and width and height are the respective
measurements of the oval.drawArc(int x1,int y1,int width,int
height,angle1 ,angle2), where x1 and y1 are the
coordinates of the top corners and width and height are
the respective measurements of the arc and angle1 and
angle2 are the starting arc and ending arcs (in
degrees).
setFont
method in the java.awt package.
Font f = new Font("Courier",Font.BOLD+Font.Italic,
16); ||||| g.setFont(f);
setColor(Color.gray) sets the string color to gray . setBackground(Color.red) sets the background color to
red.Part III
User Interface Components
Label() - Creates an empty labelLabel(String) - Creates a label with the given stringLabel(String, align) - Creates a string label with the
specified alignment (RIGHT,LEFT,CENTER)
Button() - Creates a button without a labelButton(String str) - Creates a button with the stringCheckbox()
Checkbox() - Creates a checkbox without a label Checkbox(String str) - Creates a checkbox with a string
label in it
TextField() - Creates a empty Text FieldTextField(String str) - Creates a Text Field with the
specified StringTextField(String str,align) - Creates a Text Field with the
specified String with the alignmentsetText() method is used in connection with the Text
Fields. For instance, to set a text in Choice to the
text field, the method setText() method is used
TextArea() - Creates empty Text AreaTextArea(rows,charcters) - Creates empty Text Area, with
the specified rows and charcters.TextArea(String str,rows,charcters) - Creates a default
String Text Area, with the specified rows and charcters
Choice c = new Choice() )add method and
connecting the Choice object
Choice ch = new Choice(); ch.add("Java"),
ch.add("XML"); add(ch)
List l = new List() )add method and
connecting the List object
setEditable(Boolean) method is used to edit the text
inside a choice component.Part IV
Layout Managers
The
Concept
setLayout()
method.
Flow Layout() creates a default
layout centered and leaves a position of 5 pixels of
space between each Component.Flow Layout( int how) creates a layout
with the specified alignment (LEFT,RIGHT,CENTER). Flow Layout(int how,int h,int v) creates
a layout with the specified spaces.
Border Layout() creates a default Border
Layout.Border Layout(int horz,int vert) leaves
the specified spaces between components.
Grid Layout() - creates single
column grid layout.Grid Layout(num rows ,num cols) creates a
Grid containing the specified rows and columns. For
example, Grid Layout(3,3) creates a 3 x 3 Grid.Grid Layout(num rows,num cols,int
horz,int vert) creates a Grid containing the specified
spaces between the Grid.
Panels
Panel p1 = new
Panel().add() method of the Container Class
can be used to add a Component to the Panel.Glossary
java.awt.*). if
You are using methods and interfaces from this package. About the Author
Anand Narayanaswamy is a
graduate of the University of Kerala. He is currently working
as an instructor teaching Java, Visual Basic, and technologies such as ASP
and XML. He enjoys learning new programming languages like C#. Currently,
Anand lives in Thiruvananthapuram, Kerala State, India. He can be contacted via
his Website.