Académique Documents
Professionnel Documents
Culture Documents
• can open network connection only to site from where applet was itself loaded
– GUI
Security Restrictions:
Applets Cannot…
– They can, however, instruct the browser to display pages that are generally accessible on the Web,
which might include some local files
– The browser may choose to cache certain files, including some loaded by applets, but this choice is
not under direct control of the applet
• Open network connections other than to the server from which the applet was loaded
Applets Cannot…
– These actions are prohibited in applets because there is no way to determine whether the
operations these local programs perform are safe
– Applets should not be able to discover the username of the person running them or specific
system information such as current users, directory names or listings, system software, and so forth
– However, applets can determine the name of the host they are on; this information is already
reported to the HTTP server that delivered the applet
/*
Applet Life Cycle Example
This java example explains the life cycle of Java applet.
*/
import java.applet.Applet;
import java.awt.Graphics;
/*
*
* Applet can either run by browser or appletviewer application.
* Define <applet> tag within comments as given below to speed up
* the testing.
*/
/*
<applet code="AppletLifeCycleExample" width=100 height=100>
</applet>
*/
/*
* init method is called first.
* It is used to initialize variables and called only once.
*/
public void init() {
super.init();
}
/*
* start method is the second method to be called. start method is
* called every time the applet has been stopped.
*/
public void start() {
super.start();
}
/*
* stop method is called when the the user navigates away from
* html page containing the applet.
*/
public void stop() {
super.stop();
}
/*
* destroy method is called when browser completely removes
* the applet from memeory. It should free any resources initialized
* during the init method.
*/
public void destroy() {
super.destroy();
}
import java.applet.Applet;
import java.awt.Graphics;
/*
<applet code = "BasicAppletExample" width = 200 height = 200>
</applet>
*/
public class BasicAppletExample extends Applet{
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
/*
<applet code = "ColoredHelloWorldExample" width = 300 height = 300>
</applet>
*/
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
/*
<applet code="CreateCustomColor" width=200 height=100>
</applet>
*/
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
/*
<applet code = "HelloWorldInDifferentColorsExample" width = 300 height = 300>
</applet>
*/
import java.applet.*;
import java.awt.*;
/*
<applet code="StringApp" width=500 height=500></applet>
*/
public class StringApp extends Applet
{
public void paint(Graphics g)
{
String s[]={"h","e","l","l","o"," ","w","o","r","l","d"};
for(int i=0;i<11;i++)
{
g.setColor(c[i]);
g.setFont(new Font("verdana",Font.BOLD,15));
g.drawString(s[i],100,100+i*20);
}
}
}
import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
/*
<applet code="CreateBoldFontExample" width=200 height=200>
</applet>
*/
public class CreateBoldFontExample extends Applet{
/*
* To create a bold font, use
* Font(String name,int style, int size)
* constructor of font class.
*
* sepcify Font.BOLD as style.
*/
g.setFont(myFont);
import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
/*
<applet code="CreateBoldItalicFontExample" width=200 height=200>
</applet>
*/
/*
* To create a bold and italic font, use
* Font(String name,int style, int size)
* constructor of font class.
*
* sepcify Font.BOLD | Font.ITALIC as style.
*/
g.setFont(myFont);
import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/*
<applet code="HandleCheckboxEvent" width=200 height=200>
</applet>
*/
public class HandleCheckboxEvent extends Applet implements ItemListener{
//create checkboxes
java = new Checkbox("Java");
vb = new Checkbox("Visual Basic");
c = new Checkbox("C");
add(java);
add(vb);
add(c);
import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/*
<applet code="GetSelectedRadioButtonExample" width=200 height=200>
</applet>
*/
//create group
lngGrp = new CheckboxGroup();
//add listeners
java.addItemListener(this);
cpp.addItemListener(this);
vb.addItemListener(this);
}
/*
* To get selected radio button, use
* Checkbox getSelectedCheckbox()
* method of CheckboxGroup class.
*/
/*
<applet code="SetSelectedRadioButtonExample" width=200 height=200>
</applet>
*/
//create group
lngGrp = new CheckboxGroup();
lngGrp.setSelectedCheckbox(cpp);
}
}
import java.applet.Applet;
import java.awt.Checkbox;
/*
<applet code="DisableCheckboxExample" width=100 height=200>
</applet>
*/
//create Checkboxes
Checkbox Checkbox1 = new Checkbox("Checkbox 1");
Checkbox Checkbox2 = new Checkbox("Checkbox 2");
//add Checkboxes
add(Checkbox1);
add(Checkbox2);
/*
* To disable a Checkbox, use
* void setEnabled(Boolean enable)
* method.
*/
Checkbox2.setEnabled(false);
}
}
Colouring checkbox
import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.Color;
/*
<applet code="ChangeCheckboxForegroundExample" width=200 height=200>
</applet>
*/
public class ChangeCheckboxForegroundExample extends Applet{
//create checkboxes
Checkbox checkBox1 = new Checkbox("Checkbox 1");
Checkbox checkBox2 = new Checkbox("Checkbox 2");
/*
* To change foreground color of a checkbox use
* setForeground(Color c) method.
*/
checkBox1.setForeground(Color.magenta);
checkBox2.setForeground(Color.blue);
//add buttons
add(checkBox1);
add(checkBox2);
}
}
Change font
import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.Font;
/*
<applet code="ChangeCheckboxFontExample" width=200 height=200>
</applet>
*/
public class ChangeCheckboxFontExample extends Applet{
//create checkboxes
Checkbox checkbox1 = new Checkbox("Checkbox 1");
Checkbox checkbox2 = new Checkbox("Checkbox 2");
/*
* To change font of a checkbox use
* setFont(Font f) method.
*/
//add checkboxes
add(checkbox1);
add(checkbox2);
}
}
Create button
import java.applet.Applet;
import java.awt.Button;
/*
<applet code="CreateAWTButtonExample" width=200 height=200>
</applet>
*/
/*
* To create a button use
* Button() constructor.
*/
/*
* Set button caption or label using
* void setLabel(String text)
* method of AWT Button class.
*/
/*
* To create button with the caption use
* Button(String text) constructor of
* AWT Button class.
*/
Actions on button
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
<applet code="HandleActionEventExample" width=200 height=200>
</applet>
*/
String actionMessage="";
//add Buttons
add(Button1);
add(Button2);
/*
* Get the action command using
* String getActionCommand() method.
*/
if(action.equals("Ok"))
actionMessage = "Ok Button Pressed";
else if(action.equals("Cancel"))
actionMessage = "Cancel Button Pressed";
repaint();
}
}
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
<applet code="ButtonSetActionCommandExample" width=200 height=200>
</applet>
*/
String actionMessage="";
//add Button
add(Button1);
/*
* By default, button's action command is it's label. But in
* some cases, labels are too long and is not appropriate to use
* it as an action command. In such situation you would want to
* define custom short action command for a button.
*
* To set custom action command for a button, use
* void setActionCommand(String command)
* method of AWT Button class.
*/
Button1.setActionCommand("Agree");
}
/*
* Get the action command using
* String getActionCommand() method.
*/
repaint();
}
}
Combo box
import java.applet.Applet;
import java.awt.Choice;
/*
<applet code="CreateChoiceExample" width=200 height=200>
</applet>
*/
public class CreateChoiceExample extends Applet{
/*
* To create a AWT choice control or a combobox, use
* Choice()
* constructor of AWT Choice class.
*/
/*
* To add items in a choice control or a combobox, use
* void add(String item)
* method of AWT Choice class.
*/
language.add("Java");
language.add("C++");
language.add("VB");
language.add("Perl");
}
}
import java.applet.Applet;
import java.awt.Choice;
import java.awt.Graphics;
/*
<applet code="CountNumberOfItems" width=200 height=200>
</applet>
*/
/*
* To count number of items in a choice or a combobox, use
* int getItemCount() method of AWT Choice class.
*/
/*
<applet code="DisableChoiceExample" width=200 height=200>
</applet>
*/
/*
* To disable choice or a combobox, use
* void setEnabled(boolean enable)
* method.
*/
language.setEnabled(false);
}
}
import java.applet.Applet;
import java.awt.Choice;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/*
<applet code="GetSelectedItemExample" width=200 height=200>
</applet>
*/
Create label
import java.applet.Applet;
import java.awt.Label;
/*
<applet code="CreateLabel" width=200 height=200>
</applet>
*/
/*
* To create a new label use
* Label() constructor of AWT Label class.
*/
/*
* To set label text use,
* void setText(String text) method of AWT Label class
*/
label1.setText("My Label 1");
/*
* To create label with the text use
* Label(String text) constructor.
*/
/*
* To add label use
* void add(Component c) method.
*/
add(label1);
add(label2);
}
}
Label alignment
import java.applet.Applet;
import java.awt.Label;
/*
<applet code="CreateLableWithAlignment" width=100 height=200>
</applet>
*/
/*
* To create a label with it's text alignment use
* Label(String text, int alignment)
* constructor of AWT Label class.
*
* The possible alignment values are
* Label.LEFT, Label.RIGHT, and Label.CENTER
*/
//add labels
add(label1);
add(label2);
add(label3);
/*
* To change alignment of label's text use
* void setAlignment(int alignment)
* method of AWT Label class.
*/
}
}
import java.applet.Applet;
import java.awt.Label;
/*
<applet code="DisableLabelExample" width=100 height=200>
</applet>
*/
//add labels
add(label1);
add(label2);
/*
* To disable a label, use
* void setEnabled(Boolean enable)
* method.
*/
label2.setEnabled(false);
}
}
import java.applet.Applet;
import java.awt.Color;
import java.awt.Label;
/*
<applet code="ChangeBackgroundColor" width=100 height=200>
</applet>
*/
//add labels
add(label1);
add(label2);
/*
* To set background color of a label use
* void setBackground(Color c)
* method.
*/
label1.setBackground(Color.green);
label2.setBackground(Color.red);
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Label;
/*
<applet code="DetermineIfLabelEnabled" width=100 height=200>
</applet>
*/
boolean isLabel1Enabled;
boolean isLabel2Enabled;
//add labels
add(label1);
add(label2);
label1.setEnabled(false);
/*
* To determine if the label is enabled or not use
* boolean isEnabled() method.
*/
isLabel1Enabled = label1.isEnabled();
isLabel2Enabled = label2.isEnabled();
import java.applet.Applet;
import java.awt.List;
/*
<applet code="CreateListExample" width=200 height=200>
</applet>
*/
public class CreateListExample extends Applet{
/*
* Create list using
* List() or
* List(int visibleRows) or
* List(int visibleRows, boolean multiSelect)
* constructor of AWT List class.
*/
/*
* To add items to a list use,
* void add(String item)
* method of AWT List class.
*/
list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");
list.add("Six");
list.add("Seven");
//add list
add(list);
}
}
import java.applet.Applet;
import java.awt.List;
/*
<applet code="CreateMultiSelectListExample" width=200 height=200>
</applet>
*/
public class CreateMultiSelectListExample extends Applet{
/*
* Create multi-select list using
* List(int visibleRows, boolean multiSelect)
* constructor of AWT List class.
*/
//add list
add(list);
}
}
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/*
<applet code="GetSelectedItemFromMultiSelectExample" width=200 height=200>
</applet>
*/
//add list
add(list);
//add listener
list.addItemListener(this);
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/*
<applet code="GetSelectedItemFromSingleSelectExample" width=200 height=200>
</applet>
*/
//add list
add(list);
//add listener
list.addItemListener(this);
}