Example usage for java.awt Graphics2D fillRect

List of usage examples for java.awt Graphics2D fillRect

Introduction

In this page you can find the example usage for java.awt Graphics2D fillRect.

Prototype

public abstract void fillRect(int x, int y, int width, int height);

Source Link

Document

Fills the specified rectangle.

Usage

From source file:Main.java

/**
 * Creates a shadow mask/*w  w  w.  j a  va2 s  .c  om*/
 * @param image
 * @param shadowColor
 * @param shadowOpacity
 * @return
 */
private static BufferedImage createShadowMask(BufferedImage image, Color shadowColor, float shadowOpacity) {
    BufferedImage mask = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = mask.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, shadowOpacity));
    g2d.setColor(shadowColor);
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
    g2d.dispose();

    return mask;
}

From source file:org.mabb.fontverter.opentype.DebugGlyphDrawer.java

public static void drawGlyph(TtfGlyph glyph) throws IOException {
    BufferedImage image = new BufferedImage(650, 650, BufferedImage.TYPE_INT_RGB);
    Graphics2D gfx = image.createGraphics();

    gfx.translate(0, 300);/*from   w  w  w.  j  ava2 s.co  m*/
    gfx.scale(1, -1);

    gfx.setColor(Color.white);
    gfx.fillRect(0, -1000, 2060, 2060);
    gfx.setColor(Color.lightGray);
    gfx.translate(100, 50);
    gfx.fillRect(0, 0, 1000, 1000);
    gfx.setColor(Color.BLACK);

    gfx.scale(.05, .05);

    //        gfx.rotate(Math.toRadians(180));
    //        gfx.translate(-2200, -2200);

    Color[] colors = new Color[] { Color.BLACK, Color.MAGENTA, Color.GREEN, Color.BLUE, Color.cyan };
    java.util.List<Path2D.Double> paths = glyph.getPaths();
    for (int i = 0; i < paths.size(); i++) {
        Path2D pathOn = paths.get(i);
        gfx.setColor(colors[i]);

        gfx.draw(pathOn);
    }

    gfx.dispose();

    //        ImageIO.write(image, "jpg", new File("test.jpg"));
}

From source file:script.imglib.analysis.ChartUtils.java

public static final Image<RGBALegacyType> asImage(final JFreeChart chart, int width, int height) {
    ChartPanel panel = new ChartPanel(chart);
    Dimension d = panel.getPreferredSize();
    if (-1 == width && -1 == height) {
        width = d.width;//from   w ww  .  j a v a 2 s .c  o m
        height = d.height;
        panel.setSize(d);
    } else {
        panel.setSize(width, height);
    }
    layoutComponent(panel);
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bi.createGraphics();
    if (!panel.isOpaque()) {
        g.setColor(panel.getBackground());
        g.fillRect(0, 0, width, height);
    }
    panel.paint(g);
    int[] pixels = new int[width * height];
    PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
    }
    g.dispose();

    Array<RGBALegacyType, IntAccess> a = new Array<RGBALegacyType, IntAccess>(new ArrayContainerFactory(),
            new IntArray(pixels), new int[] { width, height }, 1);
    // create a Type that is linked to the container
    final RGBALegacyType linkedType = new RGBALegacyType(a);
    // pass it to the DirectAccessContainer
    a.setLinkedType(linkedType);

    return new Image<RGBALegacyType>(a, new RGBALegacyType());
}

From source file:net.imglib2.script.analysis.ChartUtils.java

public static final Img<ARGBType> asImage(final JFreeChart chart, int width, int height) {
    final ChartPanel panel = new ChartPanel(chart);
    final Dimension d = panel.getPreferredSize();
    if (-1 == width && -1 == height) {
        width = d.width;//from ww w .  j av a 2s.c om
        height = d.height;
        panel.setSize(d);
    } else {
        panel.setSize(width, height);
    }
    layoutComponent(panel);
    final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = bi.createGraphics();
    if (!panel.isOpaque()) {
        g.setColor(panel.getBackground());
        g.fillRect(0, 0, width, height);
    }
    panel.paint(g);
    final int[] pixels = new int[width * height];
    final PixelGrabber pg = new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width);
    try {
        pg.grabPixels();
    } catch (final InterruptedException e) {
    }
    g.dispose();

    final ArrayImg<ARGBType, IntArray> a = new ArrayImg<ARGBType, IntArray>(new IntArray(pixels),
            new long[] { width, height }, 1);

    // create a Type that is linked to the container
    final ARGBType linkedType = new ARGBType(a);
    // pass it to the DirectAccessContainer
    a.setLinkedType(linkedType);

    return a;
}

