Sie sind auf Seite 1von 26

APPLET

Console based application:


Executed in console by java run time interpreter
Program execution starts from main method
Interaction with the application through input and output
Applet base application:
Applets are not executed in console based java run time
interpreter
Normally executed in browser
Program execution starts from init method
Interaction with the application through events
APPLET


Object Component Container Panel Applet
AppletContext
AppletStub
AudioClip
Applet Class Methods
Init() first method to be called, for initialization of applet
members, called only once during execution of the program
Start() executed after init(), executed when the applet
receiver the flow, when browser receive the applet page
Stop() executed when applet loss the focus
Destroy() executed when applet is disposed
getImage(URL urlobject) returns the image object of the
specified URL
getParameter(String name) receives the value from the
<PARAM> tag.
getCodeBase() returns the url of the associated applet
getDocumentBase() returns the url of the HTML document
that invokes the applet
showStatus() to display the message in status window
getAppletContext() returns the appletcontext object
getAudioclip() returns the browser audioclip
AppletContext (Interface)
Gives information about applet environment
Methods:
showDocument(URL url) shows a
document of specified URL
Enumeration getApplet() return the
enumeration of applet

AppletStub (interface)
Act as interface between applet and
browser
AudioClip (interface)
For playing audioclips
Methods
Loop()
Play()
Stop()
AWT (Abstract Window Toolkit
Object
Image
FontMetrics
Font
Graphics
Component
Component (abstract class)

Methods:
Paint() used for drawing
Repaint() calls the paint method
Repaint(int x,int y, ind width, int height) used to call
paint in particular window
SetFont(Font f) sets the specified font to the applet
setBackground(Color c) set the specified color to the
background
setForeground(Color c) sets the specified color to the
foreground
getFont() returns the current font
getBackground() returns the current background color
getForeground() returns the current foreground color


Graphics (Abstract Class)
Methods
drawLine(int x1,inty1,int x2, int y2)
drawString(String s, int x, int y)
drawRect(int x, int y, int width, int height)
drawOval(int x, int y, int width, int height)
fillOval(int x, int y, int width, int height)
frawPolygon(int x[], int y[], int pts)
drawImage(Image img, int x, int y,
ImageObserver this)
Simple program
import java.applet.*;
import java.awt.*;
//<applet code="simpleapplet.class" width=500 height=500>
//</applet>
class simpleapplet extends Applet
{
public void init()
{
setBackground(Color.red);
}
public void paint(Graphics g)
{
g.drawString("Hello",100,100);
g.drawRect(150,150,150,50);
}
}
Get parameter value from param
tag
<applet >
<param name=regno value=111>
<param name=stuname value=xyz>
</applet>
import java.applet.*;
import java.awt.*;
/* <applet code="appletgetparam.class" width=500 height=500>
<param name=roll value=12345>
<param name=name1 value=lkfjsdlkfs>
</applet> */
public class appletgetparam extends Applet
{
String rollno, name;
public void init()
{
setBackground(Color.red);

}
public void start()
{
rollno=getParameter("roll");
name=getParameter("name1");
//img=getImage(getCodeBase(),"photo.jpg");
}
public void paint(Graphics g)
{
g.drawString(rollno,50,50);
g.drawString(name, 100,50);
}
}
AWT Controls or components

Object
Component
CheckBoxGroup
Canvas
Scrollbar
TextArea
TextField
Label
List
Choice
CheckBox
Button
Button
Constructor
Button()
Button(String str)
Methods
getLabel() returns the label of the
button
setLabel() sets the lable of the button
Class buttondemo extends Applet
{
Button b1,b2;
Public void init()
{
B1=new Button(First);
B2=new Button(Second);
Add(b1);//add(Component compobj);
Add(b2);
}
}

Label
Label()
Label(String s)
Label(String s, int align)
Methods
getText()
setText()
Checkbox
Checkbox(String str,boolean on)
Checkbox(String str,boolean
on,CheckboxGroup)
Methods
getState() returns boolean value of a
checkbox
setState(boolean on) sets the boolean value
of a checkbox
getLabel() returns the label of the checkbox
setLabel(String str) sets the label of the
checkbox
scrollbar
Constants
UNIT_INCREMENT
UNIT_DECREMENT
BLOCK_INCREMENT
BLOCK_DECREMENT
TRACK
Constructor
Scrollbar()
CheckboxGroup
CheckboxGroup()
Methods
getSelectedCheckbox() returns the
selected checkbox
setSelectedCheckbox(Checkbox o)
sets the checkbox to select state

Choice
Choice()
Choice(String str)
Methods
getSelectedITem() returns the selected item
getSelectedIndex() returns the selected index
getItem(int index) returns the selected item of
given index
select(int index) selects the item of specified
index
addItem(String s) adds the item to the choice
List
List()
List(int numrows, boolean on)
Methods
getSelectedItems() returns array of
items
getSelectedIndexes() returns array of
indexes
TextField
TextField()
TextField(int size)
TextField(String s)
TextField(String s, int size)
Methods
String getText()
Void setText(String s)
Void setEchochar(char c)
Boolean isEchochar()
Void setEditable(boolean on)
TextArea
TextArea(int row,int col)
TextArea(String s)
TextArea(String s, int row,int col,int scrollbars)
Constants:
SCROLLBAR_HORIZONTAL_ONLY
SCROLLBAR_VERTICAL_ONLY
SCROLLBAR_NONE
SCROLLBAR_BOTH
Methods
Append(Strins s)
Insert(String s,int startindex)
replaceRange(String s,int startindex, int endindex)

Component
Dialog
Container
Frame Window
Applet Panel
FileDialog
import java.applet.*;
import java.awt.*;
class userintcomponents extends Applet
{
Button b1,b2,b3;
Label l1,l2,l3;
Checkbox cb1,cb2;
CheckboxGroup cbg;
Choice browser;
List listos;
Scrollbar sbh,sbv;
TextField tf1,tf2;
TextArea ta;
public void init()
{
l1=new Label("Label 1");
l2=new Label("Label 2");
l3=new Label("Label 3");
b1=new Button("Button 1");
b2=new Button("Button 2");
b3=new Button("Button 3");
cbg=new CheckboxGroup();
cb1=new Checkbox("windos os",cbg, true);
cb2=new Checkbox("Linux os",cbg,false);
browser=new Choice();
browser.add("IE");
browser.add("NETSCAPE");
listos=new List(3,true); //true==> multiselect
listos.add("Win 98");
listos.add("Linux");
listos.add("Win xp");
add(l1);
add(b1);
add(l2);
add(b2);
add(l3);
add(b3);
add(cb1);
add(cb2);
add(browser);
add(listos);
sbv = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 50); //getParameter(height);
sbh = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 50);// getParameter(width);
add(sbv);
add(sbh);
tf1=new TextField(20);
tf2=new TextField(30);
add(tf1);
add(tf2);
ta=new TextArea(3,5);
add(ta);
}
}

Das könnte Ihnen auch gefallen