Example usage for java.awt Rectangle getX

List of usage examples for java.awt Rectangle getX

Introduction

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

Prototype

public double getX() 

Source Link

Document

Returns the X coordinate of the bounding Rectangle in double precision.

Usage

From source file:Main.java

/**
 * Returns the center position of the specified component.
 *///from  www  .jav a  2 s.  co m
public static Point2D.Double getCenter(Component c) {
    if (c != null) {
        final Rectangle r = c.getBounds();
        return new Point2D.Double(r.getX() + (r.getWidth() / 2d), r.getY() + (r.getHeight() / 2d));
    }

    return new Point2D.Double(0d, 0d);
}

From source file:Main.java

public static Point positionToClickPoint(Container component, int caretPosition, Container invokedIn) {
    if (component == null) {
        return null;
    }/* w  w w.j  a  v a2 s .c  o m*/

    //System.err.println("Checking: " + component.getClass().getName());
    if (component.getClass().getName().indexOf("EditorPane") >= 0) {
        try {
            java.lang.reflect.Method pointGetter = component.getClass().getMethod("modelToView",
                    new Class[] { Integer.TYPE });
            Rectangle rec = (Rectangle) pointGetter.invoke(component,
                    new Object[] { new Integer(caretPosition) });
            //System.err.println("Before: " + (int)rec.getY());
            // FIXME: somehow it fails here to convert point from scrollable component
            Point point = SwingUtilities.convertPoint(component, (int) rec.getX(), (int) rec.getY() + 10,
                    invokedIn);
            // FIXME: ugly hack :(
            if (point.getY() > 1024) {
                point = new Point((int) point.getX(), 250);
            }
            //System.err.println("After: " + (int)point.getY());
            return point;
        } catch (Exception e) {
            System.err.println("Method invocation exception caught");
            e.printStackTrace();

            //FIXME: BUG
            return null;
            //throw new RuntimeException("Method invocation exception caught");
        }
    }

    for (int i = 0; i < component.getComponentCount(); i++) {
        java.awt.Component childComponent = component.getComponent(i);
        if (childComponent instanceof javax.swing.JComponent) {
            Point point = positionToClickPoint((javax.swing.JComponent) childComponent, caretPosition,
                    invokedIn);
            if (point != null) {
                return point;
            }
        }
    }

    return null;
}

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

/**
 * Construct the smallest rectangle,/*from  w w  w .  jav a  2 s . co m*/
 * 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:Main.java

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

    Rectangle r = new Rectangle(100, 100, 200, 200);

    g2.fill(r);/*from w w w  .j  a  v  a2s .com*/
    System.out.println(r.getX());

}

From source file:CopyAreaPerformance.java

protected void paintComponent(Graphics g) {
    long startTime = System.nanoTime();
    // prevVX is set to -10000 when first enabled
    if (useCopyArea && prevVX > -9999) {
        // Most of this code determines the proper areas to copy and clip
        int scrollX = viewX - prevVX;
        int scrollY = viewY - prevVY;
        int copyFromY, copyFromX;
        int clipFromY, clipFromX;
        if (scrollX == 0) {
            // vertical scroll
            if (scrollY < 0) {
                copyFromY = 0;//from  w  w  w . j a va2 s. c om
                clipFromY = 0;
            } else {
                copyFromY = scrollY;
                clipFromY = getHeight() - scrollY;
            }
            // copy the old content, set the clip to the new area
            g.copyArea(0, copyFromY, getWidth(), getHeight() - Math.abs(scrollY), 0, -scrollY);
            g.setClip(0, clipFromY, getWidth(), Math.abs(scrollY));
        } else {
            // horizontal scroll
            if (scrollX < 0) {
                copyFromX = 0;
                clipFromX = 0;
            } else {
                copyFromX = scrollX;
                clipFromX = getWidth() - scrollX;
            }
            // copy the old content, set the clip to the new area
            g.copyArea(copyFromX, 0, getWidth() - Math.abs(scrollX), getHeight(), -scrollX, 0);
            g.setClip(clipFromX, 0, Math.abs(scrollX), getHeight());
        }
    }
    // Track previous view position for next scrolling operation
    prevVX = viewX;
    prevVY = viewY;
    // Get the clip in case we need it later
    Rectangle clipRect = g.getClip().getBounds();
    int clipL = (int) (clipRect.getX());
    int clipT = (int) (clipRect.getY());
    int clipR = (int) (clipRect.getMaxX());
    int clipB = (int) (clipRect.getMaxY());
    g.setColor(Color.WHITE);
    g.fillRect(clipL, clipT, (int) clipRect.getWidth(), (int) clipRect.getHeight());
    for (int column = 0; column < 256; ++column) {
        int x = column * (SMILEY_SIZE + PADDING) - viewX;
        if (useClip) {
            if (x > clipR || (x + (SMILEY_SIZE + PADDING)) < clipL) {
                // trivial reject; outside to the left or right
                continue;
            }
        }
        for (int row = 0; row < 256; ++row) {
            int y = row * (SMILEY_SIZE + PADDING) - viewY;
            if (useClip) {
                if (y > clipB || (y + (SMILEY_SIZE + PADDING)) < clipT) {
                    // trivial reject; outside to the top or bottom
                    continue;
                }
            }
            Color faceColor = new Color(column, row, 0);
            drawSmiley(g, faceColor, x, y);
        }
    }
    long stopTime = System.nanoTime();
    System.out.println("Painted in " + ((stopTime - startTime) / 1000000) + " ms");
}

