One of the first times I was trying to learn Java, I was using a tutorial that had you learning all the aspects of the Java languange while developing a Java Applet. I found it a good way to learn as I was actually creating something tangable with each tutorial and could see the output to the screen instead of seeing a black console output. I think it's a good way to keep people engaged but unfortunately Java Applets are a thing of the past with a lot of browsers no longer supporting them. I thought it would be cool to have a quick look at applets and how they work even though they are rarely used any more, but it may inspire someone to resurect them again.
The Java Applet is the framework that lets you build the applet and includes five main methods we use including:
(1)init for initialization of the applet, (2)start after the browser calls the initilisation, (3)stop when the user moves off the page, (4)destroy when the browser shuts down and (5)paint which is inherited from the java.awt class is called after the start method or any time the applet needs to repaint itself.
1 import java.applet.*;
2 import java.awt.*;
3
4 public class HelloWorldApplet extends Applet {
5 public void paint (Graphics g) {
6 g.drawString("Hello World", 25, 50);
7 }
8 }
Then you need to set up the html code to server the applet
1 <html>
2 <title>The Hello, World Applet</title>
3 <hr>
4 <applet code = "HelloWorldApplet.class" width = "320" height = "120">
5 If your browser was Java-enabled, a "Hello, World"
6 message would appear here.
7 </applet>
8 <hr>
9 </html>
If you have Java Applets enabled in your browser, the output will looks similar to the following image: