Search in sources :

Example 91 with Graphics

use of java.awt.Graphics in project Voxel_Game by ASasseCreations.

the class AssetLoader method createAtlas.

public static final AtlasResult createAtlas() throws URISyntaxException, IOException {
    int textureSize = 0;
    for (final BufferedImage image : RawAtlasTile.MODIFIED_IMAGE.values()) if (image.getHeight() > textureSize)
        textureSize = image.getHeight();
    final int rows = getTextureAtlasSize();
    final int imageSize = rows * textureSize;
    final BufferedImage image = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_ARGB);
    {
        final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
        for (int i = 0; i < pixels.length; i++) pixels[i] = 0x00000000;
    }
    final List<String> keys = new ArrayList<>(RawAtlasTile.MODIFIED_IMAGE.keySet());
    final List<BufferedImage> images = new ArrayList<>(RawAtlasTile.MODIFIED_IMAGE.values());
    final Map<String, BlockTexture> textures = new HashMap<>();
    final Graphics g = image.getGraphics();
    for (int y = 0; y < rows; y++) for (int x = 0; x < rows; x++) {
        if (x + y * rows >= images.size())
            continue;
        g.drawImage(images.get(x + y * rows), x * textureSize, y * textureSize, textureSize, textureSize, null);
        textures.put(keys.get(x + y * rows), new BlockTexture(x, y));
    }
    g.dispose();
    final AtlasResult result = new AtlasResult();
    final TextureProperties properties = new TextureProperties();
    properties.maxLevel = (int) (Math.log(textureSize) / Math.log(2));
    properties.minFilter = GL11.GL_NEAREST_MIPMAP_LINEAR;
    properties.magFilter = GL11.GL_NEAREST;
    properties.mipmaps = true;
    result.image = TextureLoader.getTexture(image, properties);
    result.textures = textures;
    return result;
}
Also used : Graphics(java.awt.Graphics) TextureProperties(org.asassecreations.engine.texture.TextureProperties) HashMap(java.util.HashMap) BlockTexture(org.asassecreations.voxelgame.world.block.texture.BlockTexture) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage)

Example 92 with Graphics

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

the class ImageUtils method assertImageSimilar.

public static void assertImageSimilar(String relativePath, BufferedImage goldenImage, BufferedImage image, double maxPercentDifferent) throws IOException {
    assertEquals("Only TYPE_INT_ARGB image types are supported", TYPE_INT_ARGB, image.getType());
    if (goldenImage.getType() != TYPE_INT_ARGB) {
        BufferedImage temp = new BufferedImage(goldenImage.getWidth(), goldenImage.getHeight(), TYPE_INT_ARGB);
        temp.getGraphics().drawImage(goldenImage, 0, 0, null);
        goldenImage = temp;
    }
    assertEquals(TYPE_INT_ARGB, goldenImage.getType());
    int imageWidth = Math.min(goldenImage.getWidth(), image.getWidth());
    int imageHeight = Math.min(goldenImage.getHeight(), image.getHeight());
    // Blur the images to account for the scenarios where there are pixel
    // differences
    // in where a sharp edge occurs
    // goldenImage = blur(goldenImage, 6);
    // image = blur(image, 6);
    int width = 3 * imageWidth;
    @SuppressWarnings("UnnecessaryLocalVariable") int // makes code more readable
    height = imageHeight;
    BufferedImage deltaImage = new BufferedImage(width, height, TYPE_INT_ARGB);
    Graphics g = deltaImage.getGraphics();
    // Compute delta map
    long delta = 0;
    for (int y = 0; y < imageHeight; y++) {
        for (int x = 0; x < imageWidth; x++) {
            int goldenRgb = goldenImage.getRGB(x, y);
            int rgb = image.getRGB(x, y);
            if (goldenRgb == rgb) {
                deltaImage.setRGB(imageWidth + x, y, 0x00808080);
                continue;
            }
            // If the pixels have no opacity, don't delta colors at all
            if (((goldenRgb & 0xFF000000) == 0) && (rgb & 0xFF000000) == 0) {
                deltaImage.setRGB(imageWidth + x, y, 0x00808080);
                continue;
            }
            int deltaR = ((rgb & 0xFF0000) >>> 16) - ((goldenRgb & 0xFF0000) >>> 16);
            int newR = 128 + deltaR & 0xFF;
            int deltaG = ((rgb & 0x00FF00) >>> 8) - ((goldenRgb & 0x00FF00) >>> 8);
            int newG = 128 + deltaG & 0xFF;
            int deltaB = (rgb & 0x0000FF) - (goldenRgb & 0x0000FF);
            int newB = 128 + deltaB & 0xFF;
            int avgAlpha = ((((goldenRgb & 0xFF000000) >>> 24) + ((rgb & 0xFF000000) >>> 24)) / 2) << 24;
            int newRGB = avgAlpha | newR << 16 | newG << 8 | newB;
            deltaImage.setRGB(imageWidth + x, y, newRGB);
            delta += Math.abs(deltaR);
            delta += Math.abs(deltaG);
            delta += Math.abs(deltaB);
        }
    }
    // 3 different colors, 256 color levels
    long total = imageHeight * imageWidth * 3L * 256L;
    float percentDifference = (float) (delta * 100 / (double) total);
    String error = null;
    String imageName = getName(relativePath);
    if (percentDifference > maxPercentDifferent) {
        error = String.format("Images differ (by %.1f%%)", percentDifference);
    } else if (Math.abs(goldenImage.getWidth() - image.getWidth()) >= 2) {
        error = "Widths differ too much for " + imageName + ": " + goldenImage.getWidth() + "x" + goldenImage.getHeight() + "vs" + image.getWidth() + "x" + image.getHeight();
    } else if (Math.abs(goldenImage.getHeight() - image.getHeight()) >= 2) {
        error = "Heights differ too much for " + imageName + ": " + goldenImage.getWidth() + "x" + goldenImage.getHeight() + "vs" + image.getWidth() + "x" + image.getHeight();
    }
    assertEquals(TYPE_INT_ARGB, image.getType());
    if (error != null) {
        // Expected on the left
        // Golden on the right
        g.drawImage(goldenImage, 0, 0, null);
        g.drawImage(image, 2 * imageWidth, 0, null);
        // Labels
        if (imageWidth > 80) {
            g.setColor(Color.RED);
            g.drawString("Expected", 10, 20);
            g.drawString("Actual", 2 * imageWidth + 10, 20);
        }
        File output = new File(getTempDir(), "delta-" + imageName);
        if (output.exists()) {
            boolean deleted = output.delete();
            assertTrue(deleted);
        }
        ImageIO.write(deltaImage, "PNG", output);
        error += " - see details in " + output.getPath() + "\n";
        error = saveImageAndAppendMessage(image, error, relativePath);
        System.out.println(error);
        fail(error);
    }
    g.dispose();
}
Also used : Graphics(java.awt.Graphics) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 93 with Graphics

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

