Graphics Programming Using Swing In Java

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:
  • void setColor(Color color) used for set the graphics of current color to specified color.
  • void setFont(Font font) used to set the font graphics.
  • void drawString(String text, int x, int y) is used to draw a string.
  • void drawLine(int x1, int y1, int x2, int y2) is used to draw a line between given points.
  • void drawRect(int x, int y, int width, int height) is used to draw rectangle.
  • void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
  • void drawOval(int x, int y, int width, int height) is used to draw oval
  • void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) is used to draw a circular or elliptical arc.
  • void fillRect(int x, int y, int width, int height) is used to fill rectangle with default color.
  • void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
  • void fillOval(int x, int y, int width, int height)  is used to fill oval with default color.
  • void fillArc(int x, int y, int width, int height, int startAngle, int angularExtent) is used to fill circular or elliptical arc.
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(251404070);  
  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(500280);  
  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(251404070);  
  6.         setForeground(Color.RED);  
  7.         grap.fillOval(1401404060);  
  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(500280);  
  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(10201401509060);  
  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(500280);  
  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(1504014060);  
  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(500280);  
  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(1504014060);  
  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(500280);  
  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