Program Description:
Today we are going to create our first basic java applet program using JApplet component. This is just a very short and simple java code that you can really understand.
JApplet is a subclass of Applet, a class in the java.applet package. An applet is a visual window inside a webpage that means they need to be run as part of a World Wide Web page.
In creating an Applet it involves two files the java file where our java codes resides of course and the HTML file where our java codes will be running using the ".class" file.
In creating our class it should always extends to JApplet. Example:
public class myFirstApplet extends JApplet {
}
then next to our java applet code is our HTML code where we embed our java code using the ".class" file. Here is the basic code on how to embed our java applet code in a webpage.
1. Starts with an opening HTML tag <html>
2. Followed by an opening Applet tag <applet>
3. specify your java applet code by locating the ".class" file which is usually created if you compile your java codes.
4. Specify the width and the height of your applet in the webpage.
5. Closing the applet tag </applet>
6. Closing the html tag </html>
Note: Compile your java code and your HTML code then run the HTML file. I am using JCreatorPro so I automatically load the HTML file, then compile, and run. See output below:
Output:
Code:
Java Code
/**
* File: myFirstApplet.java
* Tiltle: My First JApplet | How to create an Applet
* Author: http://java-code-complete.blogspot.com/
*/
//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
public class myFirstApplet extends JApplet {
//Creating paint method so we can draw text on applet's background
public void paint(Graphics g) {
//Call the method paint
super.paint(g);
//Draw 3 strings at different locations
g.drawString("Welcome to my First Java Applet",30,30);
g.drawString("Welcome to Java Programming",30,50);
g.drawString("Thank you for Visiting: http://java-code-complete.blogspot.com",30,90);
}
}
HTML Code:
No comments:
Post a Comment