Going Old School Java For Day 16

in #java6 years ago

Time For A Quick Look At Java Applets

abstract-access-close-up-1089438.jpg
Image courtesty of pexels.com

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.

What Did I Learn Today


An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.
The differences between an applet and a usual java app are

  • An applet is a Java class that extends the java.applet.Applet class.
  • A main() method is not invoked on an applet, and an applet class will not define main().
  • Applets are designed to be embedded within an HTML page.
  • When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine.
  • A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.
  • The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime.
  • Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed.
  • Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.

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.

Code For The Day


First you need to set up the java code as you normally would

  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:
hellowapplet.png

I'll be posting daily, sharing my experiences on my “1 Month of Java Code” experiences”, my previous post on day 16 can be found below, so feel free to have a look:
https://steemit.com/java/@run.vince.run/java-keyboard-input-for-day-16-of-javatober

If you have found this post useful or interesting, please consider Commenting, Upvoting, Following and/or Resteeming

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 62205.55
ETH 2397.85
USDT 1.00
SBD 2.50