Posted By:
20 Feb, 2014 1:07 pm PST

How to draw pictures in Java ?

Write a program to draw a home with moon in the background.

import java.awt.*;

import java.awt.event.*;

public class Moon extends Frame{

       public static void main(String[] args) {

              Moon m=new Moon();



              m.setTitle("My Home");

m.addWindowListener(new WindowAdapter()


       public void windowClosing(WindowEvent e)






public void paint(Graphics s)


       int x[]={375,275,475};

       int y[]={125,200,200};

       int n=3;

//set gray background for frame


//set yellow color for rectangle -house


s.fillRect(300, 200, 150, 100);

//set blue color for another rectangle-door


s.fillRect(350, 210,50, 60);

//draw a line below the door

s.drawLine(350, 280, 400, 280);

//set dark gray for polygon - roog



//set cyan color for oval- moon


s.fillOval(100, 100, 60, 60);

//set green for arcs- grass


s.fillArc(50, 250, 150, 100, 0, 180);

s.fillArc(150, 250, 150, 100, 0, 180);

s.fillArc(450, 250, 150, 100, 0, 180);

//draw a line - the bottom most line of drawing

s.drawLine(50, 300, 600, 300);

//display some text


s.drawString("My Happy Home", 275, 350);






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.

Paint function() :  This method will be invoked when it is time to paint. This method will be executed by the painting subsystem whenever you component needs to be rendered. Its signature is:

  • public void paint(Graphics g)

Functions used in paint()



public abstract void drawLine(int x1,

int y1,

int x2,

int y2)

 Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.


x1 - the first point's x coordinate.

y1 - the first point's y coordinate.

x2 - the second point's x coordinate.

y2 - the second point's y coordinate. 

2. fillRect

public abstract void fillRect(int x,

int y,

int width,

int height)

Fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context's current color.


x - the x coordinate of the rectangle to be filled.

y - the y coordinate of the rectangle to be filled.

width - the width of the rectangle to be filled.

height - the height of the rectangle to be filled.


3. fillOval

public abstract void fillOval(int x,

int y,

int width,

int height)

Fills an oval bounded by the specified rectangle with the current color.


x - the x coordinate of the upper left corner of the oval to be filled.

y - the y coordinate of the upper left corner of the oval to be filled.

width - the width of the oval to be filled.

height - the height of the oval to be filled.


4.  fillArc

public abstract void fillArc(int x,

int y,

int width,

int height,

int startAngle,

int arcAngle)

Fills a circular or elliptical arc covering the specified rectangle.

The resulting arc begins at startAngle and extends for arcAngle degrees. Angles are interpreted such that 0 degrees is at the 3 o'clock position. A positive value indicates a counter-clockwise rotation while a negative value indicates a clockwise rotation.

The center of the arc is the center of the rectangle whose origin is (xy) and whose size is specified by the width and height arguments.

The resulting arc covers an area width + 1 pixels wide by height + 1 pixels tall.

The angles are specified relative to the non-square extents of the bounding rectangle such that 45 degrees always falls on the line from the center of the ellipse to the upper right corner of the bounding rectangle. As a result, if the bounding rectangle is noticeably longer in one axis than the other, the angles to the start and end of the arc segment will be skewed farther along the longer axis of the bounds.


x - the x coordinate of the upper-left corner of the arc to be filled.

y - the y coordinate of the upper-left corner of the arc to be filled.

width - the width of the arc to be filled.

height - the height of the arc to be filled.

startAngle - the beginning angle.

arcAngle - the angular extent of the arc, relative to the start angle.


5. fillPolygon


public abstract void fillPolygon(int[] xPoints,

int[] yPoints,

int nPoints)

Fills a closed polygon defined by arrays of x and y coordinates.

This method draws the polygon defined by nPoint line segments, where the first nPoint - 1 line segments are line segments from (xPoints[i - 1], yPoints[i - 1]) to (xPoints[i], yPoints[i]), for 1 ≤ i ≤ nPoints. The figure is automatically closed by drawing a line connecting the final point to the first point, if those points are different.

The area inside the polygon is defined using an even-odd fill rule, also known as the alternating rule.


xPoints - a an array of x coordinates.

yPoints - a an array of y coordinates.

nPoints - a the total number of points.


Was it useful?  

Please login to reply to this problem.

Related Problems on Fixoncloud