Hello world applet in java

Java Applet Basics

Note:
java.applet package package has been deprecated in Java 9 and later versions,as applets are no longer widely used on the web.

Let’s understand first how many Package does GUI support:

Throwback of making GUI application:
Java was launched on 23-Jan-1996(JDK 1.0) and at that time it only supported CUI(Character User Interface) application. But in 1996 VB(Visual Basic) of Microsoft was preferred for GUI programming. So the Java developers in hurry(i.e within 7 days) have given the support for GUI from Operating System(OS). Now, the components like button, etc. were platform-dependent(i.e in each platform there will be different size, shape button). But they did the intersection of such components from all platforms and gave a small library which contains these intersections and it is available in AWT(Abstract Window Toolkit) technology but it doesn’t have advanced features like dialogue box, etc.

Now to run Applet, java needs a browser and at that time only “Internet Explorer” was there of Microsoft but Microsoft believes in monopoly. So “SUN Micro-System”(the company which developed Java) contracted with other company known as “Netscape”(which developed Java Script) and now the “Netscape” company is also known as “Mozilla Firefox” which we all know is a browser. Now, these two companies have developed a technology called “SWING” and the benefit is that the SWING components are produced by Java itself. Therefore now it is platform-independent as well as some additional features have also been added which were not in AWT technology. So we can say that SWING is much more advanced as compared to AWT technology.

What is Applet?
An applet is a Java program that can be embedded into a web page. It runs inside the web browser and works at client side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web server.
Applets are used to make the website more dynamic and entertaining.

Important points :

  1. All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.
  2. Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer.
  3. In general, execution of an applet does not begin at main() method.
  4. Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString().
Читайте также:  Javascript вывести часовой пояс

Life cycle of an applet :

It is important to understand the order in which the various methods shown in the above image are called. When an applet begins, the following methods are called, in this sequence:

When an applet is terminated, the following sequence of method calls takes place:

Let’s look more closely at these methods.

1. init( ) : The init( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet.

2. start( ) : The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Note that init( ) is called once i.e. when the first time an applet is loaded whereas start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).

3. paint( ) : The paint( ) method is called each time an AWT-based applet’s output must be redrawn. This situation can occur for several reasons. For example, the window in which the applet is running may be overwritten by another window and then uncovered. Or the applet window may be minimized and then restored.
paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called.
The paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required.
Note: This is the only method among all the method mention above, which is parameterized. It’s prototype is
public void paint(Graphics g)
where g is an object reference of class Graphic.

Now the Question Arises:

Q. In the prototype of paint() method, we have created an object reference without creating its object. But how is it possible to create object reference without creating its object?

Ans. Whenever we pass object reference in arguments then the object will be provided by its caller itself. In this case the caller of paint() method is browser, so it will provide an object. The same thing happens when we create a very basic program in normal Java programs. For Example:

public static void main(String []args)<>

Here we have created an object reference without creating its object but it still runs because it’s caller,i.e JVM will provide it with an object.

4. stop( ) : The stop( ) method is called when a web browser leaves the HTML document containing the applet—when it goes to another page, for example. When stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.

Читайте также:  Как создать класс javascript

5. destroy( ) : The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The stop( ) method is always called before destroy( ).

Creating Hello World applet :
Let’s begin with the HelloWorld applet :

Источник

The «Hello World» Applet

Compile the source file using the Java compiler.

If the compilation succeeds, the compiler creates a file named HelloWorld.class in the same directory (folder) as the Java source file ( HelloWorld.java ). This class file contains Java bytecodes.

If the compilation fails, make sure you typed in and named the program exactly as shown above. If you can’t find the problem, see Common Compiler and Interpreter Problems .

Create an HTML File that Includes the Applet

Using a text editor, create a file named Hello.html in the same directory that contains HelloWorld.class . This HTML file should contain the following text:

   A Simple Program  Here is the output of my program:  

Run the Applet

To run the applet, you need to load the HTML file into an application that can run Java applets. This application might be a Java-compatible browser or another Java applet viewing program, such as the Applet Viewer provided in the JDK. To load the HTML file, you usually need to tell the application the URL of the HTML file you’ve created. For example, you might enter something like the following into a browser’s URL or Location field:

