Example usage for java.awt Rectangle Rectangle

List of usage examples for java.awt Rectangle Rectangle

Introduction

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

Prototype

public Rectangle(int x, int y, int width, int height) 

Source Link

Document

Constructs a new Rectangle whose upper-left corner is specified as (x,y) and whose width and height are specified by the arguments of the same name.

Usage

From source file:fr.romainf.QRCode.java

/**
 * Renders the QRCode data in an SVG document.
 *
 * @param matrix      BitMatrix BitMatrix of the encoded QRCode
 * @param pixelColour Color The colour of pixels representing bits
 * @return SVGGraphics2D/*from w ww .j  av a  2  s .  co m*/
 */
private static SVGGraphics2D renderSVG(BitMatrix matrix, Color pixelColour) {
    DOMImplementation implementation = SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    Document document = implementation.createDocument(svgNS, "svg", null);

    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    int width = matrix.getWidth();
    int height = matrix.getHeight();

    Area area = new Area();
    for (int x = 0; x < width; x += 1) {
        for (int y = 0; y < height; y += 1) {
            if (matrix.get(x, y)) {
                area.add(new Area(new Rectangle(x, y, 1, 1)));
            }
        }
    }

    svgGenerator.setPaint(pixelColour);
    svgGenerator.fill(area);
    return svgGenerator;
}

From source file:gdsc.smlm.ij.plugins.FIRE.java

private MemoryPeakResults cropToRoi(MemoryPeakResults results) {
    if (roiBounds == null)
        return results;

    // Adjust bounds relative to input results image
    double xscale = roiImageWidth / results.getBounds().width;
    double yscale = roiImageHeight / results.getBounds().height;
    roiBounds.x /= xscale;//  ww  w  .  j  av a 2  s.  co  m
    roiBounds.width /= xscale;
    roiBounds.y /= yscale;
    roiBounds.height /= yscale;

    float minX = (int) (roiBounds.x);
    float maxX = (int) Math.ceil(roiBounds.x + roiBounds.width);
    float minY = (int) (roiBounds.y);
    float maxY = (int) Math.ceil(roiBounds.y + roiBounds.height);

    // Create a new set of results within the bounds
    MemoryPeakResults newResults = new MemoryPeakResults();
    newResults.begin();
    for (PeakResult peakResult : results.getResults()) {
        float x = peakResult.params[Gaussian2DFunction.X_POSITION];
        float y = peakResult.params[Gaussian2DFunction.Y_POSITION];
        if (x < minX || x > maxX || y < minY || y > maxY)
            continue;
        newResults.add(peakResult);
    }
    newResults.end();
    newResults.copySettings(results);
    newResults.setBounds(new Rectangle((int) minX, (int) minY, (int) (maxX - minX), (int) (maxY - minY)));
    return newResults;
}

From source file:de.dakror.villagedefense.game.entity.struct.Struct.java

public void setBump(Rectangle2D r) {
    super.setBump(new Rectangle((int) Math.round(r.getX() * Tile.SIZE), (int) Math.round(r.getY() * Tile.SIZE),
            (int) Math.round(r.getWidth() * Tile.SIZE), (int) Math.round(r.getHeight() * Tile.SIZE)));
}

From source file:DividerLayout.java

/**
 * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
 *///from  www . ja v  a2 s .  c  o m
public void layoutContainer(Container container) {
    Insets insets = container.getInsets();
    Dimension westSize = new Dimension(0, 0);
    Dimension centerSize = new Dimension(0, 0);
    Dimension eastSize = new Dimension(0, 0);
    Rectangle centerBounds = new Rectangle(0, 0, 0, 0);
    Dimension containerSize = container.getSize();
    int centerX = containerSize.width / 2;
    int centerY = containerSize.height / 2;
    if ((centerComponent != null) && (centerComponent.isVisible())) {
        centerSize = centerComponent.getPreferredSize();
        centerSize.width = Math.min(centerSize.width, containerSize.width - insets.left - insets.right);
        centerSize.height = Math.min(centerSize.height, containerSize.height - insets.top - insets.bottom);
        centerComponent.setBounds(centerX - (centerSize.width / 2), centerY - (centerSize.height / 2),
                centerSize.width, centerSize.height);
        centerBounds = centerComponent.getBounds();
    }
    if ((westComponent != null) && (westComponent.isVisible())) {
        westSize = westComponent.getPreferredSize();
    }
    if ((eastComponent != null) && (eastComponent.isVisible())) {
        eastSize = eastComponent.getPreferredSize();
    }
    /* int maxWidth = Math.min(westSize.width, eastSize.width);
     maxWidth = Math.min(maxWidth, (containerSize.width -
         centerBounds.width - insets.left -
         insets.right) / 2);*/
    int maxWidth = (containerSize.width - centerBounds.width - insets.left - insets.right) / 2;

    /* int maxHeight = Math.min(westSize.height, eastSize.height);
      maxHeight = Math.max(maxHeight, containerSize.height -
         insets.top - insets.bottom);*/
    int maxHeight = containerSize.height - insets.top - insets.bottom;
    if (westComponent != null) {
        westComponent.setBounds(centerBounds.x - maxWidth, centerY - (maxHeight / 2), maxWidth, maxHeight);
    }
    if (eastComponent != null) {
        eastComponent.setBounds(centerBounds.x + centerBounds.width, centerY - (maxHeight / 2), maxWidth,
                maxHeight);
    }
}

From source file:com.jaeksoft.searchlib.crawler.web.browser.BrowserDriver.java

final public Rectangle getRectangle(WebElement element) {
    if (element == null)
        return null;
    Rectangle box = new Rectangle(element.getLocation().x, element.getLocation().y, element.getSize().width,
            element.getSize().height);// w  w w  .j a v  a 2 s.co  m
    return box;
}

From source file:TexturedPanel.java

/**
 * Creates a new TexturePaint using the provided colors.
 *///from  w ww .  j av a 2 s. c o  m
