Here you can find the source of gradientFillShape(Graphics2D g, Color startColor, Color endColor, Shape shape)
public static void gradientFillShape(Graphics2D g, Color startColor, Color endColor, Shape shape)
//package com.java2s; /*/*from w ww.java 2s .c om*/ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */ import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Shape; public class Main { public static void gradientFillShape(Graphics2D g, Color startColor, Color endColor, Shape shape) { initializeGraphics2D(g); GradientPaint gp = getGradientPaint(startColor, endColor, shape); g.setPaint(gp); g.fill(shape); } public static void initializeGraphics2D(Graphics2D g) { g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } public static GradientPaint getGradientPaint(Color startColor, Color endColor, Shape shape) { GradientPaint gp = new GradientPaint((int) shape.getBounds().getX(), (int) shape.getBounds().getY(), startColor, (int) (shape.getBounds().getX() + shape.getBounds().getWidth()), (int) (shape.getBounds().getY() + shape.getBounds().getHeight()), endColor, false); return gp; } }