file:/home/kwalrath/HTML/Hello.html

Once you’ve successfully completed these steps, you should see something like this in the browser window:

What Next?

  • Continue on in this lesson to learn about the anatomy of applets and about importing classes.
  • Go back to The Anatomy of a Java Application if you haven’t already read it and you’re interested in a quick introduction to key Java concepts.
  • Learn more about writing applets by going to the Writing Applets trail.
  • Go back to the Trail Map to get an overview of the trails you can follow.

Источник

Overview

In this tutorial, we will create a Java Hello World Applet. The applet displays the greeting “Hello World!”. A Java Applet is a special program that runs on a Java-enabled web browser. The applet runs in a web browser such as Internet Explorer, Firefox, etc. The java.applet.Applet class provides the standard interface between the browser and the applet code.

Java Applet Example

The steps to create a Java Applet are as follows:

To create the applet, we’ll perform the following steps.

  • Create a Java source
  • Compile the Java file
  • Create an HTML file
  • Run the Java Applet

Create a Java source

Open a text editor and type the following code. Save the file as HelloWorld.java

import java.applet.*; import java.awt.*; /** * The HelloWorld applet that * displays "Hello World!" message. */ public class HelloWorld extends Applet < public void paint(Graphics g) < g.drawString("Hello world! \n Java Tutorials - www.TestingDocs.com", 250, 250); >>

Compile the Java file

Open the command prompt and type the following command.

Create an HTML file

Create an HTML file with the following command.

Читайте также:  Рекурсивный ввод массива java

Java Hello World Applet

Run the Java Applet

We can run the applet using the applet viewer tool. On Windows operating system the tool can be found under the JDK bin folder. ( appletviewer.exe)

That’s it. We have successfully created and run a sample Java applet.

Java Tutorials

Java Tutorial on this website:

For more information on Java, visit the official website :

Источник

The «Hello World» Applet

Compile the source file using the Java compiler.

If compilation succeeds, the compiler creates a file named HelloWorld.class . If compilation fails, make sure you typed in and named the program exactly as shown above.

Create an HTML File that Includes the Applet

Create a file named Hello.html in the same directory that contains HelloWorld.class . This HTML file should contain the following text:

   A Simple Program  Here is the output of my program:  

Load the HTML File

Load the HTML file into an application that can run Java applets. This application might be a Java-compatible browser or another Java applet viewing program, such as the Applet Viewer provided in the JDK. To load the HTML file, you usually need to tell the application the URL of the HTML file you’ve created. For example, you might enter something like the following into a browser’s URL or Location field:

file:/home/kwalrath/HTML/Hello.html

Once you’ve successfully completed these steps, you should see something like this in the browser window:

Now What?

  • Continue on in this lesson to learn about the anatomy of applets and about importing classes.
  • Return to the Trail Map to get an overview of the trails you can follow.
  • Learn more about writing applets by going to the Writing Applets trail.

Источник

Hello World Applet Example

Example Output

You may also like

Load New HTML File Using Applet Context Example

Fill Rectangle & Square in Applet Window Example

Get Foreground Color Of an Applet Window Example

Set Foreground Color Of an Applet Window Example

Draw Line in Applet Window Example

Draw Oval & Circle in Applet Window Example

Generate Bouncing Lines Using Applet Example

Get Applet’s Document URL or Directory Base Example

Draw Dots at Random Locations in an Applet Example

Applet Life Cycle Example

Draw 3D Rectangle & Square in Applet Window Example

Using Applet dimension to print center aligned text Example

Add Comment

Cancel reply

Hello all I am super new to Java and already have problems with codes in general, well i am still learning! I run the above code and seems i am doing something wrong because i get an error:
=============
———- Run ———-Error: Main method not found in class HelloWorldApplet, please define the main method as: public static void main(String[] args)
Output completed (0 sec consumed) – Normal Termination
=============
Can you explain why i got this error and what can i do to fix it?
Compile correct but when i run the error shows. Have jdk1.7.0_07 installed correct, able to run other codes but not this one, try in NetBeans, Eclipse, EditPlus but nothing Please have a look and let me know where am i wrong Best regards
Gabi

hi i had try the coding above where i used to get the error
java.lang.ClassNotFoundException: Hello World

Источник

Оцените статью