Java programming with applet

Lesson: Java Applets

This lesson discusses the basics of Java applets, how to develop applets that interact richly with their environment, and how to deploy applets.

A Java applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a web page and runs in the context of a browser. An applet must be a subclass of the java.applet.Applet class. The Applet class provides the standard interface between the applet and the browser environment.

Swing provides a special subclass of the Applet class called javax.swing.JApplet . The JApplet class should be used for all applets that use Swing components to construct their graphical user interfaces (GUIs).

The browser’s Java Plug-in software manages the lifecycle of an applet.

Use a web server to test the examples in this lesson. The use of local applets is not recommended, and local applets are blocked when the security level setting in the Java Control Panel is set to High or Very High.

Note: To run the applets, use IE mode on Microsoft Edge. See Microsoft Edge + Internet Explorer mode: Getting Started guide.

Note: Please make sure you have at least Java SE Development Kit (JDK) 6 update 10 release on your client machine before proceeding further. You will need this to view the sample rich internet applications and read subsequent sections without interruptions.

Источник

Java Applet Basics

Note:
java.applet package package has been deprecated in Java 9 and later versions,as applets are no longer widely used on the web.

Let’s understand first how many Package does GUI support:

Throwback of making GUI application:
Java was launched on 23-Jan-1996(JDK 1.0) and at that time it only supported CUI(Character User Interface) application. But in 1996 VB(Visual Basic) of Microsoft was preferred for GUI programming. So the Java developers in hurry(i.e within 7 days) have given the support for GUI from Operating System(OS). Now, the components like button, etc. were platform-dependent(i.e in each platform there will be different size, shape button). But they did the intersection of such components from all platforms and gave a small library which contains these intersections and it is available in AWT(Abstract Window Toolkit) technology but it doesn’t have advanced features like dialogue box, etc.

Now to run Applet, java needs a browser and at that time only “Internet Explorer” was there of Microsoft but Microsoft believes in monopoly. So “SUN Micro-System”(the company which developed Java) contracted with other company known as “Netscape”(which developed Java Script) and now the “Netscape” company is also known as “Mozilla Firefox” which we all know is a browser. Now, these two companies have developed a technology called “SWING” and the benefit is that the SWING components are produced by Java itself. Therefore now it is platform-independent as well as some additional features have also been added which were not in AWT technology. So we can say that SWING is much more advanced as compared to AWT technology.

Читайте также:  Обновить css до последней версии no steam

What is Applet?
An applet is a Java program that can be embedded into a web page. It runs inside the web browser and works at client side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web server.
Applets are used to make the website more dynamic and entertaining.

Important points :

  1. All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.
  2. Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer.
  3. In general, execution of an applet does not begin at main() method.
  4. Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString().

Life cycle of an applet :

It is important to understand the order in which the various methods shown in the above image are called. When an applet begins, the following methods are called, in this sequence:

When an applet is terminated, the following sequence of method calls takes place:

Let’s look more closely at these methods.

1. init( ) : The init( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet.

2. start( ) : The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Note that init( ) is called once i.e. when the first time an applet is loaded whereas start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).

3. paint( ) : The paint( ) method is called each time an AWT-based applet’s output must be redrawn. This situation can occur for several reasons. For example, the window in which the applet is running may be overwritten by another window and then uncovered. Or the applet window may be minimized and then restored.
paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called.
The paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required.
Note: This is the only method among all the method mention above, which is parameterized. It’s prototype is
public void paint(Graphics g)
where g is an object reference of class Graphic.

Читайте также:  Админ-панель

Now the Question Arises:

Q. In the prototype of paint() method, we have created an object reference without creating its object. But how is it possible to create object reference without creating its object?

Ans. Whenever we pass object reference in arguments then the object will be provided by its caller itself. In this case the caller of paint() method is browser, so it will provide an object. The same thing happens when we create a very basic program in normal Java programs. For Example:

public static void main(String []args)<>

Here we have created an object reference without creating its object but it still runs because it’s caller,i.e JVM will provide it with an object.

4. stop( ) : The stop( ) method is called when a web browser leaves the HTML document containing the applet—when it goes to another page, for example. When stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.

5. destroy( ) : The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The stop( ) method is always called before destroy( ).

Creating Hello World applet :
Let’s begin with the HelloWorld applet :

Источник

Developing an Applet

An application designed using component-based architecture can be developed into a Java applet. Consider the example of a Java applet with a Swing-based graphical user interface (GUI). With component-based design, the GUI can be built with smaller building blocks or components. The following general steps are used to create an applet GUI:

  • Create a class MyTopJPanel that is a subclass of javax.swing.JPanel . Lay out your applet’s GUI components in the constructor of the MyTopJPanel class.
  • Create a class called MyApplet that is a subclass of javax.swing.JApplet .
  • In the init method of MyApplet , instantiate MyTopJPanel and set it as the applet’s content pane.

The following sections explore these steps in greater detail by using the Dynamic Tree Demo applet. If you are not familiar with Swing, see Creating a GUI with Swing to learn more about using Swing GUI components.

Читайте также:  Тег HTML, атрибут xmlns

Note: If you don’t see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.

Creating the Top JPanel Class

Create a class that is a subclass of JPanel . This top JPanel acts as a container for all your other UI components. In the following example, the DynamicTreePanel class is the topmost JPanel . The constructor of the DynamicTreePanel class invokes other methods to create and lay out the UI controls properly.

public class DynamicTreePanel extends JPanel implements ActionListener < private int newNodeSuffix = 1; private static String ADD_COMMAND = "add"; private static String REMOVE_COMMAND = "remove"; private static String CLEAR_COMMAND = "clear"; private DynamicTree treePanel; public DynamicTreePanel() < super(new BorderLayout()); //Create the components. treePanel = new DynamicTree(); populateTree(treePanel); JButton addButton = new JButton("Add"); addButton.setActionCommand(ADD_COMMAND); addButton.addActionListener(this); JButton removeButton = new JButton("Remove"); // . JButton clearButton = new JButton("Clear"); // . //Lay everything out. treePanel.setPreferredSize( new Dimension(300, 150)); add(treePanel, BorderLayout.CENTER); JPanel panel = new JPanel(new GridLayout(0,3)); panel.add(addButton); panel.add(removeButton); panel.add(clearButton); add(panel, BorderLayout.SOUTH); >// . >

Creating the Applet

For a Java applet that has a Swing-based GUI, create a class that is a subclass of javax.swing.JApplet . An applet that does not contain a Swing-based GUI can extend the java.applet.Applet class.

Override the applet’s init method to instantiate your top JPanel class and create the applet’s GUI. The init method of the DynamicTreeApplet class invokes the createGUI method in the AWT Event Dispatcher thread.

package appletComponentArch; import javax.swing.JApplet; import javax.swing.SwingUtilities; public class DynamicTreeApplet extends JApplet < //Called when this applet is loaded into the browser. public void init() < //Execute a job on the event-dispatching thread; creating this applet's GUI. try < SwingUtilities.invokeAndWait(new Runnable() < public void run() < createGUI(); >>); > catch (Exception e) < System.err.println("createGUI didn't complete successfully"); >> private void createGUI() < //Create and set up the content pane. DynamicTreePanel newContentPane = new DynamicTreePanel(); newContentPane.setOpaque(true); setContentPane(newContentPane); >>

Benefits of Separating Core Functionality From the Final Deployment Mechanism

Another way to create an applet is to just remove the layer of abstraction (separate top JPanel ) and lay out all the controls in the applet’s init method itself. The downside to creating the GUI directly in the applet is that it will now be more difficult to deploy your functionality as a Java Web Start application, if you choose to do so later.

In the Dynamic Tree Demo example, the core functionality resides in the DynamicTreePanel class. It is now trivial to drop the DynamicTreePanel class into a JFrame and deploy as a Java Web Start application.

Hence, to preserve portability and keep deployment options open, follow component-based design as described on this page.

Download source code for the Dynamic Tree Demo Applet example to experiment further.

Источник

Оцените статью