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(Point p, Dimension d) 

Source Link

Document

Constructs a new Rectangle whose upper-left corner is specified by the Point argument, and whose width and height are specified by the Dimension argument.

Usage

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    Rectangle r = new Rectangle(new Point(10, 10), new Dimension(40, 40));

    g2.fill(r);//from  w ww .j  a  va 2  s. c  om

}

From source file:Main.java

/**
 * Returns component bounds relative to another component.
 *
 * @param component/*from   w  ww .j  a v a  2 s . c o m*/
 *            component to process
 * @param relativeTo
 *            component relative to which bounds will be returned
 * @return component bounds relative to another component
 */
public static Rectangle getRelativeBounds(final Component component, final Component relativeTo) {
    return new Rectangle(getRelativeLocation(component, relativeTo), component.getSize());
}

From source file:Main.java

public static void setPreferredGeometry(JFrame frame) {

    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension frameSize = frame.getPreferredSize();
    Dimension screenSize = tk.getScreenSize();

    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }/*ww w .j  a  v a2  s. c  o  m*/

    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }

    Point p = new Point((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    Rectangle rect = new Rectangle(p, frameSize);
    frame.setBounds(rect);
}

From source file:AWTUtilities.java

/**
 * Packs and centers the given window relative to the given component. The
 * specified component may be <code>null</code>, in which case the window will
 * be centered on the screen. The method also makes sure that the target
 * window is fully visible by calling <code>forceToScreen</code>.
 *//*w  ww  .  j av a  2  s . co  m*/

public static void centerWindow(Window target, Component parent) {
    target.pack();

    Dimension size = target.getSize();
    Rectangle parentBounds = parent == null || !parent.isShowing() ? getUsableScreenBounds()
            : new Rectangle(parent.getLocationOnScreen(), parent.getSize());

    target.setLocation(parentBounds.x + (parentBounds.width - size.width) / 2,
            parentBounds.y + (parentBounds.height - size.height) / 2);

    forceToScreen(target);
}

From source file:GeneralPathDemo.java

public void paintComponent(Graphics g) {
    // super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    if (firstTime) {
        Dimension dim = getSize();
        int w = dim.width;
        int h = dim.height;
        oddShape = createPath();/*from w  w w. j  a  v a  2 s  . c o  m*/
        area = new Rectangle(w, h);
        bi = (BufferedImage) createImage(w, h);
        big = bi.createGraphics();
        big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        firstTime = false;
    }

    // Clears the shape that was previously drawn.
    big.setColor(Color.white);
    big.fillRect(0, 0, area.width, area.height);

    big.setColor(Color.magenta);
    big.setStroke(new BasicStroke(3.0f));
    big.draw(oddShape);
    // Draws the buffered image to the screen.
    g2.drawImage(bi, 0, 0, this);

}

From source file:Main.java

private static float getPercentageOnScreen(Point location, Dimension size, Rectangle screen) {
    Rectangle frameBounds = new Rectangle(location, size);
    Rectangle2D intersection = frameBounds.createIntersection(screen);
    int frameArea = size.width * size.height;
    int intersectionArea = (int) (intersection.getWidth() * intersection.getHeight());
    float percentage = (float) intersectionArea / frameArea;
    return percentage < 0 ? 0 : percentage;
}

From source file:GUIUtils.java

/**
 * Centers <CODE>wind</CODE> within its parent. If it has no parent then
 * center within the screen. If centering would cause the title bar to go
 * above the parent (I.E. cannot see the titlebar and so cannot move the
 * window) then move the window down./*from   w  w  w .j a va  2  s. c  o m*/
 * 
 * @param wind
 *          The Window to be centered.
 * 
 * @throws IllegalArgumentException
 *           If <TT>wind</TT> is <TT>null</TT>.
 */
public static void centerWithinParent(Window wind) {
    if (wind == null) {
        throw new IllegalArgumentException("null Window passed");
    }
    final Container parent = wind.getParent();
    if (parent != null && parent.isVisible()) {
        center(wind, new Rectangle(parent.getLocationOnScreen(), parent.getSize()));
    } else {
        centerWithinScreen(wind);
    }
}

From source file:org.dkpro.lab.reporting.ChartUtil.java

/**
 * Exports a JFreeChart to a SVG file.//from ww  w.  ja v  a 2 s .  c  om
 *
 * @param chart JFreeChart to export
 * @param aOS stream to write to.
 * @param aWidth width of the chart in pixels
 * @param aHeight height of the chart in pixels
 * @throws IOException if writing the svgFile fails.
 * @see <a href="http://dolf.trieschnigg.nl/jfreechart/">Saving JFreeChart as SVG vector images
 *      using Batik</a>
 */
