
Applets
How to add applets to a website
Type the code into html, <APPLET CODE = "NameofClass.class" WIDTH=400 HEIGHT = 100></APPLET>
Then store all the class files into the same directory as your web files.
What is an Applet?
Applet runs on web browsers, it's
very similar to swing applications. The differences are, instead of extending
JFrame class, applets extend the JApplet class. In JFrame, the space of a window
is managed by the host operating system's window system and in applet, the space
of the window is manage by the browser.
Here are the two methods that are called automatically by the applet:
init();
paint();
We will see examples on both, lets start with an example on init().
/*
This is a simple applet program
It prints out Hello World
*
*/
import javax.swing.*;
import java.awt.*;
public class showApplet extends JApplet
{
private JLabel msg;
private JPanel panel;
public void init()
{
// Sets the size of the applet
setSize(200, 200);
// Create a panel
panel = new JPanel();
//
Create a message label
msg = new JLabel("Hello World");
// Add the label to the panel
panel.add(msg);
// Add the panel to the content pane
add(panel);
}
}

As we can see from the above example, Applets are very similar from GUI, the only difference is it doesn't need a main method. Now lets see an example on how to use the paint() method to pain in applet.
/*
The program paint shapes in the applet
*
*/
import javax.swing.*;
import java.awt.Graphics;
public class ShapesApplet extends JApplet
{
public void paint( Graphics g )
{
// Inherits from the JApplet paint() method
super.paint( g );
// Draws a rectangle (x position, y
position, width, height)
g.drawRect( 50, 40, 30, 60 );
// Fills a rectangle (x position, y
position, width, height)
g.fillRect( 10, 80, 30, 10 );
// Fills a oval (x position, y position, width,
height)
g.fillOval( 90, 50, 40, 90 );
//
Draw a ova (x position, y position, width, height)
g.drawOval( 100, 300, 50, 40 );
}
}

Now lets see an example that we used in the Swing
chapter, then we can see how similar JApplet and swings are. If we know one then
we know both.
/*
This is a small JApplet starbucks order program
It demonstrate the use of BevelBorder, JCheckBox, & JRadioButton
*
*/
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class StarbucksApplet extends JApplet
{
JButton ok;
JPanel radiopanel;
JPanel checkboxpanel;
JPanel mainpanel;
JRadioButton tall, grande, venti;
JCheckBox ice_green, coffee, orange_juice;
public void init()
{
// Sets the size of the Applet
setSize(550, 220);
// Creates a button listener
ButtonListener bl = new ButtonListener();
// Create two panels one for JRadioButton and one for the main panel
radiopanel = new JPanel();
mainpanel = new JPanel();
// Creates a border for JRadioButton
Border b = BorderFactory.createTitledBorder("Size");
// Adds the border to the radio panel
radiopanel.setBorder(b);
// Group the radio buttons together
ButtonGroup radiogroup = new ButtonGroup();
// Add the radio buttons to the panel and group
tall = new JRadioButton("Tall");
radiopanel.add(tall);
radiogroup.add(tall);
grande = new JRadioButton("Grande");
radiopanel.add(grande);
radiogroup.add(grande);
venti = new JRadioButton("Venti");
radiopanel.add(venti);
radiogroup.add(venti);
// Adds the JRadioButton panel to the main panel
mainpanel.add(radiopanel);
// Creates the JCheckBox panel
checkboxpanel = new JPanel();
// Creates a border for the check box panel
Border b2 = BorderFactory.createTitledBorder("Drinks");
// Adds the border to the check box
checkboxpanel.setBorder(b2);
// Create JCheckbox and add the check boxes into the panel
ice_green = new JCheckBox("Ice Green Tea");
checkboxpanel.add(ice_green);
coffee = new JCheckBox("Coffee");
checkboxpanel.add(coffee);
orange_juice = new JCheckBox("Orange Juice");
checkboxpanel.add(orange_juice);
// adds the check box panel to the main panel
mainpanel.add(checkboxpanel);
// Creates a ok button
ok = new JButton("OK");
/*
Creates a listener for ok button to listen for any events
We pass the bl into the parameter, bl is created from the very beginning
*
*/
ok.addActionListener(bl);
// Adds the ok button into the main panel
mainpanel.add(ok);
// Adds the main panel into the content pane
add(mainpanel);
}
/*
Inside here, all the events of JCheckBox and JRadioButton will be handled
*
*/
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
String drinks = "";
// Determine what is been selected in JCheck Box
if(ice_green.isSelected())
{
drinks += "Ice Green Tea\n";
}
if(coffee.isSelected())
{
drinks += "Coffee\n";
}
if(orange_juice.isSelected())
{
drinks += "Orange Juice\n";
}
// Determine what is been selected in JRadio Button
String msg = "You ordered a ";
if(tall.isSelected())
{
msg += " tall " + drinks;
}
if(grande.isSelected())
{
msg += " grande " + drinks;
}
if(venti.isSelected())
{
msg += " venti " + drinks;
}
// Outputs the order in JOptionPane
JOptionPane.showMessageDialog(null, msg);
/*
Deselected all the check box
Selects tall in radio button
*
*/
ice_green.setSelected(false);
coffee.setSelected(false);
orange_juice.setSelected(false);
tall.setSelected(true);
}
}
}
}

ClickHere to download StarbucksApplet.java