Java Program to Create a Basic Applet

This is a Java Program to Create a Basic Applet

Problem Description

We have to write a program in Java such that it creates a basic applet with some text written on the applet.

Expected Input and Output

For creating a basic applet, we can have the following set of input and output.

To View the Applet :

When the program is executed,
it is expected that an applet is created with "Hello World" written.
Problem Solution

1. To create an applet inherit the Applet class.
2. Write text on the frame, using drawString method of Graphics class.

advertisement
advertisement
Program/Source Code

Here is source code of the Java Program to create a basic applet. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /*Java Program to Create a Basic Applet*/
  2. import java.applet.*;
  3. import java.awt.*;
  4. public class Basic_Applet extends Applet
  5. {
  6.     //Function to initialize the applet
  7.     public void init()
  8.     {
  9. 	setBackground(Color.white);
  10. 	setLayout(new FlowLayout(FlowLayout.CENTER));
  11.     }
  12.     //Function to draw the string
  13.     public void paint(Graphics g)
  14.     {
  15. 	g.drawString("Hello World",100,200);
  16.     }
  17. }
  18. /*
  19. <applet code=Basic_Applet.class width=400 height=400>
  20. </applet>
  21. */

To compile and run the program use the following commands :

Note: Join free Sanfoundry classes at Telegram or Youtube
>>>javac Basic_Applet.java
>>>appletviewer Basic_Applet.java
Program Explanation

1. To create an applet, it is needed to inherit the Applet class.
2. The method public void init() is defined in the Applet class and it is used to initialize the applet.
3. The method drawString(String,int x,int y) draws a string starting from the (x,y) co-ordinate.

Runtime Test Cases

Here’s the run time test cases for creating a basic applet for different input cases.

advertisement

Test case 1 – To View the Applet.
java-program-basic-applet

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.