Search in sources :

Example 31 with GradientPaint

use of java.awt.GradientPaint in project yamcs-studio by yamcs.

the class ImageUtils method changeImageColor.

/**
 * Apply color change on an image.
 *
 * @param color
 * @param imageData
 */
public static ImageData changeImageColor(Color color, ImageData originalImageData) {
    if (color == null || originalImageData == null || color.getRGB().equals(new RGB(0, 0, 0)))
        return originalImageData;
    ImageData imageData = ImageUtils.convertToGrayscale(originalImageData);
    int luminance = (int) Math.round((0.299 * color.getRed()) + (0.587 * color.getGreen()) + (0.114 * color.getBlue()));
    // find min/max/average values ignoring white & transparent
    int sum = 0, count = 0, min = 0, max = 0;
    int[] lineData = new int[imageData.width];
    PaletteData palette = imageData.palette;
    for (int y = 0; y < imageData.height; y++) {
        imageData.getPixels(0, y, imageData.width, lineData, 0);
        // Analyze each pixel value in the line
        for (int x = 0; x < lineData.length; x++) {
            int pixelValue = lineData[x];
            // Do not set transparent pixel
            if (lineData[x] != imageData.transparentPixel) {
                // Get pixel color value if not using direct palette
                if (!palette.isDirect) {
                    pixelValue = palette.getPixel(palette.colors[lineData[x]]);
                }
                RGB current = palette.getRGB(pixelValue);
                if (current.blue == current.green && current.blue == current.red && current.blue < 255) {
                    min = Math.min(current.red, min);
                    max = Math.max(current.red, max);
                    sum += current.red;
                    count++;
                }
            }
        }
    }
    if (count == 0)
        return imageData;
    // we need to adjust the gradient depending on the luminance unless
    // bright colors will appear in white
    int gradientWidth = 512, gradientHeight = 10;
    int average = (int) sum / count;
    int start = average - 32;
    if (start < 0)
        start = 0;
    int end = max + luminance;
    if (end > gradientWidth - 1)
        end = gradientWidth - 1;
    // create the color gradient
    java.awt.Color color1 = new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue());
    java.awt.Color color2 = new java.awt.Color(250, 250, 255);
    BufferedImage gradient = new BufferedImage(gradientWidth, gradientHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = gradient.createGraphics();
    g2.setPaint(color1);
    g2.fill(new java.awt.Rectangle(0, 0, start, gradientHeight));
    g2.setPaint(new GradientPaint(start, 0, color1, end, gradientHeight, color2, false));
    g2.fill(new java.awt.Rectangle(start, 0, end - start, gradientHeight));
    g2.setPaint(color2);
    g2.fill(new java.awt.Rectangle(end, 0, gradientWidth - end, gradientHeight));
    for (int y = 0; y < imageData.height; y++) {
        imageData.getPixels(0, y, imageData.width, lineData, 0);
        // Analyze each pixel value in the line
        for (int x = 0; x < lineData.length; x++) {
            int pixelValue = lineData[x];
            // Do not set transparent pixel
            if (lineData[x] != imageData.transparentPixel) {
                // Get pixel color value if not using direct palette
                if (!palette.isDirect) {
                    pixelValue = palette.getPixel(palette.colors[lineData[x]]);
                }
                RGB current = palette.getRGB(pixelValue);
                if (current.blue == current.green && current.blue == current.red && current.blue < 255) {
                    int gradientRGB = gradient.getRGB(current.red, 0);
                    java.awt.Color gradientColor = new java.awt.Color(gradientRGB);
                    RGB degraded = new RGB(gradientColor.getRed(), gradientColor.getGreen(), gradientColor.getBlue());
                    if (palette.isDirect) {
                        int appliedColor = palette.getPixel(degraded);
                        imageData.setPixel(x, y, appliedColor);
                    } else {
                        palette.colors[lineData[x]] = degraded;
                    }
                }
            }
        }
    }
    return imageData;
}
Also used : Color(org.eclipse.swt.graphics.Color) GradientPaint(java.awt.GradientPaint) RGB(org.eclipse.swt.graphics.RGB) GradientPaint(java.awt.GradientPaint) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData)

Example 32 with GradientPaint

use of java.awt.GradientPaint in project processdash by dtuma.

the class TrashCanIcon method paintIcon.

@Override
protected void paintIcon(Graphics2D g2, int width, int height, float scale) {
    // calculate the gradient for shading
    Color shadow = PaintUtils.mixColors(fill, edge, 0.3f);
    GradientPaint gradient = new GradientPaint(0, 0, fill, width, 0, shadow);
    // draw the can
    float s2 = 2 * scale;
    int hPad = (int) s2;
    int ww = width - 2 * hPad - 2;
    int top = (int) (2 * scale + 1);
    RoundRectangle2D can = new //
    RoundRectangle2D.Float(//
    hPad, //
    top, ww, height - top - 1, s2, s2);
    g2.setPaint(gradient);
    g2.fill(can);
    g2.setColor(edge);
    g2.draw(can);
    // draw lines on the can
    int lineSpacing = Math.min(5, Math.max(2, ww / 5));
    int lineIndent = Math.max(2, (lineSpacing + ww % lineSpacing) / 2);
    for (int x = hPad + lineIndent; x < can.getMaxX() - 1; x += lineSpacing) g2.drawLine(x, top, x, height - 3);
    // draw the handle
    hPad = (int) (5 * scale);
    ww = width - 2 * hPad - 2;
    g2.setStroke(new BasicStroke(1));
    g2.setColor(edge);
    g2.draw(new RoundRectangle2D.Float(hPad, 0, ww, 3 * scale, s2, s2));
    // draw the lid
    hPad = (int) scale;
    ww = width - 2 * hPad - 2;
    int lh = (int) (2 * scale + 0.5);
    Area lid = new Area();
    lid.add(new Area(new //
    RoundRectangle2D.Float(//
    hPad, //
    2 * hPad, ww, lh * 2, s2, s2)));
    lid.intersect(new Area(new Rectangle2D.Float(hPad, 2 * hPad, ww, lh)));
    g2.setPaint(gradient);
    g2.fill(lid);
    g2.setPaint(edge);
    g2.draw(lid);
}
Also used : BasicStroke(java.awt.BasicStroke) Area(java.awt.geom.Area) Color(java.awt.Color) RoundRectangle2D(java.awt.geom.RoundRectangle2D) GradientPaint(java.awt.GradientPaint) GradientPaint(java.awt.GradientPaint)

Example 33 with GradientPaint

use of java.awt.GradientPaint in project jdk8u_jdk by JetBrains.

the class BufferedPaints method setGradientPaint.

/************************* GradientPaint support ****************************/
/**
     * Note: This code is factored out into a separate static method
     * so that it can be shared by both the Gradient and LinearGradient
     * implementations.  LinearGradient uses this code (for the
     * two-color sRGB case only) because it can be much faster than the
     * equivalent implementation that uses fragment shaders.
     *
     * We use OpenGL's texture coordinate generator to automatically
     * apply a smooth gradient (either cyclic or acyclic) to the geometry
     * being rendered.  This technique is almost identical to the one
     * described in the comments for BufferedPaints.setTexturePaint(),
     * except the calculations take place in one dimension instead of two.
     * Instead of an anchor rectangle in the TexturePaint case, we use
     * the vector between the two GradientPaint end points in our
     * calculations.  The generator uses a single plane equation that
     * takes the (x,y) location (in device space) of the fragment being
     * rendered to calculate a (u) texture coordinate for that fragment:
     *     u = Ax + By + Cz + Dw
     *
     * The gradient renderer uses a two-pixel 1D texture where the first
     * pixel contains the first GradientPaint color, and the second pixel
     * contains the second GradientPaint color.  (Note that we use the
     * GL_CLAMP_TO_EDGE wrapping mode for acyclic gradients so that we
     * clamp the colors properly at the extremes.)  The following diagram
     * attempts to show the layout of the texture containing the two
     * GradientPaint colors (C1 and C2):
     *
     *                        +-----------------+
     *                        |   C1   |   C2   |
     *                        |        |        |
     *                        +-----------------+
     *                      u=0  .25  .5   .75  1
     *
     * We calculate our plane equation constants (A,B,D) such that u=0.25
     * corresponds to the first GradientPaint end point in user space and
     * u=0.75 corresponds to the second end point.  This is somewhat
     * non-obvious, but since the gradient colors are generated by
     * interpolating between C1 and C2, we want the pure color at the
     * end points, and we will get the pure color only when u correlates
     * to the center of a texel.  The following chart shows the expected
     * color for some sample values of u (where C' is the color halfway
     * between C1 and C2):
     *
     *       u value      acyclic (GL_CLAMP)      cyclic (GL_REPEAT)
     *       -------      ------------------      ------------------
     *        -0.25              C1                       C2
     *         0.0               C1                       C'
     *         0.25              C1                       C1
     *         0.5               C'                       C'
     *         0.75              C2                       C2
     *         1.0               C2                       C'
     *         1.25              C2                       C1
     *
     * Original inspiration for this technique came from UMD's Agile2D
     * project (GradientManager.java).
     */
private static void setGradientPaint(RenderQueue rq, AffineTransform at, Color c1, Color c2, Point2D pt1, Point2D pt2, boolean isCyclic, boolean useMask) {
    // convert gradient colors to IntArgbPre format
    PixelConverter pc = PixelConverter.ArgbPre.instance;
    int pixel1 = pc.rgbToPixel(c1.getRGB(), null);
    int pixel2 = pc.rgbToPixel(c2.getRGB(), null);
    // calculate plane equation constants
    double x = pt1.getX();
    double y = pt1.getY();
    at.translate(x, y);
    // now gradient point 1 is at the origin
    x = pt2.getX() - x;
    y = pt2.getY() - y;
    double len = Math.sqrt(x * x + y * y);
    at.rotate(x, y);
    // now gradient point 2 is on the positive x-axis
    at.scale(2 * len, 1);
    // now gradient point 2 is at (0.5, 0)
    at.translate(-0.25, 0);
    // now gradient point 1 is at (0.25, 0), point 2 is at (0.75, 0)
    double p0, p1, p3;
    try {
        at.invert();
        p0 = at.getScaleX();
        p1 = at.getShearX();
        p3 = at.getTranslateX();
    } catch (java.awt.geom.NoninvertibleTransformException e) {
        p0 = p1 = p3 = 0.0;
    }
    // assert rq.lock.isHeldByCurrentThread();
    rq.ensureCapacityAndAlignment(44, 12);
    RenderBuffer buf = rq.getBuffer();
    buf.putInt(SET_GRADIENT_PAINT);
    buf.putInt(useMask ? 1 : 0);
    buf.putInt(isCyclic ? 1 : 0);
    buf.putDouble(p0).putDouble(p1).putDouble(p3);
    buf.putInt(pixel1).putInt(pixel2);
}
Also used : PixelConverter(sun.awt.image.PixelConverter) TexturePaint(java.awt.TexturePaint) LinearGradientPaint(java.awt.LinearGradientPaint) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint) MultipleGradientPaint(java.awt.MultipleGradientPaint) GradientPaint(java.awt.GradientPaint)

Example 34 with GradientPaint

use of java.awt.GradientPaint in project gitblit by gitblit.

the class HeaderPanel method paintComponent.

@Override
public void paintComponent(Graphics oldG) {
    Graphics2D g = (Graphics2D) oldG;
    Point2D startPoint = new Point2D.Float(0, 0);
    Point2D endPoint = new Point2D.Float(0, getHeight());
    Paint gradientPaint = new GradientPaint(startPoint, lightColor, endPoint, getBackground(), false);
    g.setPaint(gradientPaint);
    g.fill(new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
    g.setColor(new Color(0xff, 0x99, 0x00));
    int stroke = 2;
    g.setStroke(new BasicStroke(stroke));
    g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
}
Also used : BasicStroke(java.awt.BasicStroke) Point2D(java.awt.geom.Point2D) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) GradientPaint(java.awt.GradientPaint) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) Paint(java.awt.Paint) GradientPaint(java.awt.GradientPaint) Graphics2D(java.awt.Graphics2D)

Example 35 with GradientPaint

use of java.awt.GradientPaint in project EnrichmentMapApp by BaderLab.

the class ColorLegendPanel method paint.

@Override
public void paint(Graphics g) {
    final int w = getWidth();
    final int h = getHeight();
    if (w <= 0 || h <= 0)
        return;
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    float ww = w / 5.f;
    // To center the legend horizontally
    float hPad = ww / 2.f;
    //Gradient line start
    Point2D.Float p1 = new Point2D.Float(hPad, 0.f);
    //Gradient line end
    Point2D.Float p2 = new Point2D.Float(hPad + ww, 0.f);
    //empty white box
    //Gradient line start
    Point2D.Float p3 = new Point2D.Float(hPad + ww, 0.f);
    //Gradient line start
    Point2D.Float p5 = new Point2D.Float(hPad + 2 * ww, 0.f);
    //Gradient line start
    Point2D.Float p7 = new Point2D.Float(hPad + 3 * ww, 0.f);
    //Gradient line end
    Point2D.Float p8 = new Point2D.Float(hPad + 4 * ww, 0.f);
    float w1 = 30;
    float w2 = 30;
    float hh = h / 2;
    // Need to create two gradients, one one for the max and one for the min
    //Acyclic gradient
    GradientPaint g1 = new GradientPaint(p1, minColor, p2, Color.WHITE, false);
    //Acyclic gradient
    GradientPaint g2 = new GradientPaint(p7, Color.WHITE, p8, maxColor, false);
    Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x, p1.y, w1, hh);
    Rectangle2D.Float rect2 = new Rectangle2D.Float(p3.x, p3.y, w2, hh);
    Rectangle2D.Float rect3 = new Rectangle2D.Float(p5.x, p5.y, w2, hh);
    Rectangle2D.Float rect4 = new Rectangle2D.Float(p7.x, p7.y, w1, hh);
    g2d.setFont(getLabelFont());
    // Text y offset
    float tyOffset = hh + h / 3.f;
    if (minColor != Color.WHITE) {
        g2d.setPaint(g1);
        g2d.fill(rect1);
        g2d.setPaint(Color.WHITE);
        // make a white block
        g2d.setPaint(Color.WHITE);
        g2d.fill(rect2);
        g2d.setPaint(getLabelForeground());
        g2d.drawString(phenotype1, p1.x, p1.y + tyOffset);
    } else {
        g2d.setPaint(getLabelForeground());
        g2d.drawString(phenotype1, p5.x, p5.y + tyOffset);
    }
    // Make a white block
    g2d.setPaint(Color.WHITE);
    g2d.fill(rect3);
    g2d.setPaint(g2);
    g2d.fill(rect4);
    // Border
    g2d.setPaint(UIManager.getColor("Separator.foreground"));
    g2d.draw(new Rectangle2D.Float(p1.x, p1.y, (2 * w1 + 2 * w2), hh));
    g2d.setPaint(getLabelForeground());
    g2d.drawString(phenotype2, p7.x, p7.y + tyOffset);
}
Also used : Point2D(java.awt.geom.Point2D) Rectangle2D(java.awt.geom.Rectangle2D) GradientPaint(java.awt.GradientPaint) GradientPaint(java.awt.GradientPaint) Graphics2D(java.awt.Graphics2D)

Aggregations

GradientPaint (java.awt.GradientPaint)55 Graphics2D (java.awt.Graphics2D)37 Color (java.awt.Color)33 BasicStroke (java.awt.BasicStroke)14 Font (java.awt.Font)13 Paint (java.awt.Paint)12 Rectangle2D (java.awt.geom.Rectangle2D)11 FontMetrics (java.awt.FontMetrics)9 BufferedImage (java.awt.image.BufferedImage)9 Rectangle (java.awt.Rectangle)8 RadialGradientPaint (java.awt.RadialGradientPaint)6 Stroke (java.awt.Stroke)6 RoundRectangle2D (java.awt.geom.RoundRectangle2D)6 Point2D (java.awt.geom.Point2D)5 LinearGradientPaint (java.awt.LinearGradientPaint)4 Shape (java.awt.Shape)4 Ellipse2D (java.awt.geom.Ellipse2D)4 RectangularShape (java.awt.geom.RectangularShape)4 Graphics (java.awt.Graphics)3 MultipleGradientPaint (java.awt.MultipleGradientPaint)3