public static void writeChartAsSVG(OutputStream aOS, JFreeChart chart, int aWidth, int aHeight)
        throws IOException {
    // Get a DOMImplementation and create an XML document
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(null, "svg", null);

    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    // draw the chart in the SVG generator
    chart.draw(svgGenerator, new Rectangle(aWidth, aHeight));

    // Write svg file
    Writer out = new OutputStreamWriter(aOS, "UTF-8");
    svgGenerator.stream(out, true /* use css */);
    out.flush();
    out.close();
}

From source file:fr.gael.dhus.datastore.processing.impl.ProcessingUtils.java

/**
 * Cut the quicklook that have a big width/height ratio.
 * Each sub-part is dispatched on multiple bands.
 *
 * @param quick_look the quick_look to be cut
 * @param max_ratio the maximum ratio between quick_look width and height.
 * @param margin the margin between each band. default 5.
 *//*from  ww w. j a va2  s.  c  o m*/
public static RenderedImage cutQuickLook(RenderedImage input_image, double max_ratio, int margin) {
    ColorModel color_model = input_image.getColorModel();
    if ((color_model == null) && (input_image.getSampleModel() != null)) {
        color_model = ColorRenderer.createColorModel(input_image.getSampleModel());
    }

    BufferedImage quick_look;
    try {
        quick_look = PlanarImage.wrapRenderedImage(input_image).getAsBufferedImage(
                new Rectangle(input_image.getWidth(), input_image.getHeight()), color_model);
    } catch (Exception e) {
        logger.error("Problem getting buffered image.", e);
        throw new IllegalArgumentException("Problem getting buffered image", e);
    }

    if ((quick_look != null) && ((quick_look.getWidth() > 0) && (quick_look.getHeight() > 0))) {
        //Compute width/height ratio
        int ql_width = quick_look.getWidth();
        int ql_height = quick_look.getHeight();
        int ratio = (int) Math.sqrt(Math.max(ql_width, ql_height) / Math.min(ql_width, ql_height));

        //Check if the quicklook has a strong width/height ratio
        if ((ratio < max_ratio) || (ratio <= 1))
            return PlanarImage.wrapRenderedImage(quick_look);

        /**
         * Cut the wider side (width or height) into "ratio" bands.
         * Ex: If height = 3 * width then we cut 3 bands along columns
         *     So height' = height / 3   (extract 1 band / 3 from height)
         *        width'  = width  * 3   (dispatch 3 bands along lines)
         */
        int width = ql_width; //width of the bands
        int height = ql_height; //height of the bands

        if (ql_width < ql_height) //cut along height
        {
            width = (ql_width + margin) * ratio;
            height = ql_height / ratio;
        } else //cut along width
        {
            width = ql_width / ratio;
            height = (ql_height + margin) * ratio;
        }

        //Dispatch the sub-parts
        BufferedImage quick_look_cut = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = quick_look_cut.createGraphics();

        for (int k = 0; k < ratio; k++) {
            BufferedImage ql_band = null;
            //Dispatch on columns
            if (ql_width < ql_height) {
                ql_band = quick_look.getSubimage(0, (k * ql_height) / ratio, ql_width, ql_height / ratio);
                g2.drawImage(ql_band, null, k * (ql_width + margin), 0);
            }
            //Dispatch on lines
            else {
                ql_band = quick_look.getSubimage((k * ql_width) / ratio, 0, ql_width / ratio, ql_height);
                g2.drawImage(ql_band, null, 0, k * (ql_height + margin));
            }
        } //for each band

        g2.dispose();
        return PlanarImage.wrapRenderedImage(quick_look_cut);
    }
    return PlanarImage.wrapRenderedImage(quick_look);
}

From source file:Main.java

ScreenCaptureRectangle(final BufferedImage screen) {
    BufferedImage screenCopy = new BufferedImage(screen.getWidth(), screen.getHeight(), screen.getType());
    JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
    JScrollPane screenScroll = new JScrollPane(screenLabel);

    screenScroll.setPreferredSize(new Dimension(300, 300));

    repaint(screen, screenCopy);/*ww  w .  j av a  2 s .c  om*/
    screenLabel.repaint();

    screenLabel.addMouseMotionListener(new MouseMotionAdapter() {
        Point start = new Point();

        @Override
        public void mouseMoved(MouseEvent me) {
            start = me.getPoint();
            repaint(screen, screenCopy);
            screenLabel.repaint();
        }

        @Override
        public void mouseDragged(MouseEvent me) {
            Point end = me.getPoint();
            captureRect = new Rectangle(start, new Dimension(end.x - start.x, end.y - start.y));
            repaint(screen, screenCopy);
            screenLabel.repaint();
        }
    });
    JOptionPane.showMessageDialog(null, screenScroll);
}