Search in sources :

Example 6 with Graphics

use of java.awt.Graphics in project processing by processing.

the class EditorStatus method paint.

//public void paintComponent(Graphics screen) {
public void paint(Graphics screen) {
    //    if (okButton == null) setup();
    Dimension size = getSize();
    if ((size.width != sizeW) || (size.height != sizeH)) {
        // component has been resized
        offscreen = null;
    }
    if (offscreen == null) {
        sizeW = size.width;
        sizeH = size.height;
        offscreen = Toolkit.offscreenGraphics(this, sizeW, sizeH);
    }
    Graphics g = offscreen.getGraphics();
    /*Graphics2D g2 =*/
    Toolkit.prepareGraphics(g);
    g.setFont(font);
    if (metrics == null) {
        metrics = g.getFontMetrics();
        ascent = metrics.getAscent();
    }
    g.drawImage(bgImage[mode], 0, 0, sizeW, sizeH, this);
    g.setColor(fgColor[mode]);
    // https://github.com/processing/processing/issues/3265
    if (message != null) {
        // needs to be set each time on osx
        g.setFont(font);
        // calculate right edge of the text for rollovers (otherwise the pane
        // cannot be resized up or down whenever a URL is being displayed)
        rightEdge = LEFT_MARGIN + g.getFontMetrics().stringWidth(message);
        // set the highlight color on rollover so that the user's not surprised
        // to see the web browser open when they click
        urlRollover = (url != null) && (mouseX > LEFT_MARGIN && mouseX < rightEdge);
        if (urlRollover) {
            g.setColor(urlColor);
        }
        g.drawString(message, LEFT_MARGIN, (sizeH + ascent) / 2);
    }
    if (indeterminate) {
        //int x = cancelButton.getX();
        //int w = cancelButton.getWidth();
        int w = Toolkit.getButtonWidth();
        int x = getWidth() - RIGHT_MARGIN - w;
        int y = getHeight() / 3;
        int h = getHeight() / 3;
        g.setColor(new Color(0x80000000, true));
        g.drawRect(x, y, w, h);
        for (int i = 0; i < 10; i++) {
            int r = (int) (x + Math.random() * w);
            g.drawLine(r, y, r, y + h);
        }
    }
    screen.drawImage(offscreen, 0, 0, sizeW, sizeH, null);
}
Also used : Graphics(java.awt.Graphics) Color(java.awt.Color) Dimension(java.awt.Dimension)

Example 7 with Graphics

use of java.awt.Graphics in project processing by processing.

the class ColorChooser method createColorFields.

