Java: Java Applet's and the Web



Java Applet's and the Web


Assumed Knowledge: Basic HTML knowledge
Information: By the time you're finished follow this tutorial, You will be able to write basic applets, create a minimum requirement HTML containers and put your applet on the internet (through HTML).


What is an Applet used for..?
Applets are used to provide interactive features to web applications that cannot be provided by HTML alone. They can capture mouse input and also have controls like buttons or check boxes. In response to the user action an applet can change the provided graphic content

I'm using Eclipse IDE for this tutorial
To start this tutorial, Open your java IDE, create a new project and make a new class file, I will call mine appletTest.java. Once we have that sorted we will add a few lines to our project,

Starting with the imports we will use.
import java.awt.*;
import java.applet.*;

What are these imports..?
The java.awt or abstract window toolkit package contains all of the classes for creating user interfaces and for painting graphics and images.
The java.applet package contains all the classes for creating applets.

Next, we'll add the extensions to the class.
public class appletTest extends Applet {

       
}

Adding this Applet extension to the class allows us to edit to create and edit an applet (this is one of the classes in the java.applet package).

We will now add the graphics method.
        public void paint (Graphics g) {

                        g
.drawRect(10, 10, 300, 100);

                        g
.drawString("Basic Java Applet", 20, 25);

       
}

By creating this method using the information in the brackets ( ) allows us to add graphical content to our applet (Graphics is apart of the awt package). g.drawRect allows the user to draw a basic rectangle at specific X and Y coords, and at specific Width's and Height's.

Creating the HTML container

A Container is an object that stores other objects (its elements), and that has methods for accessing its elements. In particular, every type that is a model of Container has an associated iterator type that can be used to iterate through the Container's elements.

This is the process to create a HTML file in the Eclipse IDE, by all means, a previous made one can be dragged and droped.

Posted Image

Posted Image

I will use the same name as i did before, appletTest.html

Once we have made our .html file, we will now want to edit the contents, Once again this is the process to edit the HTML file in the Eclipse IDE, This can be done by selecting the file and opening it with notepad or other software.

Posted Image

Let's create our HTML container,

<html>
<head><title>Basic Applet</title></head>
<body><applet codebase = "." code = "appletTest.class" name = "Basic Applet" width = "500" height = "500" hspace = "0" vspace = "0" align = "middle"></applet>
</body>
</html>

Make sure you have debugged the .java file, and that it has created a .class file. (this should be with the .class file)

once you've have completed this, its time to view the final result,

Posted Image

    No comments:

    Post a Comment