the class CompositionArea method setText.

/**
     * Sets the text and caret to be displayed in this composition area.
     * Shows the window if it contains text, hides it if not.
     */
void setText(AttributedCharacterIterator composedText, TextHitInfo caret) {
    composedTextLayout = null;
    if (composedText == null) {
        // there's no composed text to display, so hide the window
        compositionWindow.setVisible(false);
        this.caret = null;
    } else {
        /* since we have composed text, make sure the window is shown.
               This is necessary to get a valid graphics object. See 6181385.
            */
        if (!compositionWindow.isVisible()) {
            compositionWindow.setVisible(true);
        }
        Graphics g = getGraphics();
        if (g == null) {
            return;
        }
        try {
            updateWindowLocation();
            FontRenderContext context = ((Graphics2D) g).getFontRenderContext();
            composedTextLayout = new TextLayout(composedText, context);
            Rectangle2D bounds = composedTextLayout.getBounds();
            this.caret = caret;
            // Resize the composition area to just fit the text.
            FontMetrics metrics = g.getFontMetrics();
            Rectangle2D maxCharBoundsRec = metrics.getMaxCharBounds(g);
            int newHeight = (int) maxCharBoundsRec.getHeight() + HEIGHT_MARGIN;
            int newFrameHeight = newHeight + compositionWindow.getInsets().top + compositionWindow.getInsets().bottom;
            // If it's a passive client, set the width always to PASSIVE_WIDTH (480px)
            InputMethodRequests req = handler.getClientInputMethodRequests();
            int newWidth = (req == null) ? PASSIVE_WIDTH : (int) bounds.getWidth() + WIDTH_MARGIN;
            int newFrameWidth = newWidth + compositionWindow.getInsets().left + compositionWindow.getInsets().right;
            setPreferredSize(new Dimension(newWidth, newHeight));
            compositionWindow.setSize(new Dimension(newFrameWidth, newFrameHeight));
            // show the composed text
            paint(g);
        } finally {
            g.dispose();
        }
    }
}
Also used : Graphics(java.awt.Graphics) FontMetrics(java.awt.FontMetrics) InputMethodRequests(java.awt.im.InputMethodRequests) Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) Dimension(java.awt.Dimension) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Example 94 with Graphics

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

the class CGLGraphicsConfig method flip.

@Override
public void flip(final LWComponentPeer<?, ?> peer, final Image backBuffer, final int x1, final int y1, final int x2, final int y2, final BufferCapabilities.FlipContents flipAction) {
    final Graphics g = peer.getGraphics();
    try {
        g.drawImage(backBuffer, x1, y1, x2, y2, x1, y1, x2, y2, null);
    } finally {
        g.dispose();
    }
    if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
        final Graphics2D bg = (Graphics2D) backBuffer.getGraphics();
        try {
            bg.setBackground(peer.getBackground());
            bg.clearRect(0, 0, backBuffer.getWidth(null), backBuffer.getHeight(null));
        } finally {
            bg.dispose();
        }
    }
}
Also used : Graphics(java.awt.Graphics) Graphics2D(java.awt.Graphics2D)

Example 95 with Graphics

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

the class VolatileSurfaceManager method initContents.

/**
     * Set contents of the current SurfaceData to default state (i.e. clear
     * the background).
     */
public void initContents() {
    // because we do not create a backup surface for them
    if (sdCurrent != null) {
        Graphics g = vImg.createGraphics();
        g.clearRect(0, 0, vImg.getWidth(), vImg.getHeight());
        g.dispose();
    }
}
Also used : Graphics(java.awt.Graphics)

Aggregations

Graphics (java.awt.Graphics)158 BufferedImage (java.awt.image.BufferedImage)91 Graphics2D (java.awt.Graphics2D)22 Dimension (java.awt.Dimension)20 Color (java.awt.Color)19 File (java.io.File)18 Frame (java.awt.Frame)14 JFrame (javax.swing.JFrame)13 Point (java.awt.Point)12 Rectangle (java.awt.Rectangle)12 Component (java.awt.Component)11 IOException (java.io.IOException)11 JComponent (javax.swing.JComponent)9 FontMetrics (java.awt.FontMetrics)8 GraphicsConfiguration (java.awt.GraphicsConfiguration)8 Insets (java.awt.Insets)8 Panel (java.awt.Panel)8 ImageIcon (javax.swing.ImageIcon)8 GraphicsDevice (java.awt.GraphicsDevice)6 Window (java.awt.Window)6