Friday, December 20, 2024
HomeComputer programmingJava programmingJava: Turn Your .class into an Applet

Java: Turn Your .class into an Applet

The following Tech-Recipes tutorial describes how to modify your existing java GUIs into applets to share with the web-viewing world.


Note that most file transfers are not possible in applets. It just does not work like that. There are ways around it to do some read or write operations, but that is beyond this simple transformation tech recipe.

An applet is basically the GUI you already made, except it does not have a frame. The frame is supplied by the browser opening the .class file.

It is quite a simple task, assuming your code was written using panels instead of frames (as you should have). This will allow for easy transfer from java code to applet.

There are a few methods that are required to make an applet work:
public void init() {}
This method is run when the applet is being loaded into memory or constructed. Think of this as the applet constructor.

public void start() {}
This is run once all preparations are done, and the code is actually beginning to run. Place any splash screens or intro parts here.

public void stop() {}
This is run when the applet has been terminated, while memory is being freed and the code is stopping.

public void destroy() {}
This method is called just before a system exit, notifying that the applet has finished and cannot be run again without reloading it.

CODE Example (applet.class):

import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class applet extends JApplet{
static final int WIDTH = 500;
static final int HEIGHT = 60;

JTextField title;

public class display extends JPanel{
public display(){
Container container;
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
getContentPane().setLayout(layout);
container = getContentPane();

JLabel label1 = new JLabel("Name:");
layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(1,1,1,1);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 2.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(label1, layoutConstraints);
container.add(label1);

title = new JTextField();
layoutConstraints.gridx = 1; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 2; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(1,1,1,1);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 40.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(title, layoutConstraints);
container.add(title);
}
}

public void init() {
System.out.println("Applet initializing");
display d = new display();
getContentPane().add(d);
}
public void start() {
System.out.println("Applet starting");
}
public void stop() {
System.out.println("Applet stopping");
}
public void destroy() {
System.out.println("Applet destroyed");
}
public static void main(String args[]){
JFrame frame = new JFrame("Code Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
applet a = new applet();

a.init(); // Do what browser normally does
frame.getContentPane().add(a); // Add applet to frame

frame.setSize(WIDTH, HEIGHT); // Set the size of the frame
frame.setVisible(true); // Show the frame
}
}

*Note: I have added the JFrame information into the main of the java file. This allows us to test our program as we go along without having to run the html file.

CODE Example (runApplet.html):








A simple applet tage designating the source and display size is all that is necessary within the head of your html file.

NOTE: Your applet has more than one .class file, but the main file is the only one that needs to be called. The html and class files should be located in the same directory to ensure all .class files can be found.

Questions/Comments: [email protected]
-William. § (marvin_gohan)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!