Search in sources :

Example 61 with Color

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

the class RadialGradientPrintingTest method doPaint.

public void doPaint(Graphics2D g2d) {
    g2d.translate(DIM * 0.2, DIM * 0.2);
    Shape s = new Rectangle2D.Float(0, 0, DIM * 2, DIM * 2);
    // RadialGradientPaint
    Point2D centre = new Point2D.Float(DIM / 2.0f, DIM / 2.0f);
    float radius = DIM / 2.0f;
    Point2D focus = new Point2D.Float(DIM / 3.0f, DIM / 3.0f);
    float[] stops = { 0.0f, 1.0f };
    Color[] colors = { Color.red, Color.white };
    RadialGradientPaint rgp = new RadialGradientPaint(centre, radius, focus, stops, colors, RadialGradientPaint.CycleMethod.NO_CYCLE);
    g2d.setPaint(rgp);
    g2d.fill(s);
    g2d.translate(DIM * 2.2, 0);
    Color[] colors1 = { Color.red, Color.blue, Color.green };
    float[] stops1 = { 0.0f, 0.5f, 1.0f };
    RadialGradientPaint rgp1 = new RadialGradientPaint(centre, radius, focus, stops1, colors1, RadialGradientPaint.CycleMethod.REFLECT);
    g2d.setPaint(rgp1);
    g2d.fill(s);
    g2d.translate(-DIM * 2.2, DIM * 2.2);
    Color[] colors2 = { Color.red, Color.blue, Color.green, Color.white };
    float[] stops2 = { 0.0f, 0.3f, 0.6f, 1.0f };
    RadialGradientPaint rgp2 = new RadialGradientPaint(centre, radius, focus, stops2, colors2, RadialGradientPaint.CycleMethod.REPEAT);
    g2d.setPaint(rgp2);
    g2d.fill(s);
}
Also used : Shape(java.awt.Shape) Point2D(java.awt.geom.Point2D) Color(java.awt.Color) RadialGradientPaint(java.awt.RadialGradientPaint)

Example 62 with Color

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

the class IndexingTest method createComponentImage.

protected static BufferedImage createComponentImage(int w, int h, ComponentColorModel cm) {
    WritableRaster wr = cm.createCompatibleWritableRaster(w, h);
    BufferedImage img = new BufferedImage(cm, wr, false, null);
    Graphics2D g = img.createGraphics();
    int width = w / 8;
    Color[] colors = new Color[8];
    colors[0] = Color.red;
    colors[1] = Color.green;
    colors[2] = Color.blue;
    colors[3] = Color.white;
    colors[4] = Color.black;
    colors[5] = new Color(0x80, 0x80, 0x80, 0x00);
    colors[6] = Color.yellow;
    colors[7] = Color.cyan;
    for (int i = 0; i < 8; i++) {
        g.setColor(colors[i]);
        g.fillRect(i * width, 0, width, h);
    }
    return img;
}
Also used : WritableRaster(java.awt.image.WritableRaster) Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 63 with Color

use of java.awt.Color in project android_frameworks_base by DirtyUnicorns.

the class ImageUtils method scale.

/**
     * Resize the given image
     *
     * @param source the image to be scaled
     * @param xScale x scale
     * @param yScale y scale
     * @return the scaled image
     */
