The Graphics class in Java provides methods for drawing shapes and text. It is an abstract class in the java.awt package. Some key methods include paint() for initial drawing, repaint() to erase, and drawLine(), drawRect(), drawOval(), drawRoundRect(), drawString(), drawPolygon() and drawArc() to render the different shapes. Colors can be set using setColor() and pre-defined color constants. The Graphics class serves as the base for drawing graphics in Java applications.
The Graphics class in Java provides methods for drawing shapes and text. It is an abstract class in the java.awt package. Some key methods include paint() for initial drawing, repaint() to erase, and drawLine(), drawRect(), drawOval(), drawRoundRect(), drawString(), drawPolygon() and drawArc() to render the different shapes. Colors can be set using setColor() and pre-defined color constants. The Graphics class serves as the base for drawing graphics in Java applications.
• Graphical class is an abstract class present inside the java.awt package. • The signature of this class: public abstract class java.awt.Graphics extends java.lang.Object
• Graphics class provides the declaration of various method
which are use for the purpose of drawing line, rectangles and several other shapes for drawing different figures.
Meetendra Singh Chahar, RBSMTC, Agra 2
• Public void paint(Graphics g) • This method is used at the time of creation of graphics • Public void repaint() • This method helps to erase the graphics. • Public void update(Graphics g) • Update the Graphics by this method • Public void drawLine(int c1 int d1, int c2, int d2) • Draw the line from starting co-ordinate(c1,d1) ending co-ordinate(c2,d2)
Meetendra Singh Chahar, RBSMTC, Agra 3
• Public void drawRect(int c1 int d1, int c2, int d2) • Draw the rectangle (outline only) from starting co- ordinate(c1,d1(location)) ending co-ordinate(c2,d2(height))
• Public void fillRect(int c1 int d1, int c2, int d2)
• Draw the rectangle (outline only) from starting co- ordinate(c1,d1(location)) ending co-ordinate(c2,d2(height))
• Public void drawOval(int c1 int d1, int c2, int d2)
• Draw the Oval(outline only) • Public void fillOval(int c1 int d1, int c2, int d2)
Meetendra Singh Chahar, RBSMTC, Agra 4
• Public void drawRoundRect(int c1 int d1, int c2, int d2, int c3, int d3) • Draw the outline of a rectangle (corners are in a rounded format) • Public void fillRoundRect(int c1 int d1, int c2, int d2, int c3, int d3) • Public void drawString(String Message, int c1, int d1) • Public void drawPolygon(int c1[], int c2[], int pnt) • Draw a polygon. It take two arrays and a point of integer type as the argument.
Meetendra Singh Chahar, RBSMTC, Agra 5
• Public void fillPolygon(int c1[], int c2[], int pnt) • Draw a polygon. It take two arrays and a point of integer type as the argument. • Public void drawArc(int c1, int d1,int c2, int d2,int c3, int d3) • Draw a Arc. • Public void fillArc(int c1, int d1,int c2, int d2,int c3, int d3) • Draw a fillArc. • Public void setColor(java.awt.Color RED) • Predefine colour constants of the Color class.