Working GradientPaint Class in Java

Introduction

 
In this article, we are going to describe how to use the GradientPaint class to make a better UI. This class is found in the AWT package in Java. And the GradientPaint class provides a way to fill a shape with a linear colour gradient pattern. If Point P1 with Color C1 and Point P2 with Color C2 are specified in the user space, the Color on the P1, P2 connecting line is proportionally changed from C1 to C2. Any point P not on the extended P1, P2 connecting line has the colour of the point P' that is the perpendicular projection of P on the extended P1, P2 connecting line. Points on the extended line outside of the P1, P2 segment can be coloured in one of two ways.
  • If the gradient is cyclic then the points on the extended P1, P2 connecting line cycle back and forth between the colours C1 and C2.
  • If the gradient is acyclic then points on the P1 side of the segment have the constant Color C1 while points on the P2 side have the constant Color C2.
Constructor Details
 
1- public GradientPaint(float x1,float y1,Color color1,float x2,float y2,Color color2)
 
This constructor is used to construct a simple acyclic GradientPaint object. And this constructor takes six arguments; the first argument is a float value as x1 for the first coordinate point and the second argument takes y1 and this is the y coordinate of the first point and the third argument is a color1 as Color class object and the fourth and fifth arguments x2 and y2 specify the endpoints of the x and y coordinates.
 
Example
  1. import java.awt.Color;  
  2. import java.awt.GradientPaint;  
  3. import java.awt.Graphics;  
  4. import java.awt.Graphics2D;  
  5. import javax.swing.JFrame;  
  6. import javax.swing.JPanel;  
  7. public class GradientPaintDeom1 extends JPanel   
  8. {  
  9.  public void paint(Graphics g)   
  10.  {  
  11.   super.paint(g);  
  12.   Graphics2D g2d = (Graphics2D) g;  
  13.   GradientPaint gp1 = new GradientPaint(1020, Color.green, 150150, Color.black);  
  14.   g2d.setPaint(gp1);  
  15.   g2d.fillRect(0000350350);  
  16.  }  
  17.  public static void main(String[] args)   
  18.  {  
  19.   JFrame frame = new JFrame("GradientsGreenBlack");  
  20.   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  21.   frame.add(new GradientPaintDeom1());  
  22.   frame.setSize(350350);  
  23.   frame.setLocationRelativeTo(null);  
  24.   frame.setVisible(true);  
  25.  }  
  26. }  
 OUTPUT
 
Clipboard00.gif
 
2-public GradientPaint(float x1,float y1,Color color1,float x2,float y2,Color color2,boolean cyclic,boolean cyclic)
 
This constructor is used to construct a simple cyclic GradientPaint object. And this constructor takes six arguments; the first argument is a float value as x1 for the first coordinate point and the second argument takes y1 and this is the y coordinate of the first point and the third argument is a color1 as Color class object and the fourth and fifth arguments x2 and y2 specify the endpoint of the x and y coordinates. And the last argument is a Boolean that indicates if you want to cycle between two colours or not.
 
Example
  1. import java.awt.Color;  
  2. import java.awt.GradientPaint;  
  3. import java.awt.Graphics;  
  4. import java.awt.Graphics2D;  
  5. import javax.swing.JFrame;  
  6. import javax.swing.JPanel;  
  7. public class GradientPaintDemo2 extends JPanel   
  8. {  
  9.  public void paint(Graphics g)   
  10.  {  
  11.   super.paint(g);  
  12.   Graphics2D g2d = (Graphics2D) g;  
  13.   GradientPaint gp1 = new GradientPaint(1515, Color.green, 4040, Color.black, true);  
  14.   g2d.setPaint(gp1);  
  15.   g2d.fillRect(0000350350);  
  16.  }  
  17.  public static void main(String[] args)   
  18.  {  
  19.   JFrame frame = new JFrame("GradientsGreenBlacK");  
  20.   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  21.   frame.add(new GradientPaintDemo2());  
  22.   frame.setSize(350350);  
  23.   frame.setLocationRelativeTo(null);  
  24.   frame.setVisible(true);  
  25.  }  
  26. }  
OUTPUT
 
Clipboard04.gif
 
In the upper program you can change in the following line like as you found in the following output.
 
The line is:
  1. GradientPaint gp1 = new GradientPaint(55, Color.green, 4040,  
  2.                         Color.red, true);   
Clipboard03.gif