Search in sources :

Example 6 with TexturePaint

use of java.awt.TexturePaint in project gephi by gephi.

the class GradientSliderUI method paintTrack.

@Override
protected void paintTrack(Graphics2D g) {
    Composite oldComposite = g.getComposite();
    float alpha = slider.isEnabled() ? 1 : .5f;
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    Shape frame = getFrame();
    boolean alwaysOpaque = getProperty(slider, "GradientSlider.showTranslucency", "true").equals("false");
    if (alwaysOpaque == false) {
        if (checkerPaint == null) {
            createCheckerPaint();
        }
        g.setPaint(checkerPaint);
        g.fill(frame);
    }
    TexturePaint tp = new TexturePaint(img, new Rectangle(trackRect.x, 0, img.getWidth(), 1));
    g.setPaint(tp);
    AffineTransform oldTransform = null;
    oldTransform = g.getTransform();
    AffineTransform transform = new AffineTransform();
    if (slider.getOrientation() == GradientSlider.VERTICAL) {
        if (slider.isInverted()) {
            transform.rotate(Math.PI / 2, trackRect.x, trackRect.y);
        } else {
            transform.rotate(-Math.PI / 2, trackRect.x, trackRect.y + trackRect.height);
        }
    } else if (slider.isInverted()) {
        //flip horizontal:
        double x1 = trackRect.x;
        double x2 = trackRect.x + trackRect.width;
        //m00*x1+m02 = x2
        //m00*x2+m02 = x1
        double m00 = (x2 - x1) / (x1 - x2);
        double m02 = x1 - m00 * x2;
        transform.setTransform(m00, 0, 0, 1, m02, 0);
    } else {
    //no transform necessary
    }
    g.transform(transform);
    try {
        g.fill(transform.createInverse().createTransformedShape(trackRect));
    } catch (NoninvertibleTransformException e) {
    //this won't happen; unless a width/height
    //is zero somewhere, in which case we have nothing to paint anyway.
    }
    if (oldTransform != null) {
        g.setTransform(oldTransform);
    }
    if (getProperty(slider, "GradientSlider.useBevel", "false").equals("true")) {
        PaintUtils.drawBevel(g, trackRect);
    } else {
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Shape oldClip = g.getClip();
        int first, last;
        Color[] colors = ((GradientSlider) slider).getColors();
        float[] f = slider.getThumbPositions();
        if ((slider.isInverted() == false && slider.getOrientation() == GradientSlider.HORIZONTAL) || (slider.isInverted() == true && slider.getOrientation() == GradientSlider.VERTICAL)) {
            first = 0;
            last = colors.length - 1;
            while (f[first] < 0) {
                first++;
            }
            while (f[last] > 1) {
                last--;
            }
        } else {
            last = 0;
            first = colors.length - 1;
            while (f[last] < 0) {
                last++;
            }
            while (f[first] > 1) {
                first--;
            }
        }
        if (slider.getOrientation() == GradientSlider.HORIZONTAL) {
            g.clip(frame);
            g.setColor(colors[first]);
            g.fillRect(0, 0, trackRect.x, slider.getHeight());
            g.setColor(colors[last]);
            g.fillRect(trackRect.x + trackRect.width, 0, slider.getWidth() - (trackRect.x + trackRect.width), slider.getHeight());
        } else {
            g.clip(frame);
            g.setColor(colors[first]);
            g.fillRect(0, 0, slider.getWidth(), trackRect.y);
            g.setColor(colors[last]);
            g.fillRect(0, trackRect.y + trackRect.height, slider.getWidth(), slider.getHeight() - (trackRect.y + trackRect.height));
        }
        g.setStroke(new BasicStroke(1));
        g.setClip(oldClip);
        g.setColor(new Color(0, 0, 0, 130));
        g.draw(frame);
        g.setColor(new Color(0, 0, 0, 130));
    }
    if (slider.isPaintTicks()) {
        paintTick(g, .25f, 2);
        paintTick(g, .5f, 2);
        paintTick(g, .75f, 2);
        paintTick(g, 0f, 2);
        paintTick(g, 1f, 2);
    }
    g.setComposite(oldComposite);
}
Also used : BasicStroke(java.awt.BasicStroke) Shape(java.awt.Shape) Composite(java.awt.Composite) AlphaComposite(java.awt.AlphaComposite) Color(java.awt.Color) Rectangle(java.awt.Rectangle) TexturePaint(java.awt.TexturePaint) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TexturePaint(java.awt.TexturePaint) AffineTransform(java.awt.geom.AffineTransform)

Example 7 with TexturePaint

use of java.awt.TexturePaint in project playn by threerings.

the class JavaImage method toSubPattern.

@Override
protected Pattern toSubPattern(AbstractImageGL<?> image, boolean repeatX, boolean repeatY, float x, float y, float width, float height) {
    assert isReady() : "Cannot generate a pattern from unready image.";
    // we have to account for the scale factor when extracting our subimage
    BufferedImage subImage = img.getSubimage(scale.scaledFloor(x), scale.scaledFloor(y), scale.scaledCeil(width), scale.scaledCeil(height));
    Rectangle2D rect = new Rectangle2D.Float(0, 0, width, height);
    return new JavaPattern(image, repeatX, repeatY, new TexturePaint(subImage, rect));
}
Also used : TexturePaint(java.awt.TexturePaint) Rectangle2D(java.awt.geom.Rectangle2D) BufferedImage(java.awt.image.BufferedImage)

Example 8 with TexturePaint

use of java.awt.TexturePaint 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 9 with TexturePaint

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

the class TeamMemberTimeCellRenderer method getGradient.

/** Construct a Paint that can be used to fade to the background color.
     */
protected Paint getGradient() {
    Color backgroundColor = getBackground();
    BufferedImage i = new BufferedImage(gradientWidth, 1, BufferedImage.TYPE_INT_ARGB);
    WritableRaster alpha = i.getAlphaRaster();
    for (int x = gradientWidth; x-- > 0; ) {
        i.setRGB(x, 0, backgroundColor.getRGB());
        double sample = gradientWidth - x;
        sample = sample / gradientWidth;
        sample = (1.0 - sample * sample) * 255;
        alpha.setSample(x, 0, 0, sample);
    }
    // to perform a quadratic fade instead of a linear fade.
    return new TexturePaint(i, new Rectangle(leftBorder, 0, gradientWidth, 1));
}
Also used : TexturePaint(java.awt.TexturePaint) WritableRaster(java.awt.image.WritableRaster) Color(java.awt.Color) Rectangle(java.awt.Rectangle) BufferedImage(java.awt.image.BufferedImage) TexturePaint(java.awt.TexturePaint) Paint(java.awt.Paint)

Aggregations

TexturePaint (java.awt.TexturePaint)9 BufferedImage (java.awt.image.BufferedImage)7 Color (java.awt.Color)4 Graphics2D (java.awt.Graphics2D)4 Paint (java.awt.Paint)4 Rectangle (java.awt.Rectangle)4 Rectangle2D (java.awt.geom.Rectangle2D)4 GradientPaint (java.awt.GradientPaint)3 LinearGradientPaint (java.awt.LinearGradientPaint)3 RadialGradientPaint (java.awt.RadialGradientPaint)3 MultipleGradientPaint (java.awt.MultipleGradientPaint)2 AlphaComposite (java.awt.AlphaComposite)1 BasicStroke (java.awt.BasicStroke)1 Composite (java.awt.Composite)1 Graphics (java.awt.Graphics)1 Shape (java.awt.Shape)1 AffineTransform (java.awt.geom.AffineTransform)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 WritableRaster (java.awt.image.WritableRaster)1 HwmfHatchStyle (org.apache.poi.hwmf.record.HwmfHatchStyle)1