From source file:com.smash.revolance.ui.comparator.page.PageComparator.java

private boolean layoutEquals(PageBean reference, PageBean page) {
    List<Rectangle> layout = getLayout(page);
    for (Rectangle refElementRect : getLayout(reference)) {
        int matchCount = 0;
        for (Rectangle pageElementRect : layout) {
            if (refElementRect.getX() == pageElementRect.getX()
                    && refElementRect.getY() == pageElementRect.getY()
                    && refElementRect.getWidth() == pageElementRect.getWidth()
                    && refElementRect.getHeight() == pageElementRect.getHeight()) {
                matchCount++;/*  w  ww. j a v  a2s.c o  m*/
            }
        }
        if (layout.size() == matchCount) {
            return true;
        }
    }
    return false;
}

From source file:com.tulskiy.musique.system.configuration.Configuration.java

public void setRectangle(String key, Rectangle value) {
    if (value == null)
        remove(key);//  w w  w .  ja va  2s. c  o  m
    else {
        String s = new Formatter().format("%d %d %d %d", (int) value.getX(), (int) value.getY(),
                (int) value.getWidth(), (int) value.getHeight()).toString();
        put(key, s);
    }
}

From source file:ch5ImageWriter.java

/**
 * write out the output image specified by index imageIndex using the
 * parameters specified by the ImageWriteParam object param
 *///from w ww  .ja  v  a 2  s.  co m
