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
- import java.awt.Color;
- import java.awt.GradientPaint;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class GradientPaintDeom1 extends JPanel
- {
- public void paint(Graphics g)
- {
- super.paint(g);
- Graphics2D g2d = (Graphics2D) g;
- GradientPaint gp1 = new GradientPaint(10, 20, Color.green, 150, 150, Color.black);
- g2d.setPaint(gp1);
- g2d.fillRect(00, 00, 350, 350);
- }
- public static void main(String[] args)
- {
- JFrame frame = new JFrame("GradientsGreenBlack");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(new GradientPaintDeom1());
- frame.setSize(350, 350);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- }
OUTPUT
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
- import java.awt.Color;
- import java.awt.GradientPaint;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class GradientPaintDemo2 extends JPanel
- {
- public void paint(Graphics g)
- {
- super.paint(g);
- Graphics2D g2d = (Graphics2D) g;
- GradientPaint gp1 = new GradientPaint(15, 15, Color.green, 40, 40, Color.black, true);
- g2d.setPaint(gp1);
- g2d.fillRect(00, 00, 350, 350);
- }
- public static void main(String[] args)
- {
- JFrame frame = new JFrame("GradientsGreenBlacK");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(new GradientPaintDemo2());
- frame.setSize(350, 350);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- }
OUTPUT
In the upper program you can change in the following line like as you found in the following output.
The line is:
- GradientPaint gp1 = new GradientPaint(5, 5, Color.green, 40, 40,
- Color.red, true);