Where is applet used


Next:GUI elements Up:Basics of Java Previous:Creating menu bars & nbsp Contents & nbsp index

Applets are small Java programs that can be executed directly in web browsers. This means that these programs can be integrated into any HTML document. There are, however, some differences compared to normal applications. To program applets, the package "` java.applet. * "'Must be imported. If swing elements are required, a JApplet must be implemented. However, the package "` javax.swing.JApplet "'is used for this. The classes with the actual program code then expand applet or JApplet.
A difference to normal applications can be found in the use of the user interface. In applications, a frame must first be created for the inclusion of the components. A few panels are then usually placed in these. In contrast to this, the elements can be inserted directly into applets.

import java.applet. *; import java.awt.Graphics; public class HelloApplet extends Applet {public void paint (Graphics g) {g.drawString ("Hello World", 0, 10); }}

The most important difference to "normal" Java applications is that applets do not have a main method. Instead, there are four methods here that affect program execution milestones. These are the methods:

  1. "'Init"'
    This method is called as soon as the applet is loaded for the first time. All initializations required for executing the program should take place here.
  2. "'Start"'
    The start method is executed as soon as the "Init" 'method is completed, or when the applet comes back into the field of view of the viewer. This restart is necessary if the applet has been stopped.
  3. "Stop" '
    The "Stop" method is called when the applet disappears from the view of the user. This can be required if e.g. an animation is being carried out. As soon as the user can no longer look at the animation, the computing power required for this can be released. It does this by pausing the animation. As soon as the applet comes back into the user's field of vision, the "Start" method is called again.
  4. "Destroy" '
    When the user completely leaves the page with the applet, the "Destroy" method is called. All resources used by the program can be released here. If the applet is started again later, it will also be reinitialized.

A sample application for these four methods outputs the name of the function each time the method is called. However, in order to really see all output, a console must be opened because the applet is closed before the Destroy statement can be issued.

import javax.swing.JApplet; import java.awt.Graphics; public class Milestones extends JApplet {StringBuffer s; // method call during initialization public void init () {s = new StringBuffer (); s.append ("Init!"); System.out.println (s + ""); } // method call when the applet comes into view public void start () {s.append ("Start!"); System.out.println (s + ""); } // method call when the applet leaves the field of view public void stop () {s.append ("Stop!"); System.out.println (s + ""); } // method call when the applet is closed public void destroy () {s.append ("Destroy!"); System.out.println (s + ""); s = zero; } // Method for drawing the content public void paint (Graphics g) {g.drawString (s.toString (), 0, 10); }}
However, it is also possible to write a program in Java in such a way that it can be started both as an applet and as an application. For this purpose, the actual program part is written in a class that is derived from JPanel. The resulting panel must then be inserted in the init method in the applet and in a main method in a frame of the application.
public class AppletApp extends JApplet {public void init () {this.add (new MyPanel ()); } public static void main (String [] args) {JFrame window = new JFrame ("application window"); fenster.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); fenster.setSize (200,200); windows.add (new MyPanel ()); fenster.setVisible (true); }} class MeinPanel extends JPanel implements ActionListener {private static int count = 0; private JLabel advertisement; public MyPanel () {this.setLayout (new BorderLayout ()); JButton knopf = new JButton ("Push me!"); knopf.setActionCommand ("knopf"); knopf.addActionListener (this); this.add (button, BorderLayout.SOUTH); display = new JLabel ("" + count); this.add (display, BorderLayout.CENTER); } public void actionPerformed (ActionEvent evt) {count ++; display.setText ("" + count); }}
The main benefit of applets is that they can be easily integrated into web pages. The tag in HTML pages is used for this. The name of the executable file, usually with the ending "` .class "', must be specified here. The size of the applet must also be specified. In addition, a horizontal or vertical distance to the edge of the page can be specified with hgap or vgap. If the applet cannot be displayed on a computer, the text is displayed behind old. However, this information is optional. tags are used to transfer parameters to the applet. The name of the parameter is shown under "name" 'and the value to be transferred after "value"'. This can then be queried in the applet with the getParameter method. A string is always returned or "` null "'if no value is given. import javax.swing. *; import java.awt. *; public class Parameter extends JApplet {public void init () {String text = getParameter ("textParameter"); JLabel display1 = new JLabel ("Textparameter:" + text); String number = getParameter ("numberParameter"); Integer number = 0; if (number! = zero) {try {numberValue = Integer.parseInt (number); } catch (NumberFormatException e) {}} JLabel display2 = new JLabel ("Number parameter:" + numberValue); this.setLayout (new GridLayout (2,1)); this.add (display1); this.add (display2); }}
The following HTML code can be used to integrate this applet into an HTML page and to transfer parameters. The applet is located in the "Parameter.class" file.
<applet code=Parameter.class width="200" height="200"> <param name="textParameter" value="Textstring"> <param name="zahlParameter" value="1000"> </applet>

Since applets can be easily integrated into websites, some security restrictions have been introduced. Most obvious is the caption in new windows. If a new frame is opened from a Java applet, the message "` Java Applet Window "'can be read at the bottom. This can only be switched off by the user himself. This is to prevent, for example, that password queries are simulated and thus user passwords can be requested.

import javax.swing.JApplet; import javax.swing. *; public class AppletFrame extends JApplet {public void init () {// Create a new window JFrame window = new JFrame ("Window from the applet!"); fenster.setVisible (true); fenster.setSize (100,100); fenster.setLocation (100, 100); // Insert a text field in the window JLabel text = new JLabel ("This can be a" + "normal window!"); fenster.getContentPane (). add (text); }}
There is another restriction when reading or writing to the file system. Applets are not allowed to write directly to the file system of the host computer. In addition, applets are not allowed to establish new network connections or start applications on the host computer.

Next:GUI elements Up:Basics of Java Previous:Creating menu bars & nbsp Contents & nbsp index Rainer Friesen, Markus Stollenwerk and Daniel Valentin, 2007-03-06