Interactive Java Note - M06_UN11_P03

23
Programming Language (JAVA) Unit 6.11 – Applets Presentation 3

Transcript of Interactive Java Note - M06_UN11_P03

Page 1: Interactive Java Note - M06_UN11_P03

Programming Language (JAVA)

Unit 6.11 – Applets

Presentation 3

Page 2: Interactive Java Note - M06_UN11_P03

Objectives

At the end of this presentation, you will be able to :

• Create an applet that contains images and geometrical shapes

Page 3: Interactive Java Note - M06_UN11_P03

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.

Page 4: Interactive Java Note - M06_UN11_P03

The Graphics Class

Page 5: Interactive Java Note - M06_UN11_P03

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.

Page 6: Interactive Java Note - M06_UN11_P03

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.

Page 7: Interactive Java Note - M06_UN11_P03

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.

Page 8: Interactive Java Note - M06_UN11_P03

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.

Page 9: Interactive Java Note - M06_UN11_P03

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);

Page 10: Interactive Java Note - M06_UN11_P03

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.

Page 11: Interactive Java Note - M06_UN11_P03

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.

Page 12: Interactive Java Note - M06_UN11_P03

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.

Page 13: Interactive Java Note - M06_UN11_P03

Drawing a Rectangle

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

Example:

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

Page 14: Interactive Java Note - M06_UN11_P03

Sample Rectangle output

Page 15: Interactive Java Note - M06_UN11_P03

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.

Page 16: Interactive Java Note - M06_UN11_P03

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);

Page 17: Interactive Java Note - M06_UN11_P03

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();

Page 18: Interactive Java Note - M06_UN11_P03

Hands-On!

• Program LineRect.java draws a line and rectangle in an applet window.

Page 19: Interactive Java Note - M06_UN11_P03

Hands-On!

• Program Oval.java draws a circle and an oval filled with green color in the applet window.

Page 20: Interactive Java Note - M06_UN11_P03

Hands-On!

• Program Polygon.java draws a polygon in the applet window.

Page 21: Interactive Java Note - M06_UN11_P03

Hands-On!

• Program Face.java draws a face in an applet window.

Page 22: Interactive Java Note - M06_UN11_P03

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.

Page 23: Interactive Java Note - M06_UN11_P03

Assignment

1. Explain the Graphic class and its methods.

2. Write an applet program to display the picture given below: