Java Graphics Draw Gradient DrawGradient(Graphics g, int x, int y, int width, int height, Color color1, Color color2)

Here you can find the source of DrawGradient(Graphics g, int x, int y, int width, int height, Color color1, Color color2)

Description

Draws a gradient

License

Open Source License

Parameter

Parameter Description
g the graphics object to use to draw with
x the left position
y the top position
width with width of the gradient
height the height of the gradient
color1 the first color of the gradient
color2 the second color of the gradient

Declaration

public static void DrawGradient(Graphics g, int x, int y, int width, int height, Color color1, Color color2) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class Main {
    /**//  w  ww .  jav a 2  s  . co  m
     * Draws a gradient
     * @param g the graphics object to use to draw with
     * @param x the left position
     * @param y the top position
     * @param width with width of the gradient
     * @param height the height of the gradient
     * @param color1 the first color of the gradient
     * @param color2 the second color of the gradient
     */
    public static void DrawGradient(Graphics g, int x, int y, int width, int height, Color color1, Color color2) {
        Graphics2D graphics2D = (Graphics2D) g;

        {
            GradientPaint gradient = new GradientPaint(width / 2, y, color1, width / 2, height / 2, color2);

            Rectangle2D rect = new Rectangle2D.Double(x, y, width, height / 2);

            graphics2D.setPaint(gradient);
            graphics2D.fill(rect);
        }

        {
            GradientPaint gradient = new GradientPaint(width / 2, height / 2, color2, width / 2, height, color1);

            Rectangle2D rect = new Rectangle2D.Double(x, y + height / 2, width, height);

            graphics2D.setPaint(gradient);
            graphics2D.fill(rect);
        }
    }
}

Related

  1. linearGradient(final JComponent comp, final Color col, final int step, final int sleepTime)
  2. paintGradient(Graphics g, JComponent comp, Color color1, Color color2)
  3. paintGradientSelection(Color topLineColor, Color bottomLineColor, Color topGradientColor, Color bottomGradientColor, Graphics2D graphics2D, int width, int height)
  4. paintHorizontalGradient2D(Graphics2D g, int x, int y, int w, int h, float g1, float g2, Color c1, Color c2, Color c3, int[][] mask)