You are on page 1of 23

Programming Language (JAVA)

Unit 6.11 Applets

Presentation 3

Objectives
At the end of this presentation, you will be able to : Create an applet that contains images and geometrical shapes

The Graphics Class


The most important feature of Java is its support for graphics.
You can write applets to draw lines, figures of different shapes, images and text in different fonts and styles. The size of the applet window is defined by the attributes of the <APPLET> tag.

The Graphics Class

Methods of the graphics class


Method

clearRect( )
copyArea( ) drawArc( ) drawLine( ) drawOval()

Description Erases a rectangular area of the canvas. Copies a rectangular area of the canvas to another area. Draws a hollow arc. Draws a straight line. Draws a hollow oval.

Methods of the graphics class


Method

drawPolygon( )
drawRect( )

Description Draws a hollow polygon. Draws a hollow rectangle.

drawroundRect( ) Draws a hollow rectangle with

drawstring( ) fillArc( )

rounded corners. Displays a text string.

Draws a filled arc.

Methods of the graphics class


Method

fillOval( )
fillPolygon( ) fillRect( ) fillRoundedRect( )

Description Draws a filled oval. Draws a filled polygon. Draws a filled rectangle. Draws a filled rectangle with rounded corners.

Methods of the graphics class


Method

getColor( )
getFont( ) setColor( ) setFont( )

Description Retrieves the current drawing colour. Retrieves the currently used font. Sets the drawing colour. Sets the font.

Drawing a Line
While drawing a line in an applet window using drawLine( ) method, you need to use two pairs of coordinates.
Example: g.drawLine(20,20, 60,60);

Drawing a Circle and an Ellipse


The drawOval() method is used to draw a circle or an ellipse.
Example:

g.drawOval(10,20,50,30); fillOval() is similar to drawOval() but fills the


oval with a colour.

Drawing Arcs
The drawArc() method is used to draw arcs in an applet window.
Example :

g.drawArc(10,20,50,30,0,90); fillArc() method is similar to drawArc()


method but fills the arc with a colour.

Drawing Polygons
The drawPolygon() method is used to draw polygons in an applet window.
Example:

g.drawPolygon(xcoords,ycoords,5); fillPolygon() method is similar to drawPolygon() method but fills the arc with a
colour.

Drawing a Rectangle
drawRect( ) method is used to draw a
rectangle. This method takes four arguments.

Example:

g.drawRect(20,70, 30,40);

Sample Rectangle output

Draw Rectangle
The fillRect() method is similar to drawRect() method but the fillRect() method fills the rectangle with a colour.
The drawRoundRect() method is similar to drawRect() method but drawRoundRect() method draws a rectangle with rounded corners.

The fillRoundRect() method is similar to drawRoundRect() method but fillRoundRect() method fills the rectangle with a colour.

setColor()
The setColor() method is used to set the current foreground colour.
The following code creates an object of Color class and sets the foreground colour to red.

Color col=new Color(255,0,0); g.setColor(col);


The foreground colour can also be set using

g.setColor(Color.Red);

getColor()
The getColor() method is used to get the current foreground colour of the applet.
The following is the code to get the current foreground colour.

g.getColor();

Hands-On!
Program LineRect.java draws a line and rectangle in an applet window.

Hands-On!
Program Oval.java draws a circle and an oval filled with green color in the applet window.

Hands-On!
Program Polygon.java draws a polygon in the applet window.

Hands-On!
Program Face.java draws a face in an applet window.

Summary
In this presentation, you learnt the following To draw graphics through applets the Graphics class is used. Applets can be used to draw lines, figures of different shapes, images and text in different fonts and styles.

Assignment
1. 2. Explain the Graphic class and its methods. Write an applet program to display the picture given below:

You might also like