Java Program to Display Human Face using Applet

«
»

This is a Java Program to Display Human Face using Applet

Problem Description

We have to write a program in Java such that it displays a human face using applet.

Expected Input and Output

For displaying human face using applet, we can have the following set of input and output.

advertisement

When the Applet is Executed :

When the applet is executed,
it is expected that a human face is drawn.
Problem Solution

1. Initially, set the background color of frame as white.
2. Create a color which is similar to the human skin color, and then draw and fill the outer boundary of the face.
3. Then set the color to black.
4. Draw and fill the left eye and the right eye.
5. Draw the right eyebrow and the left eyebrow.
6. Draw the nose.
7. Draw the smile.

Program/Source Code

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

advertisement
  1. /*Java Program to Draw a Human Face using Applet*/
  2. import java.applet.*;
  3. import java.awt.*;
  4. public class Human_Face extends Applet
  5. { 
  6.     //Initialize the applet
  7.     public void init()
  8.     {
  9. 	setBackground(Color.white);
  10.     }
  11.     //Draw the human face
  12.     public void paint(Graphics g) 
  13.     { 
  14.         //Change color to cream
  15.         Color clr=new Color(255,179,86);
  16.         g.setColor(clr);
  17.         //Draw and fill the face
  18.         g.drawOval(100,100,250,300);
  19.         g.fillOval(100,100,250,300);
  20.         //Change color to black
  21.         g.setColor(Color.black);
  22.         //Draw the left eye
  23.         g.drawOval(160,185,40,25);
  24.         g.fillOval(160,185,40,25);
  25.         //Draw the right eye
  26.         g.drawOval(250,185,40,25);
  27.         g.fillOval(250,185,40,25);
  28.         //Draw the Left Eyebrow
  29.         g.drawArc(160,170,35,10,0,180);
  30.         //Draw the Right Eyebrow
  31.         g.drawArc(250,170,35,10,0,180);
  32.         //Draw the Nose
  33.         g.drawLine(210,265,210,275);
  34.         g.drawLine(240,265,240,275);
  35.         g.drawArc(210,275,30,10,0,-180); 
  36.        //Draw the smile
  37.         g.drawArc(175,300,100,50,0,-180);
  38.     } 
  39. } 
  40. /*
  41. <applet code = Human_Face.class width=500 height=500>
  42. </applet>
  43. */

To execute the applet use the following commands:

>>>javac Human_Face.java
>>>appletviewer Human_Face.java
Program Explanation

1. The RGB value (255,179,86) gives a kind of human skin color. Create a color using this value.
2. To draw and fill the face and eyes use drawOval and fillOval respectively.
3. To draw the eyebrows and smile use drawArc.
4. To draw the nose use drawArc and drawLine.

advertisement
Runtime Test Cases

Here’s the run time test cases for drawing human face using an applet.

Test case 1 – To view the Human Face in an Applet.
java-applet-human-face

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn