Thank You!
L ogistics
E d i t t h i s t e x t h e r e
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.)
Line
s
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

Basic Graphics in Java

  • 1.
    Thank You! L ogistics Ed i t t h i s t e x t h e r e JAVA Seminar Basic Graphics and Animation Presented By : Prakash Kumar (MCA/25023/22)
  • 2.
    Introduction One of themost 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.
  • 3.
    The Graphics Class Java’sGraphics 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
  • 4.
    Method Description drawOval() Drawsa 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.)
  • 5.
    Line s Line is thesimplest 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.
  • 6.
    Rectangles We can drawa 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
  • 7.
    Circles and Ellipses Thegraphics 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
  • 8.
    Drawing Arcs An arcis 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
  • 9.
    Drawing Polygons Polygons areshapes 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.
    (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); }
  • 11.
    //Applet for drawinga 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
  • 12.
    Thank You Reference :-Programming with JAVA Sixth Edition, E Balagurusamy

Editor's Notes

  • #2 Links: https://www.pexels.com/photo/black-sail-ship-on-body-of-water-906982/
  • #3 https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  • #4 https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  • #5 https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  • #6 https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  • #7 https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  • #8 https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  • #9 https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  • #10 https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  • #11 https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  • #12 https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/