Java Graphics Settings gradientFillShape(Graphics2D g, Color startColor, Color endColor, Shape shape)

Here you can find the source of gradientFillShape(Graphics2D g, Color startColor, Color endColor, Shape shape)

Description

gradient Fill Shape

License

Apache License

Declaration

public static void gradientFillShape(Graphics2D g, Color startColor, Color endColor, Shape shape) 

Method Source Code


//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;
    }
}

Related

  1. getRenderingHints(final Graphics2D g2d, final Map hintsToSave, Map savedHints)
  2. getRenderingHints(Graphics2D g2d, Map hintsToSave, RenderingHints savedHints)
  3. getRenderingHints(Graphics2D g2d, RenderingHints hintsToSave, RenderingHints savedHints)
  4. getSecondWindowConfiguration()
  5. getTransparentGraphicsConfiguration()
  6. gradientPaint(Graphics2D g2d, Point2D startPosition, Color startColor, Point2D endPosition, Color endColor, int sx, int sy, int ex, int ey)
  7. setClip(Graphics g, Rectangle clipBounds)
  8. setRenderingHints(Graphics2D g)
  9. setRenderingHints(Graphics2D g2d)