Documente Academic
Documente Profesional
Documente Cultură
Presentation 3
Objectives
At the end of this presentation, you will be able to : Create an applet that contains images and geometrical shapes
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.
drawPolygon( )
drawRect( )
drawstring( ) fillArc( )
fillOval( )
fillPolygon( ) fillRect( ) fillRoundedRect( )
Description Draws a filled oval. Draws a filled polygon. Draws a filled rectangle. Draws a filled rectangle with rounded corners.
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 Arcs
The drawArc() method is used to draw arcs in an applet window.
Example :
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);
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.
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: