Vous êtes sur la page 1sur 26

Applet

– program embedded in web page and run by “browser”

– restricted access to machine on which it is run

(restrictions can be varied by sophisticated browser)

• no access to local files

• can open network connection only to site from where applet was itself loaded

• can access some details of enclosing web page

– GUI

• use part of browser’s page window for main window

Security Restrictions:

Applets Cannot…

• Read from the local (client) disk

– Applets cannot read arbitrary files

– They can, however, instruct the browser to display pages that are generally accessible on the Web,
which might include some local files

• Write to the local (client) disk

– 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

– This restriction prevents applets from browsing behind network firewalls

Applets Cannot…

• Link to client-side C code or call programs installed on the browser machine

– Ordinary Java applications can invoke locally installed programs (Runtime.exec or


ProcessBuilder.start) as well as link to local C/C++ modules (“native” methods)

– These actions are prohibited in applets because there is no way to determine whether the
operations these local programs perform are safe

• Discover private information about the user

– 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 code to be used in html file

<applet code=“MyApplet.class” width=200 height=120> </applet>


An Applet has its own unique functionality
– init() first method called when applet
being started; typically does things
like read parameters, load images, ...
– start() called after init() and each time user
returns to HTML page with applet;
typical use - starting “threads”, ...
– stop() called each time user leaves HTML
page; typical use - suspend “threads”
– destroy() free resources (doesn’t Java do this? well,
sometimes applet does have stuff to clean up)
and kill threads

/*
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>
*/

public class AppletLifeCycleExample extends 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();
}

/* paint method is called every time applet has to redraw its


* output.
*/
public void paint(Graphics g) {
super.paint(g);
}

/*
* 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{

public void paint(Graphics g){


//write text using drawString method of Graphics class
g.drawString("This is my First Applet",20,100);
}
}

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

/*
<applet code = "ColoredHelloWorldExample" width = 300 height = 300>
</applet>
*/

public class ColoredHelloWorldExample extends Applet{


public void paint(Graphics g){
//set color to blue
g.setColor(Color.blue);

//print hello world


g.drawString("Hello World...",30,180);
}
}

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

/*
<applet code="CreateCustomColor" width=200 height=100>
</applet>
*/

public class CreateCustomColor extends Applet{

public void paint(Graphics g) {


/*
* To create a custom color using RGB, use
* Color(int red,int green, int blue)
* constructor of Color class.
*/

//this will create light blue color


Color customColor = new Color(10,10,255);

//set foreground color of an applet


this.setForeground(customColor);
g.drawString("This is a custom RGB color", 10, 50);
}
}

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

/*
<applet code = "HelloWorldInDifferentColorsExample" width = 300 height = 300>
</applet>
*/

public class HelloWorldInDifferentColorsExample extends Applet{


public void paint(Graphics g){

Color c[] = {Color.blue,Color.cyan, Color.darkGray, Color.gray,


Color.green, Color.lightGray, Color.magenta, Color.orange,
Color.pink,
Color.red, Color.white, Color.yellow};

for(int i = 0; i<c.length; i++){


g.setColor(c[i]);
g.drawString("Hello World...",10,10 + (i*10));
}
}
}

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"};

Color c[]={ Color.blue,Color.cyan, Color.darkGray, Color.gray,


Color.green, Color.lightGray, Color.magenta, Color.orange,

Color.pink,Color.red, Color.white, Color.yellow};

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{

public void paint(Graphics g) {

/*
* To create a bold font, use
* Font(String name,int style, int size)
* constructor of font class.
*
* sepcify Font.BOLD as style.
*/

Font myFont = new Font("Courier", Font.BOLD,20);

g.setFont(myFont);

g.drawString("Bold Font Example", 10, 50);


}
}

import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;

/*
<applet code="CreateBoldItalicFontExample" width=200 height=200>
</applet>
*/

