JAVA PROGRAM
Program to display smile face using applet |Java Program|
Introduction
In the world of programming, there's often a joy in simplicity. Creating something visually pleasing with just a few lines of code can be immensely satisfying. In this blog post, we'll explore how to create a simple smiley face applet using Java.
What is an Applet?
An applet is a small Java program that runs within a web browser. It's typically used to create interactive web applications or to display dynamic content on a webpage.
Creating the Smile Face Applet:
To create our smiley face applet, we'll use Java's built-in graphics capabilities. Here's the code for our smileface.java file:
smileface.java
import java.applet.*;
import java.awt.*;
public class smileface extends Applet {
public void paint(Graphics g) {
g.setColor(Color.black);
g.drawOval(20,40,250,250); // draw face
g.fillOval(50,100,50,70); // draw left eye
g.fillOval(185,100,50,70); // draw right eye
g.drawLine(138,160,138,210); // draw nose
g.drawLine(138,210,155,210); // draw mouth
g.drawArc(95,156,100,100,180,180); // draw smile
}
}
Explanation
- We begin by importing the necessary packages: java.applet.* and java.awt.*.
- We create a class named smileface that extends Applet.
- Inside the paint method, we use Graphics object g to draw various shapes:
- A black oval representing the face with drawOval.
- Two filled ovals for the eyes.
- Two lines for the nose.
- An arc for the smile.
Creating the HTML File:
To display our applet in a web browser, we need an HTML file. Here's the code for our smilface.html file:
smilface.html
<html>
<body>
<applet code="smileface.class" height="300" width="320">
</applet>
</body>
</html>
Running the Applet:
To run our smiley face applet, simply compile the smileface.java file to generate the smileface.class file. Then, open the smilface.html file in a web browser. You should see a simple smiley face displayed on the webpage.
Conclusion
In this blog post, we've explored how to create a simple smiley face applet using Java. Despite its simplicity, this example demonstrates the power of Java's graphics capabilities and the ease with which we can create visually appealing content. So next time you're looking to add some fun to your web page, consider creating your own Java applet!
Post a Comment
0 Comments