interactive java note - m06_un11_p03
TRANSCRIPT
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 Description
clearRect( ) Erases a rectangular area of the canvas.
copyArea( ) Copies a rectangular area of the canvas to another area.
drawArc( ) Draws a hollow arc.
drawLine( ) Draws a straight line.
drawOval() Draws a hollow oval.
Methods of the graphics class
Method Description
drawPolygon( ) Draws a hollow polygon.
drawRect( ) Draws a hollow rectangle.
drawroundRect( ) Draws a hollow rectangle with rounded corners.
drawstring( ) Displays a text string.
fillArc( ) Draws a filled arc.
Methods of the graphics class
Method Description
fillOval( ) Draws a filled oval.
fillPolygon( ) Draws a filled polygon.
fillRect( ) Draws a filled rectangle.
fillRoundedRect( ) Draws a filled rectangle with rounded corners.
Methods of the graphics class
Method Description
getColor( ) Retrieves the current drawing colour.
getFont( ) Retrieves the currently used font.
setColor( ) Sets the drawing colour.
setFont( ) 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. Explain the Graphic class and its methods.
2. Write an applet program to display the picture given below: