Graphics is an abstract class provided by Java AWT which is used to draw or paint on the components. It consists of various fields which hold information like components to be painted, font, color, XOR mode, etc., and methods that allow drawing various shapes on the GUI components. Graphics is an abstract class and thus cannot be initialized directly. Objects of its child classes can be obtained in the following two ways.
1. Inside paint() or update() method
It is passed as an argument to paint and update methods and therefore can be accessed inside these methods. paint() and update() methods are present in the Component class and thus can be overridden for the component to be painted.
void paint(Graphics g)
void update(Graphics g)
Example:
MyFrame.java
Java
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { public MyFrame() { setVisible( true ); setSize( 300 , 200 ); addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); } public void paint(Graphics g) { g.drawRect( 100 , 100 , 100 , 50 ); } public static void main(String[] args) { new MyFrame(); } } |
Output:
2. getGraphics() method
This method is present in the Component class and thus can be called on any Component in order to get the Graphics object for the component.
public Graphics getGraphics(): Creates a graphics context for the component. This method will return null if the component is currently not displayable.
Note: Painting must be preferably done using the paint() method. Graphics drawn via object obtained from getGraphics() method are temporary and are lost when the component is repainted again. Drawing might happen right away but repainting may take some time and therefore remove the effects of code done through getGraphics().
Example:
MyFrame.java
Java
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { public MyFrame() { setVisible( true ); setSize( 300 , 200 ); addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); } public void paint(Graphics g) { System.out.println( "painting..." ); } public static void main(String[] args) { MyFrame f = new MyFrame(); Graphics g = f.getGraphics(); System.out.println( "drawing..." ); g.drawRect( 100 , 100 , 10 , 10 ); System.out.println( "drawn..." ); } } |
Output:
This code will not render the graphics drawn via getGraphics() because paint() is called after the draw().
Java
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { public MyFrame() { setVisible( true ); setSize( 300 , 200 ); addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); } public void paint(Graphics g) { System.out.println( "painting..." ); } public static void main(String[] args) { MyFrame f = new MyFrame(); Graphics g = f.getGraphics(); try { Thread.sleep( 1000 ); } catch (Exception e) { } System.out.println( "drawing..." ); g.drawRect( 100 , 100 , 100 , 50 ); System.out.println( "drawn..." ); } } |
Output:
The above code will display the rectangle as paint() is called before draw(). Important methods for setting the properties of Graphics context:
- void setClip(int x, int y, int width, int height)
- void setClip(Shape clip)
- void setColor(Color c)
- void setFont(Font font)
- void setPaintMode()
- void setXORMode(Color c1)
Paint mode vs Xor mode
In paint mode, the new output drawn overwrites the previous one. Therefore if the color set in the graphics object is the same as that of the background, the object is not visible. In Xor mode color of the new output is obtained by XORing provided color with background and graphics color. Therefore object is always visible irrespective of background color.
Example:
Java
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { public MyFrame() { setVisible( true ); setSize( 300 , 200 ); setBackground(Color.red); addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); } public void paint(Graphics g) { g.setColor(Color.green); g.setXORMode(Color.black); g.fillRect( 100 , 100 , 100 , 50 ); } public static void main(String[] args) { new MyFrame(); } } |
Output:
In the above code,
color set for graphics is: green (00000000 11111111 00000000) background-color is: red (11111111 00000000 00000000) color provided for XOR: black (00000000 00000000 00000000) color obtained for rendering the output = green XOR red XOR black = yellow (11111111 11111111 00000000)
Drawing shapes using Graphics Object
Line
The following method of Graphics class is used to draw the line:
void drawLine(int startX, int startY, int endX, int endY)
Example:
Java
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { public MyFrame() { setVisible( true ); setSize( 300 , 300 ); addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); } public void paint(Graphics g) { g.drawLine( 100 , 100 , 200 , 200 ); } public static void main(String[] args) { new MyFrame(); } } |
Output:
Rectangle
The following methods of Graphics class are used to draw rectangles:
- void drawRect(int x, int y, int width, int height)
- void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
- void fillRect(int x, int y, int width, int height)
- void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Example:
Java
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { public MyFrame() { setVisible( true ); setSize( 300 , 300 ); addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); } public void paint(Graphics g) { g.drawRect( 50 , 60 , 50 , 20 ); g.drawRoundRect( 50 , 90 , 50 , 20 , 10 , 10 ); g.fillRect( 50 , 120 , 50 , 20 ); g.fillRoundRect( 50 , 150 , 50 , 20 , 10 , 10 ); } public static void main(String[] args) { new MyFrame(); } } |
Output:
Oval and Circle
The following methods are used to draw ovals and circles :
- void drawOval(int x, int y, int width, int height)
- void fillOval(int x, int y, int width, int height)
Example:
Java
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { public MyFrame() { setVisible( true ); setSize( 300 , 300 ); addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); } public void paint(Graphics g) { g.drawOval( 50 , 100 , 50 , 30 ); g.fillOval( 120 , 100 , 50 , 30 ); g.drawOval( 50 , 150 , 50 , 50 ); g.fillOval( 120 , 150 , 50 , 50 ); } public static void main(String[] args) { new MyFrame(); } } |
Output:
Arc
The following methods of Graphics class are used to draw arcs:
- void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
- void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Example:
Java
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { public MyFrame() { setVisible( true ); setSize( 300 , 300 ); addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); } public void paint(Graphics g) { g.drawArc( 100 , 100 , 80 , 70 , 90 , 90 ); g.fillArc( 100 , 150 , 80 , 70 , 90 , 90 ); } public static void main(String[] args) { new MyFrame(); } } |
Output:
Polygon
The following methods are used to draw polygons:
- void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
- void drawPolygon(Polygon p)
- void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
- void fillPolygon(Polygon p)
Example:
Java
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { public MyFrame() { setVisible( true ); setSize( 300 , 300 ); addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); } public void paint(Graphics g) { int [] x = { 50 , 100 , 150 , 200 , 150 , 100 }; int [] y1 = { 100 , 80 , 80 , 100 , 120 , 120 }; g.drawPolygon(x, y1, 6 ); int [] y2 = { 160 , 140 , 140 , 160 , 180 , 180 }; g.fillPolygon(x, y2, 6 ); } public static void main(String[] args) { new MyFrame(); } } |
Output: