Example usage for java.awt Graphics2D getPaint

List of usage examples for java.awt Graphics2D getPaint

Introduction

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

Prototype

public abstract Paint getPaint();

Source Link

Document

Returns the current Paint of the Graphics2D context.

Usage

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;//from   ww  w. ja v a2 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);
}

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;/*  ww w. jav  a 2 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:PaintUtils.java

/**
 * Paints a top to bottom gradient fill over the component bounds
 * from color1 to color2.//ww w.  jav a 2  s  . co  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:Main.java

public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();

    // Create the gradient paint
    GradientPaint paint = new GradientPaint(0, 0, start, width, height, end, true);

    // we need to cast to Graphics2D for this operation
    Graphics2D g2d = (Graphics2D) g;

    // save the old paint
    Paint oldPaint = g2d.getPaint();

    // set the paint to use for this operation
    g2d.setPaint(paint);//from w  w w.ja  v a2 s  . c  o  m

    // fill the background using the paint
    g2d.fillRect(0, 0, width, height);

    // restore the original paint
    g2d.setPaint(oldPaint);

    super.paint(g);
}

From source file:GradientPanel.java

public void paintComponent(Graphics g) {
    if (isOpaque()) {
        super.paintComponent(g);
        return;//from  ww  w  .  ja  v a2 s .c  om
    }

    int width = getWidth();
    int height = getHeight();

    // Create the gradient paint
    GradientPaint paint = null;

    Color sc = getForeground();
    Color ec = getBackground();

    switch (direction) {
    case HORIZONTAL: {
        paint = new GradientPaint(0, height / 2, sc, width, height / 2, ec, cyclic);
        break;
    }
    case VERTICAL: {
        paint = new GradientPaint(width / 2, 0, sc, width / 2, maxLength > 0 ? maxLength : height, ec, cyclic);
        break;
    }
    case DIAGONAL_LEFT: {
        paint = new GradientPaint(0, 0, sc, width, height, ec, cyclic);
        break;
    }
    case DIAGONAL_RIGHT: {
        paint = new GradientPaint(width, 0, sc, 0, height, ec, cyclic);
        break;
    }
    }

    if (paint == null) {
        throw new RuntimeException("Invalid direction specified in GradientPanel");
    }

    // we need to cast to Graphics2D for this operation
    Graphics2D g2d = (Graphics2D) g;

    // save the old paint
    Paint oldPaint = g2d.getPaint();

    // set the paint to use for this operation
    g2d.setPaint(paint);

    // fill the background using the paint
    g2d.fillRect(0, 0, width, height);

    // restore the original paint
    g2d.setPaint(oldPaint);

    super.paintComponent(g);
}

From source file:TwoStopsGradient.java

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    GradientPaint p;//from w  w w  .  j  av  a 2s  .  c o m
    p = new GradientPaint(0, 0, new Color(0xFFFFFF), 0, getHeight(), new Color(0xC8D2DE));

    Paint oldPaint = g2.getPaint();
    g2.setPaint(p);
    g2.fillRect(0, 0, getWidth(), getHeight());
    g2.setPaint(oldPaint);

    super.paintComponent(g);
}

From source file:org.fhcrc.cpl.toolbox.gui.chart.ChartMouseAndMotionListener.java

/**
 * Draws zoom rectangle (if present)./*  www .  jav  a 2 s  .com*/
 * The drawing is performed in XOR mode, therefore
 * when this method is called twice in a row,
 * the second call will completely restore the state
 * of the canvas.
 *
 * @param selectedRegion
 * @param stroke
 * @param fillColor
 */
protected void drawSelectedRegion(Rectangle2D selectedRegion, Stroke stroke, Color fillColor, boolean xorMode,
        Graphics2D g2) {
    // Set XOR mode to draw the zoom rectangle
    if (g2 == null)
        return;
    Paint origColor = g2.getPaint();

    if (selectedRegion != null) {
        if (xorMode) {
            g2.setXORMode(fillColor);
        } else
            g2.setPaint(fillColor);
        g2.fill(selectedRegion);
        g2.setPaint(Color.black);
        g2.setStroke(stroke);
        if (xorMode)
            g2.setPaint(Color.white);
        else
            g2.setPaint(Color.black);

        g2.draw(selectedRegion);
        g2.setPaintMode();
        g2.setPaint(origColor);
    }
}

From source file:edu.umd.cfar.lamp.chronicle.ChronicleRuler.java

private void drawLine(int x1, int y1, int x2, int y2, Paint p, Stroke stroke, PPaintContext paintContext) {
    Graphics2D g2 = paintContext.getGraphics();
    Paint old = g2.getPaint();
    Stroke oldS = g2.getStroke();
    g2.setPaint(p);/*from  w ww . j av  a 2  s.  co  m*/
    g2.setStroke(stroke);
    g2.drawLine(x1, y1, x2, y2);
    g2.setPaint(old);
    g2.setStroke(oldS);
}

From source file:TexturedPanel.java

/**
 * Paints this component with its textured background.
 *///from   ww w. ja v  a  2  s  .  com
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (ourPainter != null) {
        int w = getWidth();
        int h = getHeight();
        Insets in = getInsets();

        int x = in.left;
        int y = in.top;
        w = w - in.left - in.right;
        h = h - in.top - in.bottom;

        if (w >= 0 && h >= 0) {
            Graphics2D g2 = (Graphics2D) g;
            Paint pt = g2.getPaint();
            g2.setPaint(ourPainter);
            g2.fillRect(x, y, w, h);
            g2.setPaint(pt);
        }
    }
}

From source file:RadialGradientApp.java

@Override
protected void paintComponent(Graphics g) {
    setFont(getFont().deriveFont(70.f).deriveFont(Font.BOLD));

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Retains the previous state
    Paint oldPaint = g2.getPaint();

    // Fills the circle with solid blue color
    g2.setColor(new Color(0x0153CC));
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Adds shadows at the top
    Paint p;/*  w  ww  .  j a va 2 s. c o m*/
    p = new GradientPaint(0, 0, new Color(0.0f, 0.0f, 0.0f, 0.4f), 0, getHeight(),
            new Color(0.0f, 0.0f, 0.0f, 0.0f));
    g2.setPaint(p);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Adds highlights at the bottom 
    p = new GradientPaint(0, 0, new Color(1.0f, 1.0f, 1.0f, 0.0f), 0, getHeight(),
            new Color(1.0f, 1.0f, 1.0f, 0.4f));
    g2.setPaint(p);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Creates dark edges for 3D effect
    p = new RadialGradientPaint(new Point2D.Double(getWidth() / 2.0, getHeight() / 2.0), getWidth() / 2.0f,
            new float[] { 0.0f, 1.0f },
            new Color[] { new Color(6, 76, 160, 127), new Color(0.0f, 0.0f, 0.0f, 0.8f) });
    g2.setPaint(p);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Adds oval inner highlight at the bottom
    p = new RadialGradientPaint(new Point2D.Double(getWidth() / 2.0, getHeight() * 1.5), getWidth() / 2.3f,
            new Point2D.Double(getWidth() / 2.0, getHeight() * 1.75 + 6), new float[] { 0.0f, 0.8f },
            new Color[] { new Color(64, 142, 203, 255), new Color(64, 142, 203, 0) },
            RadialGradientPaint.CycleMethod.NO_CYCLE, RadialGradientPaint.ColorSpaceType.SRGB,
            AffineTransform.getScaleInstance(1.0, 0.5));
    g2.setPaint(p);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Adds oval specular highlight at the top left
    p = new RadialGradientPaint(new Point2D.Double(getWidth() / 2.0, getHeight() / 2.0), getWidth() / 1.4f,
            new Point2D.Double(45.0, 25.0), new float[] { 0.0f, 0.5f },
            new Color[] { new Color(1.0f, 1.0f, 1.0f, 0.4f), new Color(1.0f, 1.0f, 1.0f, 0.0f) },
            RadialGradientPaint.CycleMethod.NO_CYCLE);
    g2.setPaint(p);
    g2.fillOval(0, 0, getWidth() - 1, getHeight() - 1);

    // Restores the previous state
    g2.setPaint(oldPaint);

    // Draws the logo        
    //        FontRenderContext context = g2.getFontRenderContext();
    //        TextLayout layout = new TextLayout("R", getFont(), context);
    //        Rectangle2D bounds = layout.getBounds();
    //        
    //        float x = (getWidth() - (float) bounds.getWidth()) / 2.0f;
    //        float y = (getHeight() + (float) bounds.getHeight()) / 2.0f;
    //        
    //        g2.setColor(Color.WHITE);
    //        layout.draw(g2, x, y);
    //        
    //        Area shadow = new Area(layout.getOutline(null));
    //        shadow.subtract(new Area(layout.getOutline(AffineTransform.getTranslateInstance(1.0, 1.0))));
    //        g2.setColor(Color.BLACK);
    //        g2.translate(x, y);
    //        g2.fill(shadow);
    //        g2.translate(-x, -y);
}