01
GRAPHICS
CLASS IN JAVA
KARAN MANTRI
24261A0595
CSE-2
The Graphics class in Java is an abstract class from the
[Link] package that provides the foundation for drawing
shapes, text, and images in GUI applications.
Introduction
The Graphics class is a core component of Java's AWT,
which provides tools for building graphical user
to Graphic
interfaces.
It helps to build shapes, lines and other 2d shapes.
Class
Acts as a bridge between Java code and the screen,
enabling 2D rendering.
02
WORKING You don’t instantiate Graphics directly.
A Graphics object is passed to the paint() or update() method of a
component.
It is passed as an argument in the paint or update method.
We can also use the getGraphics() method.
These methods are overridden to customize drawing behavior.
03
Usually the paint() method is preferred as the graphics obtained
via the object of the getGraphics() method are temporary and are
lost when the component is repainted again.
04
METHOD DESCRIPTION
Draws a line between two
drawLine(x1, y1, x2, y2)
points.
drawRect(x, y, width, height) Draws a Rectangle.
fillRect(x, y, width, height) Fills a Rectangle.
drawOval(x, y, width, height) Draws oval inside Rectangle.
KEY drawString(str, x, y) Draws Text.
METHODS
setColor(Color c) Sets the drawing color.
setFont(Font f) Sets the font for text.
05
drawLine(x1, y1, x2,y2)
06
drawRect(x1, y1, width, height)
07
fillRect(x1, y1, width, height)
08
drawOval(x, y, width, height)
fillOval(x, y, width, height)
09
drawArc(x, y, width, height,
startAngle, arcAngle
10
int x[]
int y[]
drawPolygon(x, y, sides)