public class CreateBoldItalicFontExample extends Applet{

public void paint(Graphics g) {

/*
* 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.
*/

Font myFont = new Font("Courier", Font.BOLD | Font.ITALIC ,20);

g.setFont(myFont);

g.drawString("Bold & Italic Font Example", 10, 50);


}
}

Check box event handling

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{

Checkbox java = null;


Checkbox vb = null;
Checkbox c = null;
public void init(){

//create checkboxes
java = new Checkbox("Java");
vb = new Checkbox("Visual Basic");
c = new Checkbox("C");

add(java);
add(vb);
add(c);

//add item listeners


java.addItemListener(this);
vb.addItemListener(this);
c.addItemListener(this);
}

public void paint(Graphics g){

g.drawString("Java: " + java.getState(),10,80);


g.drawString("VB: " + vb.getState(), 10, 100);
.drawString("C: " + c.getState(), 10, 120);

public void itemStateChanged(ItemEvent ie) {


repaint();
}
}

Radio button event handling example

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>
*/

public class GetSelectedRadioButtonExample extends Applet implements ItemListener{

CheckboxGroup lngGrp = null;

public void init(){

//create group
lngGrp = new CheckboxGroup();

//create checkboxes and add to group


Checkbox java = new Checkbox("Java", lngGrp, true);
Checkbox cpp = new Checkbox("C++", lngGrp, false);
Checkbox vb = new Checkbox("VB", lngGrp, false);

//add radio buttons


add(java);
add(cpp);
add(vb);

//add listeners
java.addItemListener(this);
cpp.addItemListener(this);
vb.addItemListener(this);
}

public void itemStateChanged(ItemEvent ie) {


repaint();
}

public void paint(Graphics g){

/*
* To get selected radio button, use
* Checkbox getSelectedCheckbox()
* method of CheckboxGroup class.
*/

Checkbox chk = lngGrp.getSelectedCheckbox();

g.drawString(chk.getLabel() + " is selected", 10 ,70);


}
}

Set selected radio button


import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;

/*
<applet code="SetSelectedRadioButtonExample" width=200 height=200>
</applet>
*/

public class SetSelectedRadioButtonExample extends Applet{

CheckboxGroup lngGrp = null;

public void init(){

//create group
lngGrp = new CheckboxGroup();

//create checkboxes and add to group


Checkbox java = new Checkbox("Java", lngGrp, false);
Checkbox cpp = new Checkbox("C++", lngGrp, false);
Checkbox vb = new Checkbox("VB", lngGrp, false);

//add radio buttons


add(java);
add(cpp);
add(vb);
/*
* To set a radio button selected by default, use
* void setSelectedCheckbox(Checkbox chk)
* method of Java AWT CheckboxGroup class.
*/

lngGrp.setSelectedCheckbox(cpp);
}
}

Disable check box

import java.applet.Applet;
import java.awt.Checkbox;

/*
<applet code="DisableCheckboxExample" width=100 height=200>
</applet>
*/

public class DisableCheckboxExample extends Applet{

public void init(){

//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{

public void init(){

//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{

public void init(){

//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.
*/

Font myFont = new Font("Courier", Font.ITALIC,12);


checkbox1.setFont(myFont);

//add checkboxes
add(checkbox1);
add(checkbox2);
}
}

Create button

import java.applet.Applet;
import java.awt.Button;

/*
<applet code="CreateAWTButtonExample" width=200 height=200>
</applet>
*/

public class CreateAWTButtonExample extends Applet{

public void init(){

/*
* To create a button use
* Button() constructor.
*/

Button button1 = new Button();

/*
* Set button caption or label using
* void setLabel(String text)
* method of AWT Button class.
*/

button1.setLabel("My Button 1");

/*
* To create button with the caption use
* Button(String text) constructor of
* AWT Button class.
*/

Button button2 = new Button("My Button 2");

//add buttons using add method


add(button1);
add(button2);
}
}

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>
*/

public class HandleActionEventExample extends Applet implements ActionListener{

String actionMessage="";

public void init(){


//create Buttons
Button Button1 = new Button("Ok");
Button Button2 = new Button("Cancel");

//add Buttons
add(Button1);
add(Button2);

//set action listeners for buttons


Button1.addActionListener(this);
Button2.addActionListener(this);
}

public void paint(Graphics g){


g.drawString(actionMessage,10,50);
}

public void actionPerformed(ActionEvent ae){

/*
* Get the action command using
* String getActionCommand() method.
*/

String action = ae.getActionCommand();

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>
*/

public class ButtonSetActionCommandExample extends Applet implements ActionListener{

String actionMessage="";

public void init(){


//create Button
Button Button1 = new Button("I agree with the terms and conditions");

//add Button
add(Button1);

//set action listeners for buttons


Button1.addActionListener(this);

/*
* 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");
}

public void paint(Graphics g){


g.drawString(actionMessage,10,50);
}

public void actionPerformed(ActionEvent ae){

/*
* Get the action command using
* String getActionCommand() method.
*/

String action = ae.getActionCommand();


actionMessage = action + " button pressed!";

repaint();
}
}

Combo box

import java.applet.Applet;
import java.awt.Choice;

/*
<applet code="CreateChoiceExample" width=200 height=200>
</applet>
*/
public class CreateChoiceExample extends Applet{

public void init(){

/*
* To create a AWT choice control or a combobox, use
* Choice()
* constructor of AWT Choice class.
*/

Choice language = new Choice();

/*
* 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");

//add choice or combobox


add(language);

}
}

Count items in combo box

import java.applet.Applet;
import java.awt.Choice;
import java.awt.Graphics;
/*
<applet code="CountNumberOfItems" width=200 height=200>
</applet>
*/

public class CountNumberOfItems extends Applet{

Choice language = null;

public void init(){

//create choice or combobox


language = new Choice();

//add items to the choice


language.add("Java");
language.add("C++");
language.add("VB");
language.add("Perl");

//add choice or combobox


add(language);

public void paint(Graphics g){

/*
* To count number of items in a choice or a combobox, use
* int getItemCount() method of AWT Choice class.
*/

int items = language.getItemCount();


g.drawString("There are " + items + " items in a combobox", 10, 70);
}
}

Disable combo box item


import java.applet.Applet;
import java.awt.Choice;

/*
<applet code="DisableChoiceExample" width=200 height=200>
</applet>
*/

public class DisableChoiceExample extends Applet{

Choice language = null;


public void init(){

//create choice or combobox


language = new Choice();

//add items to the choice


language.add("Java");
language.add("C++");
language.add("VB");
language.add("Perl");

//add choice or combobox


add(language);

/*
* To disable choice or a combobox, use
* void setEnabled(boolean enable)
* method.
*/

language.setEnabled(false);

}
}

Get the item from combo box

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>
*/

public class GetSelectedItemExample extends Applet implements ItemListener{

Choice language = null;

public void init(){

//create choice or combobox


language = new Choice();

//add items to the choice


language.add("Java");
language.add("C++");
language.add("VB");
language.add("Perl");

//add choice or combobox


add(language);

//add item listener


language.addItemListener(this);

public void paint(Graphics g){


/*
* To get selected item, use
* String getSelectedItem()
* method of AWT Choice class.
*/
g.drawString(language.getSelectedItem(),10, 70);
}

public void itemStateChanged(ItemEvent arg0) {


repaint();
}
}

Create label

import java.applet.Applet;
import java.awt.Label;

/*
<applet code="CreateLabel" width=200 height=200>
</applet>
*/

public class CreateLabel extends Applet{

public void init(){

/*
* To create a new label use
* Label() constructor of AWT Label class.
*/

Label label1 = new Label();

/*
* 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.
*/

Label label2 = new Label("My Label 2");

/*
* 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>
*/

public class CreateLableWithAlignment extends Applet{

public void init(){

/*
* 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
*/

Label label1 = new Label("Left Aligned Label", Label.LEFT);

Label label2 = new Label("Right Aligned Label", Label.RIGHT);

Label label3 = new Label("Center Aligned Label", 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.
*/

Label label4 = new Label("Set Center Alignment");


add(label4);
label4.setAlignment(Label.CENTER);

}
}

Set label disable

import java.applet.Applet;
import java.awt.Label;

/*
<applet code="DisableLabelExample" width=100 height=200>
</applet>
*/

public class DisableLabelExample extends Applet{

public void init(){


//create labels
Label label1 = new Label("Label 1");
Label label2 = new Label("Label 2");

//add labels
add(label1);
add(label2);

/*
* To disable a label, use
* void setEnabled(Boolean enable)
* method.
*/

label2.setEnabled(false);
}
}

Set background to label

import java.applet.Applet;
import java.awt.Color;
import java.awt.Label;

/*
<applet code="ChangeBackgroundColor" width=100 height=200>
</applet>
*/

public class ChangeBackgroundColor extends Applet{

public void init(){


//create labels
Label label1 = new Label("Label 1");
Label label2 = new Label("Label 2");

//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);

Check if label is enabled or not

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Label;

/*
<applet code="DetermineIfLabelEnabled" width=100 height=200>
</applet>
*/

public class DetermineIfLabelEnabled extends Applet{

boolean isLabel1Enabled;
boolean isLabel2Enabled;

public void init(){


//create labels
Label label1 = new Label("Label 1");
Label label2 = new Label("Label 2");

//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();

public void paint(Graphics g){


g.drawString("Is label 1 enabled? " + isLabel1Enabled, 10,50);
g.drawString("Is label 2 enabled? " + isLabel2Enabled, 10,70);
}

Create list of items

import java.applet.Applet;
import java.awt.List;

/*
<applet code="CreateListExample" width=200 height=200>
</applet>
*/
public class CreateListExample extends Applet{

public void init(){

/*
* Create list using
* List() or
* List(int visibleRows) or
* List(int visibleRows, boolean multiSelect)
* constructor of AWT List class.
*/

//this list will have 5 visible rows


List list = new List(5);

/*
* 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);
}
}

Multi select on list

import java.applet.Applet;
import java.awt.List;

/*
<applet code="CreateMultiSelectListExample" width=200 height=200>
</applet>
*/
public class CreateMultiSelectListExample extends Applet{

public void init(){

/*
* Create multi-select list using
* List(int visibleRows, boolean multiSelect)
* constructor of AWT List class.
*/

//this list will have 5 visible rows


List list = new List(5, true);

//add items to a list


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);
}
}

Get selected items from 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>
*/

public class GetSelectedItemFromMultiSelectExample extends Applet implements ItemListener{

List list = null;

public void init(){

//create a multi select list


list = new List(5, true);

//add items to a list


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);

//add listener
list.addItemListener(this);

public void paint(Graphics g){


/*
* To get selected items from a multi select list, use
* String[] getSelectedItems()
* method of AWT List class.
*
* NOTE : getSelectedItems returns array of zero length if no
* items were selected.
*/
String[] items = list.getSelectedItems();
String msg = "";

for(int i=0; i < items.length; i++){


msg = items[i] + " " + msg;
}

g.drawString("Selected Items: "+ msg, 10, 120);


}

public void itemStateChanged(ItemEvent ie) {


repaint();
}

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>
*/

public class GetSelectedItemFromSingleSelectExample extends Applet implements ItemListener{

List list = null;

public void init(){

//create a single select list


list = new List(5, false);

//add items to a list


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);

//add listener
list.addItemListener(this);
}

public void paint(Graphics g){


/*
* To get selected item from a single select list, use
* String getSelectedItem()
* method of AWT List class.
*
* NOTE : getSelectedItem method returns null if no items were selected or
* multiple items were selected.
*/

g.drawString("Selected Item: "+ list.getSelectedItem(), 10, 120);


}

public void itemStateChanged(ItemEvent arg0) {


repaint();
}

Vous aimerez peut-être aussi