Posted By:
21 Feb, 2014 8:33 am PST

How to draw Smiley in Java ?

 Write a program to draw a smiling face using the methods of Graphics class.

 /Drawing a smiling face in a frame

import java.awt.*;

import java.awt.event.*;

public class CloseFrame1 extends Frame{

       public void paint(Graphics g){

             //set blue color for drawing

              g.setColor(Color.blue);

              //display rectangle to display drawing

              g.drawRect(40, 40, 200, 200);

            //face

              g.drawOval(90, 70, 80, 80);

             //eyes

              g.drawOval(110, 95, 5, 5);

              g.drawOval(145, 95, 5, 5);

             //nose

              g.drawLine(130,95,130,115);

             //mouth

              g.drawArc(113,115,35,20,0,-180);

                     }

       public static void main(String []args){

             //create the frame

              CloseFrame1 d = new CloseFrame1();

//set the size annd title

              d.setSize(400,400);

              d.setTitle("My Drawing");

              //display the frame

              d.setVisible(true);

              d.addWindowListener(new WindowAdapter(){

                     public void windowClosing(WindowEvent e){

                           System.exit(0);

                     }

              });

       }

}

OUTPUT:

Write a program that allows you to fill the shapes with some color.

//Drawing a smiling face in a frame with filled colors

import java.awt.*;

import java.awt.event.*;

public class FilledColor extends Frame{

       public static void main(String args[])

       {

              FilledColor f = new FilledColor();

              f.setTitle("My First Drawing");

              f.setSize(400,400);

              f.setVisible(true);;

              f.addWindowListener(new WindowAdapter()

              {

                     public void windowClosing(WindowEvent e)

                     {

                           System.exit(0);}

                     });

              }

       public void paint(Graphics p)

       {

              //set green color

              p.setColor(Color.green);

//display a rectangle to contain drawing

              p.fillRect(40,40,200,200);

              //set red color

              p.setColor(Color.red);

              //face

              p.fillOval(90, 70, 80, 80);

              //set black color

              p.setColor(Color.black);

              //eyes

              p.fillOval(110, 95, 5, 5);

              p.fillOval(145, 95, 5, 5);

              //nose

              p.drawLine(130, 95, 130, 115);

              //set yellow color

              p.setColor(Color.yellow);

              //mouth

              p.fillArc(113, 115, 35, 20, 0, -180);

       }

}

OUTPUT:

Attachment: workspace.zip


Description:

Java.awt : Contains all of the classes for creating user interfaces and for painting graphics and images.

Frame class : A Frame is a top-level window with a title and a border.

Syntax: 
public class Frame

extends Window

implements MenuContainer

 
 

WindowListener Interface: The listener interface for receiving window events. The class that is interested in processing a window event either implements this interface (and all the methods it contains) or extends the abstract WindowAdapter class (overriding only the methods of interest). The listener object created from that class is then registered with a Window using the window's addWindowListener method. When the window's status changes by virtue of being opened, closed, activated or deactivated, iconified or deiconified, the relevant method in the listener object is invoked, and the WindowEvent is passed to it.

 

Attachment: Smiley.zip
Was it useful?  




Please login to reply to this problem.