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:com.alvermont.terraj.stargen.ui.UIUtils.java

/**
 * Combine a list of images horizontally into one new image.
 * /*from w  w w .j av a 2 s.  c om*/
 * @param images The list of images to be combined
 * @return A new image, as wide horizontally as the sum of the input images,
 * containing all the input images
 */
public static BufferedImage combineImagesHorizontal(List<BufferedImage> images) {
    int imageType = -1;
    int height = 0;
    int width = 0;

    // first work out the sizing and image type, we assume the images
    // are all compatible.

    for (BufferedImage image : images) {
        if (imageType == -1) {
            imageType = image.getType();
        }

        width += image.getWidth();

        height = Math.max(height, image.getHeight());
    }

    // create the new image and clear it to black

    BufferedImage bi = new BufferedImage(width, height, imageType);

    Graphics2D g = bi.createGraphics();
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, width, height);

    // merge the images into the new one

    int xpos = 0;

    for (BufferedImage image : images) {
        int ypos = (height - image.getHeight()) / 2;

        g.drawImage(image, xpos, ypos, null);

        xpos += image.getWidth();
    }

    return bi;
}

From source file:org.jfree.data.general.HeatMapUtilities.java

/**
 * Creates an image that displays the values from the specified dataset.
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param paintScale  the paint scale for the z-values (<code>null</code>
 *         not permitted).// www.  ja  v a2 s .  c o  m
 *
 * @return A buffered image.
 */
