Java Graphics Draw String drawGradient(Graphics g, JComponent c, String prefix)

Here you can find the source of drawGradient(Graphics g, JComponent c, String prefix)

Description

draw Gradient

License

Apache License

Declaration

public static void drawGradient(Graphics g, JComponent c, String prefix) 

Method Source Code

//package com.java2s;
/*//from  ww w .  j  ava 2s. c  o m
 * Copyright 2005 Patrick Gotthardt
 *
 * Licensed 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 javax.swing.*;

import java.awt.*;

import java.util.HashMap;

public class Main {
    /**
     * This code is taken from the "TabContainer" of NetBeans
     */
    /*
     *                 Sun Public License Notice
     *
     * The contents of this file are subject to the Sun Public License
     * Version 1.0 (the "License"). You may not use this file except in
     * compliance with the License. A copy of the License is available at
     * http://www.sun.com/
     *
     * The Original Code is NetBeans. The Initial Developer of the Original
     * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
     * Microsystems, Inc. All Rights Reserved.
     */
    private static HashMap gpCache;

    public static void drawGradient(Graphics g, int width, int height,
            Color from, Color to) {
        drawGradient(g, 0, 0, width, height, from, to);
    }

    public static void drawGradient(Graphics g, int x, int y, int width,
            int height, Color from, Color to) {
        Graphics2D gfx = (Graphics2D) g;
        gfx.setPaint(getGradientPaint(x, y, width, height, from, to));
        gfx.fill(new Rectangle(x, y, width, height));
    }

    public static void drawGradient(Graphics g, JComponent c,
            String prefix, int x, int y, int width, int height) {
        drawGradient(g, x, y, width, height,
                (Color) c.getClientProperty(prefix + ".gradientStart"),
                (Color) c.getClientProperty(prefix + ".gradientEnd"));
    }

    public static void drawGradient(Graphics g, JComponent c, String prefix) {
        drawGradient(g, c.getWidth(), c.getHeight(),
                (Color) c.getClientProperty(prefix + ".gradientStart"),
                (Color) c.getClientProperty(prefix + ".gradientEnd"));
    }

    public static void drawGradient(Graphics g, JComponent c,
            String prefix1, String prefix2) {
        drawGradient(g, c.getWidth(), c.getHeight(),
                (Color) c.getClientProperty(prefix1 + ".gradientStart"),
                (Color) c.getClientProperty(prefix2 + ".gradientEnd"));
    }

    public static void drawGradient(Graphics g, JComponent c) {
        drawGradient(g, c.getWidth(), c.getHeight(),
                (Color) c.getClientProperty("gradientStart"),
                (Color) c.getClientProperty("gradientEnd"));
    }

    public static GradientPaint getGradientPaint(int x, int y, int width,
            int height, Color from, Color to) {
        if (gpCache == null) {
            gpCache = new HashMap(20);
        }

        //Generate a hash code for looking up an existing paint
        long bits = Double.doubleToLongBits(x) + Double.doubleToLongBits(y)
                * 37 + Double.doubleToLongBits(width) * 43
                + Double.doubleToLongBits(height) * 47;
        int hash = ((((int) bits) ^ ((int) (bits >> 32))) ^ from.hashCode() ^ (to
                .hashCode() * 17)) * 31;

        Object key = new Integer(hash);
        GradientPaint result = (GradientPaint) gpCache.get(key);
        if (result == null) {
            result = new GradientPaint(0, 0, from, 0, height, to, true);
            if (gpCache.size() > 40) {
                gpCache.clear();
            }
            gpCache.put(key, result);
        }
        return result;
    }
}

Related

  1. drawCenteredText(java.awt.Graphics g, String s, java.awt.Rectangle rect)
  2. drawClippedString(Graphics g, String t, int x, int y, int width)
  3. drawEmphasizedString(Graphics g, Color foreground, Color emphasis, String s, int underlinedIndex, int x, int y)
  4. drawFitText(Graphics2D g2, int x, int y, int width, int height, String text)
  5. drawFormattedString(Graphics2D g2, String htmlStr, int x, int y, int w, int h)
  6. drawGradientText(Graphics g, String text, int x, int y, Color c)
  7. drawLine(String pLine, Graphics2D pG, int pX, int pY, int pWidth, String pJustification)
  8. drawLineDrawChar(Graphics g, int x, int y, int bi, char c, int charWidth, int charHeight)
  9. drawMessage(Graphics2D g, String message)