Java Program to Draw a Smiling Face using Graphics Class Methods

This is a Java program to draw a smiling face using graphics class.

Problem Description

We have to write a program in Java such that it creates a frame containing a smiling face using Graphics class.

The Graphics class defines a number of drawing methods like drawLine, drawOval, drawRect, drawArc, fillArc, fillOval, fillRect, and many others.

The default color for Graphics class methods is black. To change the color method setColor is used.

Expected Input and Output

For drawing a Smiling Face using Graphics class, we can have the following set of input and output.

To draw a Smiling Face:
We expect a Smiling Face with background color of the face as yellow. The eyes are to be properly positioned and filled with color black. The smile is to be drawn with black color with proper positioning.

advertisement
advertisement
Problem Solution

1. Create a class that inherits the JPanel class.
2. To draw the smiling face, define the function : public void paint(Graphics). This function is invoked to draw the shapes as defined in the function body.
3. Create three circles using drawArc for the face and two eyes and fill them with color of your choice.
4. Create an arc using drawArc for the smile with the color of your choice.

Program/Source Code

Here is source code of the Java Program to draw a simling face using Graphics class. The program is successfully compiled and tested using BlueJ on Windows 10 and javac compiler on Fedora 30.

  1. import javax.swing.*;
  2. import java.awt.*;
  3. class Smiley extends JPanel
  4. {
  5.     //Driver function
  6.     public static void main(String args[])
  7.     {
  8.         //Create a frame
  9.         JFrame frame=new JFrame("Smiley");
  10.         frame.setSize(500,500);
  11.         frame.setBackground(Color.white); 
  12.         Smiley panel=new Smiley();
  13.         frame.add(panel);
  14.         //Set default close operation for the frame
  15.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.         frame.setVisible(true);
  17.     }
  18.     //function to draw the shapes
  19.     public void paint(Graphics g)
  20.     {
  21.         //Change color to yellow
  22.         g.setColor(Color.yellow);
  23.         //Draw and fill the face
  24.         g.drawArc(100,100,250,250,0,360);
  25.         g.fillArc(100,100,250,250,0,360);
  26.         //Change color to black
  27.         g.setColor(Color.black);
  28.         //Draw the left eye
  29.         g.drawArc(170,185,25,25,0,360);
  30.         g.fillArc(170,185,25,25,0,360);
  31.         //Draw the right eye
  32.         g.drawArc(255,185,25,25,0,360);
  33.         g.fillArc(255,185,25,25,0,360);
  34.         //Draw the smile
  35.         g.drawArc(150,215,150,100,0,-180);
  36.     }
  37. }
Program Explanation

1. Create a class Smiley that inherits the JPanel class.
2. Create a frame with background color & specific size. frame.setBackground(Color.white); is used to create the frame background color as white. frame.setSize(500,500); is used to set the width and height of the frame.
3. Create an panel of the class Smiley panel=new Smiley(); and add it to the frame by using frame.add(panel);
4. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); is a default close operation for the frame.
5. frame.setVisible(true); is used to display the frame.
6. public void paint(Graphics g) this function is used to draw the smiling face. This function is invoked to draw the shapes as defined in the function body.
7. The drawArc, fillArc and setColor functions are defined in the java.awt.Graphics package.
a) drawArc(int x,int y,int wid,int len,int start,int end): This method draws an arc of length ‘len’ and width ‘wid’ starting from ‘x’ & ‘y’ co-ordinate from the angle ‘start’ to angle ‘end’.
g.drawArc(100,100,250,250,0,360); – This method is used to draw the circle face.
g.drawArc(170,185,25,25,0,360); – This method is used to draw the left eye.
g.drawArc(255,185,25,25,0,360); – This method is used to draw the right eye.
g.drawArc(150,215,150,100,0,-180); – This method is used to draw the smile.

Note: Join free Sanfoundry classes at Telegram or Youtube

b) fillArc(int x,int y,int wid,int len,int start,int end): This method fills the arc of length ‘len’ and width ‘wid’ starting from ‘x’ & ‘y’ co-ordinate from the angle ‘start’ to angle ‘end’.
g.fillArc(100,100,250,250,0,360); – This method is used to fill the face circle with black color.
g.fillArc(170,185,25,25,0,360); – This method is used to fill the left eye circle with black color.
g.fillArc(255,185,25,25,0,360); – This method is used to fill the left eye circle with black color.

c) setColor(Color.color name): This method changes the color of the drawing for next called methods.
Colors used in this program are Yellow and Black by using setColor(Color.yellow); and setColor(Color.black); methods.

advertisement
Runtime Test Cases

Here’s the run time test case to draw a Smiling Face using methods of Graphics class.

Test case 1 – Here’s the runtime output of the Smiling Face. Smiling Face should display a face with background color as yellow. The eyes are to be properly positioned and filled with color black. The smile is to be drawn with black color with proper positioning.
java-program-draw-smiling-face-using-graphics-class

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.