protected Container createColorFields(String buttonName, ActionListener buttonListener) {
    Box box = Box.createVerticalBox();
    box.setAlignmentY(0);
    final int GAP = Platform.isWindows() ? 5 : 0;
    //10;
    final int BETWEEN = Platform.isWindows() ? 8 : 6;
    Box row;
    row = Box.createHorizontalBox();
    if (Platform.isMacOS()) {
        row.add(Box.createHorizontalStrut(17));
    } else {
        row.add(createFixedLabel(""));
    }
    // Can't just set the bg color of the panel because it also tints the bevel
    // (on OS X), which looks odd. So instead we override paintComponent().
    colorPanel = new JPanel() {

        public void paintComponent(Graphics g) {
            g.setColor(new Color(red, green, blue));
            Dimension size = getSize();
            g.fillRect(0, 0, size.width, size.height);
        }
    };
    colorPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    Dimension dim = new Dimension(70, 25);
    colorPanel.setMinimumSize(dim);
    colorPanel.setMaximumSize(dim);
    colorPanel.setPreferredSize(dim);
    row.add(colorPanel);
    row.add(Box.createHorizontalGlue());
    box.add(row);
    box.add(Box.createVerticalStrut(BETWEEN));
    //    if (Base.isMacOS()) {  // need a little extra
    //      box.add(Box.createVerticalStrut(BETWEEN));
    //    }
    row = Box.createHorizontalBox();
    row.add(createFixedLabel("H"));
    row.add(hueField = new NumberField(4, false));
    // degree symbol
    row.add(new JLabel(" °"));
    row.add(Box.createHorizontalGlue());
    box.add(row);
    box.add(Box.createVerticalStrut(GAP));
    row = Box.createHorizontalBox();
    row.add(createFixedLabel("S"));
    row.add(saturationField = new NumberField(4, false));
    row.add(new JLabel(" %"));
    row.add(Box.createHorizontalGlue());
    box.add(row);
    box.add(Box.createVerticalStrut(GAP));
    row = Box.createHorizontalBox();
    row.add(createFixedLabel("B"));
    row.add(brightnessField = new NumberField(4, false));
    row.add(new JLabel(" %"));
    row.add(Box.createHorizontalGlue());
    box.add(row);
    box.add(Box.createVerticalStrut(BETWEEN));
    //
    row = Box.createHorizontalBox();
    row.add(createFixedLabel("R"));
    row.add(redField = new NumberField(4, false));
    row.add(Box.createHorizontalGlue());
    box.add(row);
    box.add(Box.createVerticalStrut(GAP));
    row = Box.createHorizontalBox();
    row.add(createFixedLabel("G"));
    row.add(greenField = new NumberField(4, false));
    row.add(Box.createHorizontalGlue());
    box.add(row);
    box.add(Box.createVerticalStrut(GAP));
    row = Box.createHorizontalBox();
    row.add(createFixedLabel("B"));
    row.add(blueField = new NumberField(4, false));
    row.add(Box.createHorizontalGlue());
    box.add(row);
    box.add(Box.createVerticalStrut(BETWEEN));
    //
    row = Box.createHorizontalBox();
    row.add(createFixedLabel(""));
    // Windows needs extra space, OS X and Linux do not
    // Mac OS X needs 6 because #CCCCCC is quite wide
    final int hexCount = Platform.isWindows() ? 7 : 6;
    row.add(hexField = new NumberField(hexCount, true));
    row.add(Box.createHorizontalGlue());
    box.add(row);
    box.add(Box.createVerticalStrut(GAP));
    //
    //    // Not great, because the insets make things weird anyway
    //    //Dimension dim = new Dimension(hexField.getPreferredSize());
    //    Dimension dim = new Dimension(70, 20);
    //    colorPanel.setMinimumSize(dim);
    //    colorPanel.setMaximumSize(dim);
    //    colorPanel.setPreferredSize(dim);
    ////    colorPanel.setBorder(new EmptyBorder(hexField.getInsets()));
    //
    row = Box.createHorizontalBox();
    if (Platform.isMacOS()) {
        row.add(Box.createHorizontalStrut(11));
    } else {
        row.add(createFixedLabel(""));
    }
    JButton button = new JButton(buttonName);
    button.addActionListener(buttonListener);
    //System.out.println("button: " + button.getInsets());
    row.add(button);
    row.add(Box.createHorizontalGlue());
    box.add(row);
    row = Box.createHorizontalBox();
    if (Platform.isMacOS()) {
        row.add(Box.createHorizontalStrut(11));
    } else {
        row.add(createFixedLabel(""));
    }
    button = new JButton(Language.text("prompt.cancel"));
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            ColorChooser.this.hide();
        }
    });
    row.add(button);
    row.add(Box.createHorizontalGlue());
    box.add(row);
    //
    box.add(Box.createVerticalGlue());
    return box;
}
Also used : Graphics(java.awt.Graphics) Color(java.awt.Color) Dimension(java.awt.Dimension)

Example 8 with Graphics

use of java.awt.Graphics in project hid-serial by rayshobby.

the class StyledString method getParagraghSpacer.

/**
	 * Create a graphic image character to simulate paragraph breaks
	 * 
	 * @param ww
	 * @return a blank image to manage paragraph ends.
	 */