public void write(IIOMetadata metadata, IIOImage iioimage, ImageWriteParam param) {
    Node root = null;
    Node dimensionsElementNode = null;

    if (iioimage.getRenderedImage() != null)
        raster = iioimage.getRenderedImage().getData();
    else
        raster = iioimage.getRaster();

    /*
     * since this format allows you to write multiple images, the
     * streamMetadataWritten variable makes sure the stream metadata is
     * written only once
     */
    if (streamMetadataWritten == false) {
        if (metadata == null)
            metadata = getDefaultStreamMetadata(param);
        root = metadata.getAsTree("ch5.imageio.ch5stream_1.00");
        dimensionsElementNode = root.getFirstChild();
        Node numberImagesAttributeNode = dimensionsElementNode.getAttributes().getNamedItem("numberImages");
        String numberImages = numberImagesAttributeNode.getNodeValue();
        try {
            ios.writeBytes("5\n");
            ios.writeBytes(numberImages);
            ios.flush();
        } catch (IOException ioe) {
            System.err.println("IOException " + ioe.getMessage());
        }
        streamMetadataWritten = true;
    }

    String widthString;
    String heightString;
    IIOMetadata imageMetadata = (ch5ImageMetadata) iioimage.getMetadata();
    /*
     * don't really need image metadata object here since raster knows
     * necessary information
     */
    if (imageMetadata == null)
        imageMetadata = getDefaultImageMetadata(null, param);

    root = imageMetadata.getAsTree("ch5.imageio.ch5image_1.00");
    dimensionsElementNode = root.getFirstChild();

    Node widthAttributeNode = dimensionsElementNode.getAttributes().getNamedItem("imageWidth");
    widthString = widthAttributeNode.getNodeValue();

    Node heightAttributeNode = dimensionsElementNode.getAttributes().getNamedItem("imageHeight");
    heightString = heightAttributeNode.getNodeValue();

    int sourceWidth = Integer.parseInt(widthString);
    int sourceHeight = Integer.parseInt(heightString);
    int destinationWidth = -1;
    int destinationHeight = -1;
    int sourceRegionWidth = -1;
    int sourceRegionHeight = -1;
    int sourceRegionXOffset = -1;
    int sourceRegionYOffset = -1;
    int xSubsamplingFactor = -1;
    int ySubsamplingFactor = -1;

    if (param == null)
        param = getDefaultWriteParam();

    /*
     * get Rectangle object which will be used to clip the source image's
     * dimensions.
     */
    Rectangle sourceRegion = param.getSourceRegion();
    if (sourceRegion != null) {
        sourceRegionWidth = (int) sourceRegion.getWidth();
        sourceRegionHeight = (int) sourceRegion.getHeight();
        sourceRegionXOffset = (int) sourceRegion.getX();
        sourceRegionYOffset = (int) sourceRegion.getY();

        /*
         * correct for overextended source regions
         */
        if (sourceRegionXOffset + sourceRegionWidth > sourceWidth)
            destinationWidth = sourceWidth - sourceRegionXOffset;
        else
            destinationWidth = sourceRegionWidth;

        if (sourceRegionYOffset + sourceRegionHeight > sourceHeight)
            destinationHeight = sourceHeight - sourceRegionYOffset;
        else
            destinationHeight = sourceRegionHeight;
    } else {
        destinationWidth = sourceWidth;
        destinationHeight = sourceHeight;
        sourceRegionXOffset = sourceRegionYOffset = 0;
    }
    /*
     * get subsampling factors
     */
    xSubsamplingFactor = param.getSourceXSubsampling();
    ySubsamplingFactor = param.getSourceYSubsampling();

    destinationWidth = (destinationWidth - 1) / xSubsamplingFactor + 1;
    destinationHeight = (destinationHeight - 1) / ySubsamplingFactor + 1;

    byte[] sourceBuffer;
    byte[] destinationBuffer = new byte[destinationWidth];

    try {
        ios.writeBytes(new String("\n"));
        ios.writeBytes(new String(destinationWidth + "\n"));
        ios.writeBytes(new String(destinationHeight + "\n"));

        int jj;
        int index;
        for (int j = 0; j < sourceWidth; j++) {
            sourceBuffer = (byte[]) raster.getDataElements(0, j, sourceWidth, 1, null);
            jj = j - sourceRegionYOffset;
            if (jj % ySubsamplingFactor == 0) {
                jj /= ySubsamplingFactor;
                if ((jj >= 0) && (jj < destinationHeight)) {
                    for (int i = 0; i < destinationWidth; i++) {
                        index = sourceRegionXOffset + i * xSubsamplingFactor;
                        destinationBuffer[i] = sourceBuffer[index];
                    }
                    ios.write(destinationBuffer, 0, destinationWidth);
                    ios.flush();
                }
            }
        }
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
    }
}

From source file:net.sourceforge.dvb.projectx.common.Common.java

/**
 * //from  ww w .ja  v  a 2s .c  om
 */
public static void getMainFrameBounds() {
    Rectangle rect = (Rectangle) getGuiInterface().getMainFrameBounds();

    if (rect != null) {
        getSettings().setIntProperty(Keys.KEY_WindowPositionMain_X[0], (int) rect.getX());
        getSettings().setIntProperty(Keys.KEY_WindowPositionMain_Y[0], (int) rect.getY());
        getSettings().setIntProperty(Keys.KEY_WindowPositionMain_Width[0], (int) rect.getWidth());
        getSettings().setIntProperty(Keys.KEY_WindowPositionMain_Height[0], (int) rect.getHeight());
    }
}