private void setupDefaultPainter(Color foreground, Color background) {
    if (foreground == null || background == null) {
        ourPainter = null;
        return;
    }

    BufferedImage buff = new BufferedImage(6, 6, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();

    g2.setColor(background);
    g2.fillRect(0, 0, 6, 6);

    g2.setColor(foreground);
    g2.drawLine(0, 2, 6, 2);
    g2.drawLine(0, 5, 6, 5);

    ourPainter = new TexturePaint(buff, new Rectangle(0, 0, 6, 6));

    g2.dispose();
}

From source file:desmoj.extensions.visualization2d.engine.modelGrafic.StatisticGrafic.java

/**
 * Construct the smallest rectangle,//from w  w  w.  j  a v  a2  s . c om
 * which include the positions of all statistics in view  
 * @param viewId   id of view
 * @return    smallest Rectangle when an StatisticGrafic exist,
 *          null otherwise.
 */
public static Rectangle getBoundsExternGlobal(Model model, String viewId) {
    boolean found = false;
    double minX = (double) Integer.MAX_VALUE / 2;
    double minY = (double) Integer.MAX_VALUE / 2;
    double maxX = (double) Integer.MIN_VALUE / 2;
    double maxY = (double) Integer.MIN_VALUE / 2;
    String[] id = model.getStatistics().getAllIds();
    //System.out.println("Anz. Entities: "+id.length);
    for (int i = 0; i < id.length; i++) {
        Statistic statistic = model.getStatistics().get(id[i]);
        StatisticGrafic statistikGrafic = (StatisticGrafic) statistic.getGrafic();
        if (statistikGrafic != null && statistikGrafic.getViewId().equals(viewId)) {
            found = true;
            Rectangle r = statistikGrafic.getBoundsExtern();
            minX = Math.floor(Math.min(minX, r.getX()));
            minY = Math.floor(Math.min(minY, r.getY()));
            maxX = Math.ceil(Math.max(maxX, r.getX() + r.width));
            maxY = Math.ceil(Math.max(maxY, r.getY() + r.height));
            //System.out.println(statistic.getId()+"  "+statistikGrafic.pointExtern.getX()+" "+statistikGrafic.pointExtern.getY());
        }
    }
    Rectangle r = null;
    if (found)
        r = new Rectangle((int) Math.round(minX), (int) Math.round(minY), (int) Math.round(maxX - minX),
                (int) Math.round(maxY - minY));
    //System.out.println("StatisticGrafic: BoundsExtern: "+r);
    return r;
}

From source file:org.geopublishing.atlasStyler.classification.RasterClassification.java

/**
 * This is where the magic happens. Here the attributes of the features are
 * summarized in a {@link DynamicBin1D} class.
 * //from  w ww .  ja v  a 2  s  .  co  m
 * @throws IOException
 */
@Override
synchronized public DynamicBin1D getStatistics() throws InterruptedException, IOException {

    cancelCalculation.set(false);

    if (stats == null) {
        GridCoverage2D coverage = getStyledRaster().getGeoObject().read(null);

        stats = new DynamicBin1D();

        noDataValuesCount.set(0);

        final RenderedImage rim = coverage.getRenderedImage();

        long size = Long.valueOf(rim.getHeight()) * Long.valueOf(rim.getWidth());
        long maxPixels = 3000000l;
        if (size > maxPixels) {
            setSubsampling((int) (size / maxPixels));
            LOGGER.info("Subsampling to every " + getSubsampling() + " pixel");
        }

        for (int row = 0; row < rim.getHeight(); row++) {

            if (row % getSubsampling() != 0) {
                // Skipping this line for Subsampling
                continue;
            } else {
                // DO
                //               System.out.println("");
            }

            int x1 = 0;
            int w = rim.getWidth();

            int y1 = row;
            int h = 1;

            Raster data = rim.getData(new Rectangle(x1, y1, w, h));
            double[] values = data.getSamples(0, y1, w, h, getBand(), (double[]) null);

            final DoubleArrayList doubleArrayList = new DoubleArrayList(values);

            if (getStyledRaster().getNodataValue() != null) {
                int sizewithNodata = doubleArrayList.size();
                doubleArrayList
                        .removeAll(new DoubleArrayList(new double[] { getStyledRaster().getNodataValue() }));
                noDataValuesCount.addAndGet(sizewithNodata - doubleArrayList.size());
            }

            stats.addAllOf(doubleArrayList);

            //            LOGGER.debug("Added "+doubleArrayList.size()+" to statistics");
            //            LOGGER.debug(stats.size()+" in stats");
            doubleArrayList.clear();
        }

    }
    return stats;
}

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

/**
 * ?/*from  w  w  w .jav  a 2  s  .  c  o  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:CompositeEffects.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;

    // fill the background
    g.setPaint(new Color(175, 175, 175));
    g.fillRect(0, 0, getWidth(), getHeight());

    // Set text attributes
    g.setColor(Color.black);/* w  w w  .j  a va 2 s.  c om*/
    g.setFont(new Font("SansSerif", Font.BOLD, 12));

    // Draw the unmodified image
    g.translate(10, 10);
    g.drawImage(cover, 0, 0, this);
    g.drawString("SRC_OVER", 0, COVERHEIGHT + 15);

    // Draw the cover again, using AlphaComposite to make the opaque
    // colors of the image 50% translucent
    g.translate(COVERWIDTH + 10, 0);
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
    g.drawImage(cover, 0, 0, this);

    // Restore the pre-defined default Composite for the screen, so
    // opaque colors stay opaque.
    g.setComposite(AlphaComposite.SrcOver);
    // Label the effect
    g.drawString("SRC_OVER, 50%", 0, COVERHEIGHT + 15);

    // Now get an offscreen image to work with. In order to achieve
    // certain compositing effects, the drawing surface must support
    // transparency. Onscreen drawing surfaces cannot, so we have to do the
    // compositing in an offscreen image that is specially created to have
    // an "alpha channel", then copy the final result to the screen.
    BufferedImage offscreen = new BufferedImage(COVERWIDTH, COVERHEIGHT, BufferedImage.TYPE_INT_ARGB);

    // First, fill the image with a color gradient background that varies
    // left-to-right from opaque to transparent yellow
    Graphics2D osg = offscreen.createGraphics();
    osg.setPaint(new GradientPaint(0, 0, Color.yellow, COVERWIDTH, 0, new Color(255, 255, 0, 0)));
    osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT);

    // Now copy the cover image on top of this, but use the DstOver rule
    // which draws it "underneath" the existing pixels, and allows the
    // image to show depending on the transparency of those pixels.
    osg.setComposite(AlphaComposite.DstOver);
    osg.drawImage(cover, 0, 0, this);

    // And display this composited image on the screen. Note that the
    // image is opaque and that none of the screen background shows through
    g.translate(COVERWIDTH + 10, 0);
    g.drawImage(offscreen, 0, 0, this);
    g.drawString("DST_OVER", 0, COVERHEIGHT + 15);

    // Now start over and do a new effect with the off-screen image.
    // First, fill the offscreen image with a new color gradient. We
    // don't care about the colors themselves; we just want the
    // translucency of the background to vary. We use opaque black to
    // transparent black. Note that since we've already used this offscreen
    // image, we set the composite to Src, we can fill the image and
    // ignore anything that is already there.
    osg.setComposite(AlphaComposite.Src);
    osg.setPaint(new GradientPaint(0, 0, Color.black, COVERWIDTH, COVERHEIGHT, new Color(0, 0, 0, 0)));
    osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT);

    // Now set the compositing type to SrcIn, so colors come from the
    // source, but translucency comes from the destination
    osg.setComposite(AlphaComposite.SrcIn);

    // Draw our loaded image into the off-screen image, compositing it.
    osg.drawImage(cover, 0, 0, this);

    // And then copy our off-screen image to the screen. Note that the
    // image is translucent and some of the image shows through.
    g.translate(COVERWIDTH + 10, 0);
    g.drawImage(offscreen, 0, 0, this);
    g.drawString("SRC_IN", 0, COVERHEIGHT + 15);

    // If we do the same thing but use SrcOut, then the resulting image
    // will have the inverted translucency values of the destination
    osg.setComposite(AlphaComposite.Src);
    osg.setPaint(new GradientPaint(0, 0, Color.black, COVERWIDTH, COVERHEIGHT, new Color(0, 0, 0, 0)));
    osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT);
    osg.setComposite(AlphaComposite.SrcOut);
    osg.drawImage(cover, 0, 0, this);
    g.translate(COVERWIDTH + 10, 0);
    g.drawImage(offscreen, 0, 0, this);
    g.drawString("SRC_OUT", 0, COVERHEIGHT + 15);

    // Here's a cool effect; it has nothing to do with compositing, but
    // uses an arbitrary shape to clip the image. It uses Area to combine
    // shapes into more complicated ones.
    g.translate(COVERWIDTH + 10, 0);
    Shape savedClip = g.getClip(); // Save current clipping region
    // Create a shape to use as the new clipping region.
    // Begin with an ellipse
    Area clip = new Area(new Ellipse2D.Float(0, 0, COVERWIDTH, COVERHEIGHT));
    // Intersect with a rectangle, truncating the ellipse.
    clip.intersect(new Area(new Rectangle(5, 5, COVERWIDTH - 10, COVERHEIGHT - 10)));
    // Then subtract an ellipse from the bottom of the truncated ellipse.
    clip.subtract(new Area(new Ellipse2D.Float(COVERWIDTH / 2 - 40, COVERHEIGHT - 20, 80, 40)));
    // Use the resulting shape as the new clipping region
    g.clip(clip);
    // Then draw the image through this clipping region
    g.drawImage(cover, 0, 0, this);
    // Restore the old clipping region so we can label the effect
    g.setClip(savedClip);
    g.drawString("Clipping", 0, COVERHEIGHT + 15);
}