Introduction

In this article, we discuss graphics programming using Swing in Java.

Graphics in Swing

The java.awt.Graphics class provides many methods for graphics programming, including the following:
Graphics are used to design rectangles, lines, ovals, arcs, circles, etc.

Draw a simple oval in Java

In this example we draw an oval using Swing methods:
  1. import javax.swing.JFrame;
  2. import java.awt.*;
  3. public class DisplayOval extends Canvas {
  4. public void paint(Graphics grap) {
  5. grap.drawOval(25, 140, 40, 70);
  6. }
  7. public static void main(String[] args) {
  8. DisplayOval o = new DisplayOval();
  9. JFrame f = new JFrame();
  10. f.add(o);
  11. f.setSize(500, 280);
  12. //f.setLayout(null);
  13. f.setVisible(true);
  14. }
  15. }
Output
pic-1.jpg
Now, fill in the color for the oval using the fillOval() method; see:
  1. import javax.swing.JFrame;
  2. import java.awt.*;
  3. public class FillOval extends Canvas {
  4. public void paint(Graphics grap) {
  5. grap.drawOval(25, 140, 40, 70);
  6. setForeground(Color.RED);
  7. grap.fillOval(140, 140, 40, 60);
  8. }
  9. public static void main(String[] args) {
  10. FillOval o = new FillOval();
  11. JFrame jf = new JFrame();
  12. jf.add(o);
  13. jf.setSize(500, 280);
  14. //f.setLayout(null);
  15. jf.setVisible(true);
  16. }
  17. }
Output
pic-2.jpg

Draw an Arc in Java

The following will draw an Arc using Swing:
  1. import javax.swing.JFrame;
  2. import java.awt.*;
  3. public class DrawArc extends Canvas {
  4. public void paint(Graphics grap) {
  5. grap.drawArc(10, 20, 140, 150, 90, 60);
  6. }
  7. public static void main(String[] args) {
  8. DrawArc o = new DrawArc();
  9. JFrame jf = new JFrame();
  10. jf.add(o);
  11. jf.setSize(500, 280);
  12. //f.setLayout(null);
  13. jf.setVisible(true);
  14. }
  15. }
Output
pic-3.jpg

Draw a rectangle in Java

The following will draw a rectangle using Swing:
  1. import javax.swing.JFrame;
  2. import java.awt.*;
  3. public class DrawRectangle extends Canvas {
  4. public void paint(Graphics grap) {
  5. grap.drawRect(150, 40, 140, 60);
  6. }
  7. public static void main(String[] args) {
  8. DrawRectangle o = new DrawRectangle();
  9. JFrame jf = new JFrame();
  10. jf.add(o);
  11. jf.setSize(500, 280);
  12. //jf.setLayout(null);
  13. jf.setVisible(true);
  14. }
  15. }
Output
pic-4.jpg

Fill in color in rectangle in Java

The following will fill in the rectangle's color:
  1. import javax.swing.JFrame;
  2. import java.awt.*;
  3. public class FillRectangle extends Canvas {
  4. public void paint(Graphics grap) {
  5. grap.fillRect(150, 40, 140, 60);
  6. setForeground(Color.BLUE);
  7. }
  8. public static void main(String[] args) {
  9. FillRectangle o = new FillRectangle();
  10. JFrame jf = new JFrame();
  11. jf.add(o);
  12. jf.setSize(500, 280);
  13. //jf.setLayout(null);
  14. jf.setVisible(true);
  15. }
  16. }
Output
pic-5.jpg
Note: Similarly, we can draw other graphics using graphics methods.
For a string we use:
  1. void drawString(String text, int x, int y)
For a line we use:
  1. void drawLine(int x1, int y1, int x2, int y2)
To change the color we use:
  1. void setColor(Color color)
Conclusion
In this article, we learned about Graphics Programming using Swing in Java, list of all the methods present in java.awt.Graphics and how to draw various graphical elements in Java