From source file:ch5ImageReader.java

/**
 * read in the input image specified by index imageIndex using the
 * parameters specified by the ImageReadParam object param
 *//* ww  w  .  j  av  a  2 s .com*/
public BufferedImage read(int imageIndex, ImageReadParam param) {

    checkIndex(imageIndex);

    if (isSeekForwardOnly())
        minIndex = imageIndex;
    else
        minIndex = 0;

    BufferedImage bimage = null;
    WritableRaster raster = null;

    /*
     * this method sets the image metadata so that we can use the getWidth
     * and getHeight methods
     */
    setImageMetadata(iis, imageIndex);

    int srcWidth = getWidth(imageIndex);
    int srcHeight = getHeight(imageIndex);

    // initialize values to -1
    int dstWidth = -1;
    int dstHeight = -1;
    int srcRegionWidth = -1;
    int srcRegionHeight = -1;
    int srcRegionXOffset = -1;
    int srcRegionYOffset = -1;
    int xSubsamplingFactor = -1;
    int ySubsamplingFactor = -1;
    if (param == null)
        param = getDefaultReadParam();

    Iterator imageTypes = getImageTypes(imageIndex);
    try {
        /*
         * get the destination BufferedImage which will be filled using the
         * input image's pixel data
         */
        bimage = getDestination(param, imageTypes, srcWidth, srcHeight);

        /*
         * get Rectangle object which will be used to clip the source
         * image's dimensions.
         */
        Rectangle srcRegion = param.getSourceRegion();
        if (srcRegion != null) {
            srcRegionWidth = (int) srcRegion.getWidth();
            srcRegionHeight = (int) srcRegion.getHeight();
            srcRegionXOffset = (int) srcRegion.getX();
            srcRegionYOffset = (int) srcRegion.getY();

            /*
             * correct for overextended source regions
             */
            if (srcRegionXOffset + srcRegionWidth > srcWidth)
                dstWidth = srcWidth - srcRegionXOffset;
            else
                dstWidth = srcRegionWidth;

            if (srcRegionYOffset + srcRegionHeight > srcHeight)
                dstHeight = srcHeight - srcRegionYOffset;
            else
                dstHeight = srcRegionHeight;
        } else {
            dstWidth = srcWidth;
            dstHeight = srcHeight;
            srcRegionXOffset = srcRegionYOffset = 0;
        }
        /*
         * get subsampling factors
         */
        xSubsamplingFactor = param.getSourceXSubsampling();
        ySubsamplingFactor = param.getSourceYSubsampling();

        /**
         * dstWidth and dstHeight should be equal to bimage.getWidth() and
         * bimage.getHeight() after these next two instructions
         */
        dstWidth = (dstWidth - 1) / xSubsamplingFactor + 1;
        dstHeight = (dstHeight - 1) / ySubsamplingFactor + 1;
    } catch (IIOException e) {
        System.err.println("Can't create destination BufferedImage");
    }
    raster = bimage.getWritableTile(0, 0);

    /*
     * using the parameters specified by the ImageReadParam object, read the
     * image image data into the destination BufferedImage
     */
    byte[] srcBuffer = new byte[srcWidth];
    byte[] dstBuffer = new byte[dstWidth];
    int jj;
    int index;
    try {
        for (int j = 0; j < srcHeight; j++) {
            iis.readFully(srcBuffer, 0, srcWidth);

            jj = j - srcRegionYOffset;
            if (jj % ySubsamplingFactor == 0) {
                jj /= ySubsamplingFactor;
                if ((jj >= 0) && (jj < dstHeight)) {
                    for (int i = 0; i < dstWidth; i++) {
                        index = srcRegionXOffset + i * xSubsamplingFactor;
                        dstBuffer[i] = srcBuffer[index];
                    }
                    raster.setDataElements(0, jj, dstWidth, 1, dstBuffer);
                }
            }
        }
    } catch (IOException e) {
        bimage = null;
    }
    return bimage;
}