@NonNull
public static BufferedImage scale(@NonNull BufferedImage source, double xScale, double yScale) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();
    int destWidth = Math.max(1, (int) (xScale * sourceWidth));
    int destHeight = Math.max(1, (int) (yScale * sourceHeight));
    int imageType = source.getType();
    if (imageType == BufferedImage.TYPE_CUSTOM) {
        imageType = BufferedImage.TYPE_INT_ARGB;
    }
    if (xScale > 0.5 && yScale > 0.5) {
        BufferedImage scaled = new BufferedImage(destWidth, destHeight, imageType);
        Graphics2D g2 = scaled.createGraphics();
        g2.setComposite(AlphaComposite.Src);
        g2.setColor(new Color(0, true));
        g2.fillRect(0, 0, destWidth, destHeight);
        if (xScale == 1 && yScale == 1) {
            g2.drawImage(source, 0, 0, null);
        } else {
            setRenderingHints(g2);
            g2.drawImage(source, 0, 0, destWidth, destHeight, 0, 0, sourceWidth, sourceHeight, null);
        }
        g2.dispose();
        return scaled;
    } else {
        // When creating a thumbnail, using the above code doesn't work very well;
        // you get some visible artifacts, especially for text. Instead use the
        // technique of repeatedly scaling the image into half; this will cause
        // proper averaging of neighboring pixels, and will typically (for the kinds
        // of screen sizes used by this utility method in the layout editor) take
        // about 3-4 iterations to get the result since we are logarithmically reducing
        // the size. Besides, each successive pass in operating on much fewer pixels
        // (a reduction of 4 in each pass).
        //
        // However, we may not be resizing to a size that can be reached exactly by
        // successively diving in half. Therefore, once we're within a factor of 2 of
        // the final size, we can do a resize to the exact target size.
        // However, we can get even better results if we perform this final resize
        // up front. Let's say we're going from width 1000 to a destination width of 85.
        // The first approach would cause a resize from 1000 to 500 to 250 to 125, and
        // then a resize from 125 to 85. That last resize can distort/blur a lot.
        // Instead, we can start with the destination width, 85, and double it
        // successfully until we're close to the initial size: 85, then 170,
        // then 340, and finally 680. (The next one, 1360, is larger than 1000).
        // So, now we *start* the thumbnail operation by resizing from width 1000 to
        // width 680, which will preserve a lot of visual details such as text.
        // Then we can successively resize the image in half, 680 to 340 to 170 to 85.
        // We end up with the expected final size, but we've been doing an exact
        // divide-in-half resizing operation at the end so there is less distortion.
        // Number of halving operations to perform after the initial resize
        int iterations = 0;
        // Width closest to source width that = 2^x, x is integer
        int nearestWidth = destWidth;
        int nearestHeight = destHeight;
        while (nearestWidth < sourceWidth / 2) {
            nearestWidth *= 2;
            nearestHeight *= 2;
            iterations++;
        }
        BufferedImage scaled = new BufferedImage(nearestWidth, nearestHeight, imageType);
        Graphics2D g2 = scaled.createGraphics();
        setRenderingHints(g2);
        g2.drawImage(source, 0, 0, nearestWidth, nearestHeight, 0, 0, sourceWidth, sourceHeight, null);
        g2.dispose();
        sourceWidth = nearestWidth;
        sourceHeight = nearestHeight;
        source = scaled;
        for (int iteration = iterations - 1; iteration >= 0; iteration--) {
            int halfWidth = sourceWidth / 2;
            int halfHeight = sourceHeight / 2;
            scaled = new BufferedImage(halfWidth, halfHeight, imageType);
            g2 = scaled.createGraphics();
            setRenderingHints(g2);
            g2.drawImage(source, 0, 0, halfWidth, halfHeight, 0, 0, sourceWidth, sourceHeight, null);
            g2.dispose();
            sourceWidth = halfWidth;
            sourceHeight = halfHeight;
            source = scaled;
            iterations--;
        }
        return scaled;
    }
}
Also used : Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) NonNull(android.annotation.NonNull)

Example 64 with Color

use of java.awt.Color in project android_frameworks_base by DirtyUnicorns.

the class GcSnapshot method setShader.

private boolean setShader(Graphics2D g, Paint_Delegate paint) {
    Shader_Delegate shaderDelegate = paint.getShader();
    if (shaderDelegate != null) {
        if (shaderDelegate.isSupported()) {
            java.awt.Paint shaderPaint = shaderDelegate.getJavaPaint();
            assert shaderPaint != null;
            if (shaderPaint != null) {
                g.setPaint(shaderPaint);
                return true;
            }
        } else {
            Bridge.getLog().fidelityWarning(LayoutLog.TAG_SHADER, shaderDelegate.getSupportMessage(), null, /*throwable*/
            null);
        }
    }
    // if no shader, use the paint color
    g.setColor(new Color(paint.getColor(), true));
    return false;
}
Also used : Shader_Delegate(android.graphics.Shader_Delegate) Color(java.awt.Color)

Example 65 with Color

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

the class MultiResolutionSplashTest method testSplash.

static void testSplash(ImageInfo test) throws Exception {
    SplashScreen splashScreen = SplashScreen.getSplashScreen();
    if (splashScreen == null) {
        throw new RuntimeException("Splash screen is not shown!");
    }
    Graphics2D g = splashScreen.createGraphics();
    Rectangle splashBounds = splashScreen.getBounds();
    int screenX = (int) splashBounds.getCenterX();
    int screenY = (int) splashBounds.getCenterY();
    if (splashBounds.width != IMAGE_WIDTH) {
        throw new RuntimeException("SplashScreen#getBounds has wrong width");
    }
    if (splashBounds.height != IMAGE_HEIGHT) {
        throw new RuntimeException("SplashScreen#getBounds has wrong height");
    }
    Robot robot = new Robot();
    Color splashScreenColor = robot.getPixelColor(screenX, screenY);
    float scaleFactor = getScaleFactor();
    Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
    if (!compare(testColor, splashScreenColor)) {
        throw new RuntimeException("Image with wrong resolution is used for splash screen!");
    }
}
Also used : Color(java.awt.Color) Rectangle(java.awt.Rectangle) SplashScreen(java.awt.SplashScreen) Robot(java.awt.Robot) Graphics2D(java.awt.Graphics2D) SunGraphics2D(sun.java2d.SunGraphics2D)

Aggregations

Color (java.awt.Color)2973 Graphics2D (java.awt.Graphics2D)396 Font (java.awt.Font)250 BufferedImage (java.awt.image.BufferedImage)236 Dimension (java.awt.Dimension)193 JPanel (javax.swing.JPanel)193 Point (java.awt.Point)188 BasicStroke (java.awt.BasicStroke)177 ArrayList (java.util.ArrayList)173 Test (org.junit.Test)156 JLabel (javax.swing.JLabel)148 Paint (java.awt.Paint)140 Rectangle (java.awt.Rectangle)109 GradientPaint (java.awt.GradientPaint)108 Insets (java.awt.Insets)103 GridBagConstraints (java.awt.GridBagConstraints)96 IOException (java.io.IOException)96 Stroke (java.awt.Stroke)94 JScrollPane (javax.swing.JScrollPane)93 File (java.io.File)89