Example usage for java.awt Graphics2D setPaint

List of usage examples for java.awt Graphics2D setPaint

Introduction

In this page you can find the example usage for java.awt Graphics2D setPaint.

Prototype

public abstract void setPaint(Paint paint);

Source Link

Document

Sets the Paint attribute for the Graphics2D context.

Usage

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public static final void yellowHighlight(Image image, Collection<Rectangle> boxes, float outsetFactor) {
    if (CollectionUtils.isEmpty(boxes))
        return;/*w  w  w .  j  a  v  a 2 s .co  m*/
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.setPaint(Color.YELLOW);
    Composite originalComposite = g2d.getComposite();
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
    g2d.setComposite(ac);
    for (Rectangle box : boxes) {
        if (outsetFactor != 1.0F) {
            box = new Rectangle(box);
            box.grow((int) (box.getHeight() * outsetFactor), (int) (box.getWidth() * outsetFactor));
        }
        g2d.fill(box);
    }
    g2d.setComposite(originalComposite);
}

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawLinearGradientPaintTest(Graphics2D g2) {
    // top left//from w w  w  .j  av a  2 s  . c  o m
    LinearGradientPaint lgp = new LinearGradientPaint(10, 30, 50, 30, new float[] { 0.0f, 1.0f },
            new Color[] { Color.RED, Color.BLUE });
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(10, 10, 40, 40));

    // top right
    lgp = new LinearGradientPaint(80, 10, 80, 50, new float[] { 0.0f, 1.0f },
            new Color[] { Color.RED, Color.BLUE });
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(60, 10, 40, 40));

    // bottom left
    lgp = new LinearGradientPaint(10, 100, 50, 60, new float[] { 0.0f, 1.0f },
            new Color[] { Color.RED, Color.BLUE });
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(10, 60, 40, 40));

    // bottom right
    lgp = new LinearGradientPaint(70, 70, 90, 90, new float[] { 0.0f, 0.5f, 1.0f },
            new Color[] { Color.RED, Color.YELLOW, Color.BLUE }, CycleMethod.REPEAT);
    g2.setPaint(lgp);
    g2.fill(new Rectangle2D.Double(60, 60, 40, 40));

}

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawRadialGradientPaintTest(Graphics2D g2) {
    RadialGradientPaint rgp = new RadialGradientPaint(50, 50, 40, 30, 30, new float[] { 0f, 0.75f, 1f },
            new Color[] { Color.RED, Color.GREEN, Color.BLUE }, MultipleGradientPaint.CycleMethod.NO_CYCLE);

    g2.setPaint(rgp);
    Ellipse2D circle = new Ellipse2D.Double(10, 10, 80, 80);
    g2.fill(circle);//  ww w  . j  ava2 s  . co  m
}

From source file:PaintUtils.java

/**
 * Paints a top to bottom gradient fill over the component bounds
 * from color1 to color2.//from www .j ava  2 s . c  o m
 */
public static void paintGradient(Graphics g, JComponent comp, Color color1, Color color2) {
    GradientPaint paint = new GradientPaint(0, 0, color1, 0, comp.getHeight(), color2, true);
    Graphics2D g2 = (Graphics2D) g;
    Paint oldPaint = g2.getPaint();
    g2.setPaint(paint);
    g2.fillRect(0, 0, comp.getWidth(), comp.getHeight());
    g2.setPaint(oldPaint);
}

From source file:Utils.java

public static BufferedImage createGradientMask(int width, int height, int orientation) {
    // algorithm derived from Romain Guy's blog
    BufferedImage gradient = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = gradient.createGraphics();
    GradientPaint paint = new GradientPaint(0.0f, 0.0f, new Color(1.0f, 1.0f, 1.0f, 1.0f),
            orientation == SwingConstants.HORIZONTAL ? width : 0.0f,
            orientation == SwingConstants.VERTICAL ? height : 0.0f, new Color(1.0f, 1.0f, 1.0f, 0.0f));
    g.setPaint(paint);
    g.fill(new Rectangle2D.Double(0, 0, width, height));

    g.dispose();// w  w  w . j  a  v  a 2 s  .  c  o m
    gradient.flush();

    return gradient;
}

From source file:Main.java