From source file:nl.ctmm.trait.proteomics.qcviewer.utils.Utilities.java

/**
 * Scale the supplied image to the specified width and height. The scale type is either {@link Utilities#SCALE_FIT}
 * to make the scaled image fit within the width by height rectangle or {@link Utilities#SCALE_FILL} to make the
 * scaled image fill the entire rectangle (and possibly go outside it in one dimension).
 *
 * @param image     the image to be scaled.
 * @param scaleType {@link Utilities#SCALE_FIT} or {@link Utilities#SCALE_FILL}.
 * @param width     the preferred width.
 * @param height    the preferred height.
 * @return the scaled image./*from   ww w  .j  a v a  2 s  . c  o  m*/
 */
public static BufferedImage scaleImage(final BufferedImage image, final int scaleType, final int width,
        final int height) {
    logger.fine("scaleImage: width: " + width + " height: " + height);

    /* TODO: can we do the scaling once and save the images of the right size? [Freek]
     * This is a good idea. [Pravin]  
     * 
     * TODO: are there classes in the standard Java libraries or third party libraries that do this scaling? [Freek]
     * return image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
     * This article describes use of Greaphics2D.drawImage() [Pravin]
     * http://www.mkyong.com/java/how-to-resize-an-image-in-java/
     * imgscalr is Java image scaling library available under Apache 2 License. 
     * http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/
     */
    final BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics2D graphics2D = scaledImage.createGraphics();
    graphics2D.setColor(Color.white);
    graphics2D.fillRect(0, 0, width, height);
    final double imageWidth = image.getWidth();
    final double imageHeight = image.getHeight();
    final double xScale = width / imageWidth;
    final double yScale = height / imageHeight;
    double scale = 1.0;
    switch (scaleType) {
    case SCALE_FIT:
        scale = Math.min(xScale, yScale);
        break;
    case SCALE_FILL:
        scale = Math.max(xScale, yScale);
        break;
    default:
        logger.warning(String.format("Unexpected scale type: %d.", scaleType));
        break;
    }
    final double x = (width - imageWidth * scale) / 2;
    final double y = (height - imageHeight * scale) / 2;
    final AffineTransform affineTransform = AffineTransform.getTranslateInstance(x, y);
    affineTransform.scale(scale, scale);
    graphics2D.drawRenderedImage(image, affineTransform);
    graphics2D.dispose();
    return scaledImage;
}

From source file:Main.java