private ImageGraphicAttribute getParagraghSpacer(int ww) {
    if (ww == Integer.MAX_VALUE)
        ww = 1;
    BufferedImage img = new BufferedImage(ww, 10, BufferedImage.TYPE_INT_ARGB);
    Graphics g = img.getGraphics();
    g.setColor(new Color(255, 255, 255, 0));
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    return new ImageGraphicAttribute(img, GraphicAttribute.TOP_ALIGNMENT);
}
Also used : Graphics(java.awt.Graphics) ImageGraphicAttribute(java.awt.font.ImageGraphicAttribute) Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage)

Example 9 with Graphics

use of java.awt.Graphics in project platform_frameworks_base by android.

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 10 with Graphics

use of java.awt.Graphics in project libgdx by libgdx.

the class TiledMapPacker method packTilesets.

/** Traverse the specified tilesets, optionally lookup the used ids and pass every tile image to the {@link TexturePacker},
	 * optionally ignoring unused tile ids */
private void packTilesets(FileHandle inputDirHandle, Settings texturePackerSettings) throws IOException {
    BufferedImage tile;
    Vector2 tileLocation;
    Graphics g;
    packer = new TexturePacker(texturePackerSettings);
    for (TiledMapTileSet set : tilesetsToPack.values()) {
        String tilesetName = set.getName();
        System.out.println("Processing tileset " + tilesetName);
        IntArray usedIds = this.settings.stripUnusedTiles ? getUsedIdsBucket(tilesetName, -1) : null;
        int tileWidth = set.getProperties().get("tilewidth", Integer.class);
        int tileHeight = set.getProperties().get("tileheight", Integer.class);
        int firstgid = set.getProperties().get("firstgid", Integer.class);
        String imageName = set.getProperties().get("imagesource", String.class);
        TileSetLayout layout = new TileSetLayout(firstgid, set, inputDirHandle);
        for (int gid = layout.firstgid, i = 0; i < layout.numTiles; gid++, i++) {
            boolean verbose = this.settings.verbose;
            if (usedIds != null && !usedIds.contains(gid)) {
                if (verbose) {
                    System.out.println("Stripped id #" + gid + " from tileset \"" + tilesetName + "\"");
                }
                continue;
            }
            tileLocation = layout.getLocation(gid);
            tile = new BufferedImage(tileWidth, tileHeight, BufferedImage.TYPE_4BYTE_ABGR);
            g = tile.createGraphics();
            g.drawImage(layout.image, 0, 0, tileWidth, tileHeight, (int) tileLocation.x, (int) tileLocation.y, (int) tileLocation.x + tileWidth, (int) tileLocation.y + tileHeight, null);
            if (verbose) {
                System.out.println("Adding " + tileWidth + "x" + tileHeight + " (" + (int) tileLocation.x + ", " + (int) tileLocation.y + ")");
            }
            // AtlasTmxMapLoader expects every tileset's index to begin at zero for the first tile in every tileset.
            // so the region's adjusted gid is (gid - layout.firstgid). firstgid will be added back in AtlasTmxMapLoader on load
            int adjustedGid = gid - layout.firstgid;
            final String separator = "_";
            String regionName = tilesetName + separator + adjustedGid;
            packer.addImage(tile, regionName);
        }
    }
    String tilesetOutputDir = outputDir.toString() + "/" + this.settings.tilesetOutputDirectory;
    File relativeTilesetOutputDir = new File(tilesetOutputDir);
    File outputDirTilesets = new File(relativeTilesetOutputDir.getCanonicalPath());
    outputDirTilesets.mkdirs();
    packer.pack(outputDirTilesets, this.settings.atlasOutputName + ".atlas");
}
Also used : Graphics(java.awt.Graphics) IntArray(com.badlogic.gdx.utils.IntArray) Vector2(com.badlogic.gdx.math.Vector2) TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) TexturePacker(com.badlogic.gdx.tools.texturepacker.TexturePacker) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

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