public static void smoothFillHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    Graphics2D g2D = (Graphics2D) g;
    Paint savedPaint = g2D.getPaint();
    int steps = colors.length;
    double dy = (double) h / (double) (steps - 1);
    int y1 = y;/*from   w  ww. j  a va2  s.  c o  m*/
    for (int i = 0; i < steps; i++) {
        int y2 = y + (int) Math.round((double) i * dy);
        if (i == (steps - 1)) {
            g2D.setPaint(null);
            g2D.setColor(colors[i]);
            g2D.fillRect(x, y1, w, y + h - y1);
        } else {
            g2D.setPaint(new GradientPaint(0, y1, colors[i], 0, y2, colors[i + 1]));
            g2D.fillRect(x, y1, w, y2 - y1);
        }
        y1 = y2;
    }
    g2D.setPaint(savedPaint);
}

From source file:org.jfree.data.general.HeatMapUtilities.java

/**
 * Creates an image that displays the values from the specified dataset.
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param paintScale  the paint scale for the z-values (<code>null</code>
 *         not permitted).//from  w w  w  .j  av  a 2  s.  co m
 *
 * @return A buffered image.
 */
public static BufferedImage createHeatMapImage(HeatMapDataset dataset, PaintScale paintScale) {

    ParamChecks.nullNotPermitted(dataset, "dataset");
    ParamChecks.nullNotPermitted(paintScale, "paintScale");
    int xCount = dataset.getXSampleCount();
    int yCount = dataset.getYSampleCount();
    BufferedImage image = new BufferedImage(xCount, yCount, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    for (int xIndex = 0; xIndex < xCount; xIndex++) {
        for (int yIndex = 0; yIndex < yCount; yIndex++) {
            double z = dataset.getZValue(xIndex, yIndex);
            Paint p = paintScale.getPaint(z);
            g2.setPaint(p);
            g2.fillRect(xIndex, yCount - yIndex - 1, 1, 1);
        }
    }
    return image;
}

From source file:org.jfree.data.general.HeatMapUtils.java

/**
 * Creates an image that displays the values from the specified dataset.
 *
 * @param dataset  the dataset ({@code null} not permitted).
 * @param paintScale  the paint scale for the z-values ({@code null}
 *         not permitted)./*from w w w.  j a va  2  s  . c om*/
 *
 * @return A buffered image.
 */
public static BufferedImage createHeatMapImage(HeatMapDataset dataset, PaintScale paintScale) {

    Args.nullNotPermitted(dataset, "dataset");
    Args.nullNotPermitted(paintScale, "paintScale");
    int xCount = dataset.getXSampleCount();
    int yCount = dataset.getYSampleCount();
    BufferedImage image = new BufferedImage(xCount, yCount, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    for (int xIndex = 0; xIndex < xCount; xIndex++) {
        for (int yIndex = 0; yIndex < yCount; yIndex++) {
            double z = dataset.getZValue(xIndex, yIndex);
            Paint p = paintScale.getPaint(z);
            g2.setPaint(p);
            g2.fillRect(xIndex, yCount - yIndex - 1, 1, 1);
        }
    }
    return image;
}

From source file:Main.java

/**
 * Fills a gradient using the start color and end color specified.
 *
 * @param g//from  w w  w .  j  a v a 2  s.  c o  m
 *          the gradient
 * @param s
 *          the shape to fill
 * @param start
 *          the start color
 * @param end
 *          the end color
 * @param vertical
 *          true for a vertical gradient; false for horizontal
 */
public static void fillGradient(Graphics2D g, Shape s, Color start, Color end, boolean vertical) {
    Rectangle r = s.getBounds();
    Paint gp = new GradientPaint(0, 0, start, vertical ? 0 : r.width, vertical ? r.height : 0, end);
    Object o = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setPaint(gp);
    g.fill(s);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, o);
}

From source file:Main.java

public static void smoothFillInverseHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    Graphics2D g2D = (Graphics2D) g;
    Paint savedPaint = g2D.getPaint();
    int steps = colors.length;
    double dy = (double) h / (double) steps;
    int y1 = y;/*w  ww .j  a  va2 s.  c  o  m*/
    for (int i = 0; i < steps; i++) {
        int y2 = y + (int) Math.round((double) i * dy);
        g.setColor(colors[colors.length - i - 1]);
        if (i == (steps - 1)) {
            g2D.setPaint(null);
            g2D.setColor(colors[colors.length - i - 1]);
            g.fillRect(x, y1, w, y + h - y1);
        } else {
            g2D.setPaint(new GradientPaint(0, y1, colors[colors.length - i - 1], 0, y2,
                    colors[colors.length - i - 2]));
            g.fillRect(x, y1, w, y2 - y1);
        }
        y1 = y2;
    }
    g2D.setPaint(savedPaint);
}