Basic Graphics in Java

PrakashKumar982143 980 views 12 slides Nov 12, 2022
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

Basic Graphics using graphics object and graphics class in JAVA


Slide Content

Thank You! Logistics Edit this text here JAVA Seminar Basic Graphics and Animation Presented By : Prakash Kumar (MCA/25023/22)

Introduction One of the most important features of java is its ability to draw graphics. We can write Java applets that draw lines, figures of different shapes, images and text in different fonts and styles. We can also incorporate different colors in display. Every applet has its own area of the screen known as canvas, where it creates its display. Java Coordinate system has the origin (0,0) in the upper-left corner. Positive x values are to the right and positive y values are to the bottom. The values of the coordinates x and y are in pixels.

The Graphics Class Java’s Graphics class includes methods for drawing many different types of shapes, from simple lines to polygons to text in a variety of fonts. To draw a shape on the screen, we may call one of the methods available in the Graphics class. To draw a shape, we only need to use the appropriate method with the required arguments. Drawing 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 hallow arc drawLine() Draws a Straight Line

Method Description drawOval() Draws a hollow oval drawPolygon() Draws a hollow polygon drawRect() Draws a hollow rectangle drawRoundRect () Draws a hollow rounded corner rectangle drawstring() Display a text string fillArc () Draws a filled arc fillOval () Draws a filled oval fillPolygon () Draws a filled polygon fillRect () Draws a filled rectangle fillRoundRect () Draws a filled rounded corner rectangle getColor () Retrieves the current drawing color getFont () Retrieves the currently used font setColor () Sets the drawing color setFont () Sets the font (Contd.)

Lines Line is the simplest shape we can draw with the graphics class. The drawLine() method takes two pair of coordinates, (x1, y1) and (x2, y2) as arguments and draws a line between them. For example, the following statement draws a straight line from the coordinate point (20, 20) to (70, 70) : g.drawLine (20, 20, 70, 70); The g is the Graphics object passed to the method.

Rectangles We can draw a rectangle using the drawRect() method. This method takes four arguments. The first two represent the x and y coordinates of the top-left corner of the rectangle, and the remaining two represent the width and the height of the rectangle. For example, the following statement draws a rectangle starting at (10, 60) having a width of 40 pixels and a height of 30 pixels. The drawRect() method draws only the outline of a box. g.drawRect (10 , 60, 40 , 30); (x , y) width height Rectangle

Circles and Ellipses The graphics class does not have any method for circles or ellipses. The drawOval() method can be used to draw a circle or an ellipse. Ovals are just like rectangles with overly rounded corners. The drawOval() method takes four arguments. The first two represents the top-left corner of the imaginary rectangle and the other two represent the width and height of the oval itself. If the width and the height are same then the oval becomes a circle. Height width Oval within an imaginary rectangle

Drawing Arcs An arc is a part of an oval. The drawArc() designed to draw arcs takes six arguments. The first four are the same as the arguments for drawOval() method and the last two represent the starting angle of the arc and the number of degrees (sweep angle) around the arc. 90˚ 180˚ 0˚ 270˚ Arc as a part of an oval

Drawing Polygons Polygons are shapes with many sides. A polygon may be considered as a set of lines connected together. The end of the first line is the beginning of the second line, the end of the second line is the beginning of the third, and so on. This suggests that we can draw a polygon with n sides using the drawLine() method n times in succession. For Ex: To draw a Polygon with 3 sides (triangle) having vertices (10, 20), (170, 40) and (80, 140). public void paint (Graphics g) { g.drawLine(10, 20, 170, 40) g.drawLine(170, 40, 80, 140) g.drawLine(80, 140, 10, 20) }

(10, 20) (170, 40) (80, 140) A polygon with three sides We can draw polygon more conveniently using the drawPolygon() method of graphics class. This method takes 3 arguments: An array of integers containing x coordinates An array of integers containing y coordinates An integer for the total number of points The polygon above can also be drawn as: public void paint (Graphics g) { int xPoints [] = {10, 170, 80, 10}; int yPoints [] = {20, 40, 140, 20}; g.drawPolygon (xPoints, yPoints, nPoints); }

//Applet for drawing a human face import java.awt .*; import java.applet .*; public class Face extends Applet { public void paint (Graphics g) { g.drawOval (40, 40, 120, 150); g.drawOval (57, 75, 30, 20); g.drawOval (110, 75, 30,20); g.filloval (68, 81, 10, 10); g.filloval (121, 81, 10, 10) ; g.drawOval (85, 100, 30, 30); g.fillAro (60, 125, 80, 40, 180, 180); g.drawOval (25, 92, 15, 30); g.drawOval (160, 92, 15, 30); } } Output

Thank You Reference :- Programming with JAVA Sixth Edition, E Balagurusamy