|
This java applet tutorial is hello world example for java applet beginners. Java applets we can use anywhere in the websites. Hello world example simply demonstrates you to setup the environment for java applets.
hello.java
//copyrighted by topsourcecode.com
import javax.swing.*;
import java.awt.*;
public class hello extends JApplet
{
Font fontTitle;
Font sentence;
//Mehtod invoked when start the app
public void init()
{
fontTitle = new Font("Arial",Font.BOLD,16);
sentence = new Font("Arial",Font.ITALIC,10);
}
//Method invoked when updating the app
public void update( Graphics g )
{
}
//Method invoked when app terminated
public void stop()
{
}
//method to draw the screen
public void paint( Graphics g )
{
g.setFont(fontTitle);
//Starting x and y coordinates
g.drawString("Hello Java Applets",20,40);
//set new font to description
g.setFont(sentence);
String desc= "This tutorial good for java applets beginners from topsourcecode.com";
g.drawString(desc,20,60);
}
}
hello.html
<html>
<head>
<applet code="hello.class"
width="600" height="500">
Your browser is completely ignoring the <applet> tag!
</applet>
</head>
<body>
</body>
</html>
Compile hello.java code and put compiled .class file and hello.html file together in a folder.
Result

|