Drawing a Smiley in Java Applet


Java Applet is an astounding resource that grants specialists to make smart representations and liveliness inside a web program. In this educational activity, we will examine how to draw a smiley face using Java Applet. We will cover the accentuation, little by little computation, and different ways of managing to accomplish this task. Close to the completion of this article, you will have an unquestionable understanding of how to make a smiley face using Java Applet.

Syntax

To draw a smiley face in Java Applet, we really want to utilize the Illustrations class, which gives techniques to draw different shapes and pictures. The fundamental punctuation to make a smiley face is as per the following:

public void paint(Graphics g) {
   // Draw the smiley face here
}
import java.applet.Applet;
import java.awt.Graphics;

public class SmileyApplet extends Applet {
   public void paint(Graphics g) {
      // Draw the smiley face here
   }
}

Algorithm

  • To draw a smiley face in Java Applet, we can follow the accompanying bit by bit calculation:

  • Set the background color of the applet.

  • Draw a yellow circle as the face using the fillOval() method.

  • Draw two black circles for the eyes using the fillOval() method.

  • Draw a black arc as the smile using the drawArc() method.

  • Customize the smiley face by adding additional elements like eyebrows, nose, etc. (optional).

Approach 1: Basic Smiley Face

In this methodology, we will draw a smiley face utilizing fundamental shapes like circles and curves. We will keep it straightforward and center around the fundamental components.

Example

import java.applet.Applet;
import java.awt.*;

public class SmileyApplet extends Applet {
   public void paint(Graphics g) {
      setBackground(Color.white);
      g.setColor(Color.yellow);
      g.fillOval(100, 100, 200, 200);
      g.setColor(Color.black);
      g.fillOval(155, 175, 10, 20);
      g.fillOval(230, 175, 10, 20);
      g.drawArc(150, 220, 100, 50, 180, 180);
   }

   public static void main(String[] args) {
      // Create a frame
      Frame frame = new Frame("SmileyApplet Frame");

      // Create an instance of the applet
      SmileyApplet applet = new SmileyApplet();

      // Add the applet instance to the frame
      frame.add(applet);

      // Call applet's init() method
      applet.init();

      // Set frame size (applet size will be as per its preferred size)
      frame.setSize(400, 400);

      // Show the frame
      frame.setVisible(true);
      
      // Handle closing the frame
      frame.addWindowListener(new java.awt.event.WindowAdapter() {
         @Override
         public void windowClosing(java.awt.event.WindowEvent windowEvent) {
            System.exit(0);
         }
      });
   }
}

Output

Explanation

In this code, we set the foundation shade of the applet to white utilizing the setBackground() strategy. Then, we set the variety to yellow and attract a filled oval to address the face. Then, we set the variety to dark and draw two filled ovals for the eyes. At long last, we attract a bend to address the grin.

Approach 2 (Enhanced Smiley Face)

In this approach, we will enhance the smiley face by adding eyebrows and a nose. This will give the face more personality.

Example

import javax.swing.*;
import java.awt.*;

public class SmileyFaceApp extends JFrame {

   public SmileyFaceApp() {
      setTitle("Smiley Face");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(400, 400);
      setVisible(true);
   }

   public void paint(Graphics g) {
      // Set background color
      getContentPane().setBackground(Color.WHITE);

      // Draw face
      g.setColor(Color.YELLOW);
      g.fillOval(100, 100, 200, 200);

      // Draw eyebrows
      g.setColor(Color.BLACK);
      g.drawLine(145, 145, 175, 165);
      g.drawLine(235, 165, 265, 145);

      // Draw eyes
      g.setColor(Color.BLACK);
      g.fillOval(155, 175, 20, 20);
      g.fillOval(225, 175, 20, 20);

      // Draw nose
      g.setColor(Color.BLACK);
      g.fillOval(190, 205, 20, 20);

      // Draw smile
      g.setColor(Color.BLACK);
      g.drawArc(150, 175, 100, 100, 200, 140);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> new SmileyFaceApp());
   }
}

Output

Explanation

In this code, we add two additional elements to the smiley face. We draw an arc above the eyes to represent eyebrows and another filled oval below the eyes to represent the nose.

Approach 3 (Vibrant Smiley Face)

In this approach, we will make the smiley face more vibrant by using different colors for different elements.

Example

import javax.swing.*;
import java.awt.*;

public class SmileyFaceApp3 extends JFrame {

   public SmileyFaceApp3() {
      setTitle("Smiley Face");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(400, 400);
      setVisible(true);
   }

   public void paint(Graphics g) {
      // Set background color
      getContentPane().setBackground(Color.WHITE);

      // Draw face
      g.setColor(Color.YELLOW);
      g.fillOval(100, 100, 200, 200);

      // Draw eyebrows
      g.setColor(Color.BLACK);
      g.drawLine(145, 145, 175, 165);
      g.drawLine(235, 165, 265, 145);

      // Draw eyes
      g.setColor(Color.BLUE);
      g.fillOval(155, 175, 20, 20);
      g.fillOval(225, 175, 20, 20);

      // Draw nose
      g.setColor(Color.RED);
      g.fillOval(190, 225, 20, 20);

      // Draw smile
      g.setColor(Color.GREEN);
      g.drawArc(150, 175, 100, 100, 200, 140);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> new SmileyFaceApp3());
   }
}

Output

Explanation

In this code, we utilize various varieties for the face, eyes, grin, eyebrows, and nose. This adds a dynamic touch to the smiley face.

Approach 4 (Creative Smiley Face)

In this approach, we will add additional elements like hair and blush to make the smiley face even more interesting.

Example

import javax.swing.*;
import java.awt.*;

public class SmileyFaceApp4 extends JFrame {

   public SmileyFaceApp4() {
      setTitle("Smiley Face");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(400, 400);
      setVisible(true);
   }

   public void paint(Graphics g) {
      // Set background color
      getContentPane().setBackground(Color.WHITE);

      // Draw face
      g.setColor(Color.YELLOW);
      g.fillOval(100, 100, 200, 200);

      // Draw hair
      g.setColor(Color.BLACK);
      g.fillArc(140, 70, 120, 120, 0, 180);

      // Draw eyebrows
      g.setColor(Color.BLACK);
      g.drawLine(145, 165, 175, 145);
      g.drawLine(255, 145, 285, 165);

      // Draw eyes
      g.setColor(Color.BLACK);
      g.fillOval(155, 175, 20, 20);
      g.fillOval(225, 175, 20, 20);

      // Draw nose
      g.setColor(Color.BLACK);
      g.fillOval(190, 215, 20, 20);

      // Draw smile
      g.setColor(Color.BLACK);
      g.drawArc(150, 175, 100, 100, 200, 140);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> new SmileyFaceApp4());
   }
}

Output

Explanation

In this code, we introduce hair to the smiley face by drawing a filled arc above the head. We also add blush to the cheeks by drawing two small filled ovals on each side of the face.

Conclusion

Drawing a smiley face utilizing Java Applet is a tomfoolery and innovative activity. By following the bit by bit calculation and investigating various methodologies, you can alter the smiley face as indicated by your inclinations. Whether you pick an essential methodology or trial with energetic tones and extra components, the conceivable outcomes are inestimable. So feel free to release your imagination to draw your own extraordinary smiley face in Java Applet!

Updated on: 31-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements