Skip to main content
JAVA
GRAPHICS PROGRAMMING
B.ABINAYA BHARATHI M.Sc[Cs&IT],
NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE,
THENI.
1
INTRODUCTION
 Main feature in java is creating a graphical interface .
 Graphics in any language gives a wonderful look and feel to
the users .
 Two packages that are mainly used to draw graphics.
 Applet package
 awt package
2
APPLET
 An applet is a Java program that runs in a Web browser
 An applet is a Java class that extends the java.applet.Applet
class
 A main() method is not invoked on an applet
 Applets are designed to be embedded within an HTML page.
3
EXAMPLE APPLET PROGRAM
Importing applet package
` This html code tells the compiled java
applet of a sample.class
Paint method is used to paint
a applet
drawstring( ) - graphics class
displays the string within the
double quotes 4
OUTPUT
5
AWT PACKAGE
 The Abstract Window Toolkit (AWT)
 It is Java's original platform-independent windowing,
graphics, and user-interface toolkit.
 The AWT classes are contained in the java.awt package.
 It is used to create a interactive page with buttons , text box
and other tools .
6
GRAPHICS CLASS
 Graphics class include methods for drawing shapes , images
to the screen inside your applet
 Graphics class contain several inbuilt methods to create
graphical interface.
7
DRAWING STRING IN APPLET
drawString()
 drawString() is used to display string in Graphical area.
SYNTAX
drawString(String str, int x, int y)
 String to be displayed
 x and y position on the graphical window.
8
DRAWING LINES
drawLine()
 This method is used to draw a line.
SYNTAX
drawLine(int x1, int y1, int x2, int y2)
 This method contains two pair of coordinates, (x1, y1) and
(x2, y2) as arguments
 draws a line between them.
9
drawPolyline()
 It connects the xPoints and yPoints arrays
 It does not connect the endpoints.
SYNTAX
drawPolyline(int[] xPoints,int[] yPoints,int nPoints)
10
SAMPLE CODE
11
OUTPUT
12
DRAWING SHAPE PRIMITIVES
drawRect()
 Used to draw rectangle shape in an applet.
SYNTAX
drawRect(int xTopLeft, int yTopLeft, int width, int height);
 First two points represents x and y coordinates of the top
left corner
 Next two represent the width and the height of the
rectangle. 13
drawOval()
 Used to draw circle and oval in an applet.
SYNTAX
drawOval(int xTopLeft, int yTopLeft, int width, int height);
 First two arguments represents x and y coordinates of the
top left .
 Third and fourth argument represent the width and
the height of the rectangle .
14
drawArc()
 Arc is same as oval
 first four are same as arguments of drawOval( )
 Next arguments represents starting angle and degrees
around the arc.
SYNTAX
drawArc(int xTopLeft, int yTopLeft, int width, int height, int
startAngle, int arcAngle);
15
drawRoundRect()
 By using this method we can draw rounded rectangle
SYNTAX
drawRoundRect(int xTopLeft, int yTopLeft, int width, int
height, int arcWidth, int arcHeight)
 Rounded rectangle contains same argument as drawRect().
 In rounded rectangle two extra arguments representing the
width and height of the angle of corners.
16
drawPolygon()
 This method Draws an outline polygon as per the
coordinates specified in the x[] and y[] arrays
 Numpoints - number of elements in the array
SYNTAX
drawPolygon(int[] xPoints, int[] yPoints, int numPoint);
17
SAMPLE CODE
18
OUTPUT
19
FILLING PRIMITIVE SHAPES
fillOval()
 The fillOval() method draws a filled oval .
 We can’t specify the oval's center point and radii.
 The filled oval is one pixel smaller to the right and bottom
than requested.
SYNTAX
fillOval(int xTopLeft, int yTopLeft, int width, int height);
20
fillArc()
 The fillArc() method is similar to the drawArc() method
except that it draws a filled arc .
 If width and height are equal and arcAngle is 360 degrees
 fillArc() draws a filled circle.
SYNTAX
fillArc(int xTopLeft, int yTopLeft, int width, int height, int
startAngle, int arcAngle);
21
fillRect()
 fillRect() method draws a filled .
 The filled rectangle is one pixel smaller to the right and
bottom than requested.
 If width or height is negative, nothing is drawn.
SYNTAX
fillRect(int xTopLeft, int yTopLeft, int width, int height);
22
fillPolygon()
 The fillPolygon() method draws a polygon.
 If xPoints or yPoints does not have numPoints elements, it
throws the run-time exception
andIllegalArgumentException.
 If the polygon is not closed, fillPolygon() adds a segment
connecting the endpoints.
SYNTAX
fillPolygon(int[] xPoints, int[] yPoints, int numPoint);

23
fillRoundRect()
 The fillRoundRect() method is similar to
drawRoundRect() method except that it draws a filled
rectangle on the drawing area
 If width, height, arcWidth, and arcHeight are all equal, you
get a filled circle.
SYNTAX
fillRoundRect(int xTopLeft, int yTopLeft, int width, int
height, int arcWidth, int arcHeight);
24
SAMPLE CODE
25
OUTPUT
26
setColor( )
 AWT color class used to specify any color we need.
 color is specified as Color.Blue
 By default, graphics objects are drawn in the current
foreground color.
 We can change this color by calling the setColor( ).
EX:
g.setColor(Color . yellow);
27
SAMPLE CODE
28
OUTPUT
29
THANK YOU
30