/**
 * Snapshots the specified JavaFX {@link Image} object and stores a
 * copy of its pixels into a {@link BufferedImage} object, creating
 * a new object if needed./*from w w w.  j a  v a 2s.c o  m*/
 * The method will only convert a JavaFX {@code Image} that is readable
 * as per the conditions on the
 * {@link Image#getPixelReader() Image.getPixelReader()}
 * method.
 * If the {@code Image} is not readable, as determined by its
 * {@code getPixelReader()} method, then this method will return null.
 * If the {@code Image} is a writable, or other dynamic image, then
 * the {@code BufferedImage} will only be set to the current state of
 * the pixels in the image as determined by its {@link PixelReader}.
 * Further changes to the pixels of the {@code Image} will not be
 * reflected in the returned {@code BufferedImage}.
 * <p>
 * The optional {@code BufferedImage} parameter may be reused to store
 * the copy of the pixels.
 * A new {@code BufferedImage} will be created if the supplied object
 * is null, is too small or of a type which the image pixels cannot
 * be easily converted into.
 * 
 * @param img the JavaFX {@code Image} to be converted
 * @param bimg an optional {@code BufferedImage} object that may be
 *        used to store the returned pixel data
 * @return a {@code BufferedImage} containing a snapshot of the JavaFX
 *         {@code Image}, or null if the {@code Image} is not readable.
 * @since JavaFX 2.2
 */
public static BufferedImage fromFXImage(Image img, BufferedImage bimg) {
    PixelReader pr = img.getPixelReader();
    if (pr == null) {
        return null;
    }
    int iw = (int) img.getWidth();
    int ih = (int) img.getHeight();
    int prefBimgType = getBestBufferedImageType(pr.getPixelFormat(), bimg);
    if (bimg != null) {
        int bw = bimg.getWidth();
        int bh = bimg.getHeight();
        if (bw < iw || bh < ih || bimg.getType() != prefBimgType) {
            bimg = null;
        } else if (iw < bw || ih < bh) {
            Graphics2D g2d = bimg.createGraphics();
            g2d.setComposite(AlphaComposite.Clear);
            g2d.fillRect(0, 0, bw, bh);
            g2d.dispose();
        }
    }
    if (bimg == null) {
        bimg = new BufferedImage(iw, ih, prefBimgType);
    }
    IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster();
    int offset = icr.getDataOffset(0);
    int scan = icr.getScanlineStride();
    int data[] = icr.getDataStorage();
    WritablePixelFormat<IntBuffer> pf = getAssociatedPixelFormat(bimg);
    pr.getPixels(0, 0, iw, ih, pf, data, offset, scan);
    return bimg;
}

From source file:org.apache.fop.visual.BitmapComparator.java

/**
 * Builds a combined image that places a number of images next to each other for
 * manual, visual comparison./*from  w  ww. ja va 2 s .com*/
 * @param images the array of bitmaps
 * @return the combined image
 */
public static BufferedImage buildCompareImage(BufferedImage[] images) {
    BufferedImage cmp = new BufferedImage(images[0].getWidth() * images.length, images[0].getHeight(),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = cmp.createGraphics();
    g.setPaint(Color.white);
    g.fillRect(0, 0, cmp.getWidth(), cmp.getHeight());
    int lastWidth = 0;
    for (int i = 0; i < images.length; i++) {
        if (lastWidth > 0) {
            g.translate(lastWidth, 0);
        }
        if (images[i] != null) {
            g.drawImage(images[i], 0, 0, null);
            lastWidth = images[i].getWidth();
        } else {
            lastWidth = 20; //Maybe add a special placeholder image instead later
        }
    }
    g.dispose();

    return cmp;
}

From source file:com.smash.revolance.ui.model.helper.ImageHelper.java

public static BufferedImage eraseRect(BufferedImage image, int x, int y, int w, int h, double xScale,
        double yScale) {
    x = (int) (x * xScale);
    w = (int) (w * xScale);

    y = (int) (y * yScale);
    h = (int) (h * yScale);

    Graphics2D g = image.createGraphics();
    g.setColor(new Color(0, 0, 0, 0));
    g.fillRect(x, y, w, h);
    g.dispose();/* ww w. jav  a  2  s.  co  m*/
    return image;
}

From source file:com.shending.support.CompressPic.java

/**
 * ?/*from w  w w .java2 s.  co  m*/
 *
 * @param srcFile
 * @param dstFile
 * @param widthRange
 * @param heightRange
 */
public static void cutSquare(String srcFile, String dstFile, int widthRange, int heightRange, int width,
        int height) {
    int x = 0;
    int y = 0;
    try {
        ImageInputStream iis = ImageIO.createImageInputStream(new File(srcFile));
        Iterator<ImageReader> iterator = ImageIO.getImageReaders(iis);
        ImageReader reader = (ImageReader) iterator.next();
        reader.setInput(iis, true);
        ImageReadParam param = reader.getDefaultReadParam();
        int oldWidth = reader.getWidth(0);
        int oldHeight = reader.getHeight(0);
        int newWidth, newHeight;
        if (width <= oldWidth && height <= oldHeight) {
            newWidth = oldHeight * widthRange / heightRange;
            if (newWidth < oldWidth) {
                newHeight = oldHeight;
                x = (oldWidth - newWidth) / 2;
            } else {
                newWidth = oldWidth;
                newHeight = oldWidth * heightRange / widthRange;
                y = (oldHeight - newHeight) / 2;
            }
            Rectangle rectangle = new Rectangle(x, y, newWidth, newHeight);
            param.setSourceRegion(rectangle);
            BufferedImage bi = reader.read(0, param);
            BufferedImage tag = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(bi.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
            File file = new File(dstFile);
            ImageIO.write(tag, reader.getFormatName(), file);
        } else {
            BufferedImage bi = reader.read(0, param);
            BufferedImage tag = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = tag.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, tag.getWidth(), tag.getHeight());
            g2d.drawImage(bi.getScaledInstance(bi.getWidth(), bi.getHeight(), Image.SCALE_SMOOTH),
                    (width - bi.getWidth()) / 2, (height - bi.getHeight()) / 2, null);
            g2d.dispose();
            File file = new File(dstFile);
            ImageIO.write(tag, reader.getFormatName(), file);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static int[] makeGradientPallet() {
    BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    Point2D start = new Point2D.Float(0f, 0f);
    Point2D end = new Point2D.Float(99f, 0f);
    float[] dist = { 0.0f, 0.5f, 1.0f };
    Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
    g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
    g2.fillRect(0, 0, 100, 1);
    g2.dispose();//ww w  .ja v  a  2 s. c o  m

    int width = image.getWidth(null);
    int[] pallet = new int[width];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width);
    try {
        pg.grabPixels();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pallet;
}