Have you had a look at Sun Tutorials on Applets. Very good starting point if you have just
starting programming in Java.
Here is an example of a very very basic applet, that will demonstrate the various methods and when try are called once you have opened the page.
Please save in a file
MyApplet.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// simple applet
public class MyApplet extends JApplet {
private String name;
/*
* Constructor for MyApplet extends JApplet
*/
public MyApplet(){
} //-- ends class constructor
public void init() {
this.name = new String();
this.name = JOptionPane.showInputDialog( null, "What is your name? " );
System.out.println("+++++++++++++ Applet INITIALIZED +++++++++++++");
System.out.println("You said that your name was: " + this.name );
} //-- ends instance method init
// runs when you pages opens or when you refresh page
public void start() {
System.out.println("*************** Applet is START ***************");
} //-- ends instnce method start
// runs every time your referesh page...or when you try and
// and resizes browser
public void paint( Graphics g ) {
System.out.println("--------------- Applet is PAINTING ---------------");
g.drawString( "You said your name was: " + this.name, 10, 10 );
} //-- ends instance method paint
// runs when user minimizes browser or when navigates away from page
public void stop() {
System.out.println(" _________ Applet STOPPED _______");
} //-- ends instance method stop
// runs when applet is destroyed...or when user navigates to another page
public void destroy() {
System.out.println("%%%%%%%%%%%% Applet DESTORYED %%%%%%%%%%%%");
} //-- ends instance method destroy
} //-- ends class definition
Once you have done that, then please create HTML page...and dump the following contents inside it. I have called it
MyApplet.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title>My Applet</title>
</head>
<body>
<h1>My Applet</h1>
<applet code="MyApplet.class" CODEBASE="" width=500 height=500>
<p>Sorry, but you need a java enabled browser in order to view the applet.</p>
</applet>
</body>
</html>
Instead of viewing the applet in a web browser, please view it using the appletviewer command...so that you can see how all the methods are run...etc
Of course, you should compile the applet first.
and then run the appletviewer by executing the following command
appletviewer MyApplet.html
You should see from the command console..the sequence of all the methods and how they are executed. play around with the appletviewer...by minimizing, maximinzing, etc...just to see how all the methods are fired...and in what sequence...etc
Greetings
Albert