public static BufferedImage createHeatMapImage(HeatMapDataset dataset, PaintScale paintScale) {

    ParamChecks.nullNotPermitted(dataset, "dataset");
    ParamChecks.nullNotPermitted(paintScale, "paintScale");
    int xCount = dataset.getXSampleCount();
    int yCount = dataset.getYSampleCount();
    BufferedImage image = new BufferedImage(xCount, yCount, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    for (int xIndex = 0; xIndex < xCount; xIndex++) {
        for (int yIndex = 0; yIndex < yCount; yIndex++) {
            double z = dataset.getZValue(xIndex, yIndex);
            Paint p = paintScale.getPaint(z);
            g2.setPaint(p);
            g2.fillRect(xIndex, yCount - yIndex - 1, 1, 1);
        }
    }
    return image;
}

From source file:org.jfree.data.general.HeatMapUtils.java

/**
 * Creates an image that displays the values from the specified dataset.
 *
 * @param dataset  the dataset ({@code null} not permitted).
 * @param paintScale  the paint scale for the z-values ({@code null}
 *         not permitted)./*from  www .  ja  v a2  s.  c  om*/
 *
 * @return A buffered image.
 */
public static BufferedImage createHeatMapImage(HeatMapDataset dataset, PaintScale paintScale) {

    Args.nullNotPermitted(dataset, "dataset");
    Args.nullNotPermitted(paintScale, "paintScale");
    int xCount = dataset.getXSampleCount();
    int yCount = dataset.getYSampleCount();
    BufferedImage image = new BufferedImage(xCount, yCount, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    for (int xIndex = 0; xIndex < xCount; xIndex++) {
        for (int yIndex = 0; yIndex < yCount; yIndex++) {
            double z = dataset.getZValue(xIndex, yIndex);
            Paint p = paintScale.getPaint(z);
            g2.setPaint(p);
            g2.fillRect(xIndex, yCount - yIndex - 1, 1, 1);
        }
    }
    return image;
}

From source file:common.utils.ImageUtils.java

/**
 *
 * @param src images to draw, they must be resized to an appropriate size
 * @param dst image where given images will be drawen
 *///w  w w  .  j  a v a  2  s.  c  om
public static void draw4on1(BufferedImage[] src, BufferedImage dst) {
    Graphics2D g2 = dst.createGraphics();
    g2.setColor(java.awt.Color.WHITE);
    g2.fillRect(0, 0, dst.getWidth(), dst.getHeight());
    int dxi;
    int dyi = 0;

    int x0 = dst.getWidth() - 5;
    int y0 = 5;
    int x = x0;
    int y = y0;
    for (int i = 0; i < src.length; i++) {
        if (i % 2 == 0) {
            dxi = -10;
        } else {
            dxi = 0;
        }
        //g2.draw3DRect(dx - 1 , dy-tmp_bi.getHeight() - 1, tmp_bi.getWidth() + 1 , tmp_bi.getHeight() + 1, true);
        g2.drawImage(src[i], x - src[i].getWidth() + dxi, y + dyi, null);
        g2.drawString("#" + i, x - src[i].getWidth() + dxi, y + dyi + 20);
        //g2.rotate(Math.toRadians(4));
        y = y + src[i].getHeight() / 2;
        if (y > dst.getHeight() - src[i].getHeight()) {
            y = y0;
            if (dyi == 0)
                dyi = 10;
            else
                dyi = 0;
            if (x < src[i].getWidth()) {
                x = dst.getWidth();
            }
            x = x - src[i].getWidth() / 2;
        }
    }
    g2.setColor(Color.gray);
    g2.drawRect(0, 0, dst.getWidth() - 1, dst.getHeight() - 1);
    g2.dispose();
}

From source file:Main.java

/**
 * Creates a nifty looking inner shadow for the window.
 * @param width The width of the shadow.
 * @param height The height of the shadow.
 * @return The translucent shadow that was created.
 *//*from   w  ww . j a  v a  2  s  .  c o  m*/
public static BufferedImage genInnerShadow(int width, int height) {
    BufferedImage shadowOuter = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = shadowOuter.getGraphics();
    Graphics2D g2 = (Graphics2D) g;
    Point2D center = new Point2D.Float(width / 2, height / 2);
    float radius = width;
    Point2D focus = new Point2D.Float(width / 2, height / 2);
    float[] dist = { 0.0f, 1.0f };
    Color[] colors = { new Color(0, 0, 0, 0), new Color(0, 0, 0, 220) };
    RadialGradientPaint p = new RadialGradientPaint(center, radius, focus, dist, colors, CycleMethod.NO_CYCLE);
    g2.setPaint(p);
    g2.fillRect(0, 0, width, height);
    return shadowOuter;
}

From source file:Main.java

public static void smoothFillHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    Graphics2D g2D = (Graphics2D) g;
    Paint savedPaint = g2D.getPaint();
    int steps = colors.length;
    double dy = (double) h / (double) (steps - 1);
    int y1 = y;//  w  ww .ja v a  2 s  .  c o m
    for (int i = 0; i < steps; i++) {
        int y2 = y + (int) Math.round((double) i * dy);
        if (i == (steps - 1)) {
            g2D.setPaint(null);
            g2D.setColor(colors[i]);
            g2D.fillRect(x, y1, w, y + h - y1);
        } else {
            g2D.setPaint(new GradientPaint(0, y1, colors[i], 0, y2, colors[i + 1]));
            g2D.fillRect(x, y1, w, y2 - y1);
        }
        y1 = y2;
    }
    g2D.setPaint(savedPaint);
}

From source file:org.gitools.ui.app.heatmap.drawer.AbstractHeatmapDrawer.java

protected static void paintCell(Decoration decoration, Color gridColor, int gridSize, int offsetX, int offsetY,
        int width, int height, Graphics2D g, Rectangle box) {

    int y = box.y + offsetY;
    int x = box.x + offsetX;

    g.setColor(decoration.getBgColor());
    g.fillRect(x, y, width, height);

    g.setColor(gridColor);/*from w w  w  .  j  av a2  s  . co m*/
    g.fillRect(x, y + height, width, gridSize);

    String text = decoration.getFormatedValue();
    if (!StringUtils.isEmpty(text)) {

        Font font = g.getFont();

        boolean isRotated = decoration.isRotate();

        int fontHeight = (int) font.getSize2D();

        if (fontHeight <= (isRotated ? width : height)) {

            int textWidth = (int) g.getFontMetrics().getStringBounds(text, g).getWidth();
            //TODO: textWidth depends on SuperScript

            if (textWidth < (isRotated ? height : width)) {

                int leftMargin = ((width - textWidth) / 2) + 1;
                int bottomMargin = ((height - fontHeight) / 2) + 1;

                if (isRotated) {
                    leftMargin = ((width - fontHeight) / 2) + 1;
                    bottomMargin = height - (((height - textWidth) / 2));
                }

                g.setColor(Colors.bestForegroundColor(decoration.getBgColor()));
                if (text.matches("[0-9\\.]+e-?[0-9]+")) {
                    int e_pos = text.indexOf("e") + 3;
                    text = text.replaceAll("e(-?[0-9]+)", "10$1");
                    int superscriptEnd = text.length();
                    AttributedString attText = new AttributedString(text);
                    attText.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, e_pos,
                            superscriptEnd);
                    if (isRotated) {
                        g.rotate(radianAngle90);
                        g.drawString(attText.getIterator(), y + height - bottomMargin, -x - leftMargin - 1);
                        g.rotate(-radianAngle90);
                    } else {
                        g.drawString(attText.getIterator(), x + leftMargin, y + height - bottomMargin);
                    }
                } else {

                    if (isRotated) {
                        g.rotate(radianAngle90);
                        g.drawString(text, y + height - bottomMargin, -x - leftMargin - 1);

                        if ("CoCA-08".equals(text)) {
                            System.out.println("x = " + x + " leftMargin = " + leftMargin + " width = " + width
                                    + " fontHeight = " + fontHeight);
                        }

                        g.rotate(-radianAngle90);
                    } else {
                        g.drawString(text, x + leftMargin, y + height - bottomMargin);
                    }
                }
            }
        }
    }

}

From source file:be.fedict.eid.applet.maven.DocbookMojo.java

private static void graphToFile(BasicVisualizationServer<String, String> visualization, File file)
        throws IOException {
    Dimension size = visualization.getSize();
    int width = (int) (size.getWidth() + 1);
    int height = (int) (size.getHeight() + 1);
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bufferedImage.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(0, 0, 900, 650);
    visualization.setBounds(0, 0, 900, 650);
    visualization.paint(graphics);/*  w  w w  . j  ava  2 s.  c  om*/
    graphics.dispose();
    ImageIO.write(bufferedImage, "png", file);
}

From source file:net.rptools.lib.image.ImageUtil.java

public static void clearImage(BufferedImage image) {
    if (image == null)
        return;//  w  w  w . j a  v a2s . c o m

    Graphics2D g = null;
    try {
        g = (Graphics2D) image.getGraphics();
        Composite oldComposite = g.getComposite();
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.setComposite(oldComposite);
    } finally {
        if (g != null) {
            g.dispose();
        }
    }
}

From source file:org.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.java

/**
 * Returns an ARGB image filled with the given paint and using the given image as a mask.
 * @param paint the paint to fill the visible portions of the image with
 * @return a masked image filled with the given paint
 * @throws IOException if the image cannot be read
 * @throws IllegalStateException if the image is not a stencil.
 *//*from  w ww  .j a va 2  s .  com*/
public static BufferedImage getStencilImage(PDImage pdImage, Paint paint) throws IOException {
    // get mask (this image)
    BufferedImage mask = getRGBImage(pdImage, null);

    // compose to ARGB
    BufferedImage masked = new BufferedImage(mask.getWidth(), mask.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = masked.createGraphics();

    // draw the mask
    //g.drawImage(mask, 0, 0, null);

    // fill with paint using src-in
    //g.setComposite(AlphaComposite.SrcIn);
    g.setPaint(paint);
    g.fillRect(0, 0, mask.getWidth(), mask.getHeight());
    g.dispose();

    // set the alpha
    int width = masked.getWidth();
    int height = masked.getHeight();
    WritableRaster raster = masked.getRaster();
    WritableRaster alpha = mask.getRaster();

    final float[] transparent = new float[4];
    float[] alphaPixel = null;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            alphaPixel = alpha.getPixel(x, y, alphaPixel);
            if (alphaPixel[0] == 255) {
                raster.setPixel(x, y, transparent);
            }
        }
    }

    return masked;
}