Example usage for java.awt Rectangle getWidth

List of usage examples for java.awt Rectangle getWidth

Introduction

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

Prototype

public double getWidth() 

Source Link

Document

Returns the width of the bounding Rectangle in double precision.

Usage

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Sets the location of the specified child relative to the location
 * of the specified parent and then makes it visible, and size to fill window.
 * This method is mainly useful for windows, frames and dialogs. 
 * //  w w  w. java2  s. c o m
 * @param parentBounds    The bounds of the visible parent.
 * @param child     The child to display.
 * @param max      The maximum size of the window.
 */
public static void setLocationRelativeToAndSizeToWindow(Rectangle parentBounds, Component child,
        Dimension max) {
    if (child == null)
        return;
    if (parentBounds == null)
        parentBounds = new Rectangle(0, 0, 5, 5);
    if (max == null)
        max = new Dimension(5, 5);
    int x = (int) (parentBounds.getX() + parentBounds.getWidth());
    int y = (int) parentBounds.getY();
    int childWidth = child.getWidth();
    int childHeight = child.getHeight();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    if (x + childWidth > screenSize.getWidth()) {
        if (childWidth < parentBounds.getX())
            x = (int) (parentBounds.getX()) - childWidth;
        else
            x = (int) (screenSize.getWidth() - childWidth);
    }
    child.setLocation(x, y);
    int newHeight = (int) screenSize.getHeight() - y - 10;
    int newWidth = (int) screenSize.getWidth() - x - 10;

    if (newWidth > childWidth)
        childWidth = newWidth;
    if (newHeight > childHeight)
        childHeight = newHeight;

    if (childWidth > max.getWidth())
        childWidth = (int) max.getWidth();
    if (childHeight > max.getHeight())
        childHeight = (int) max.getHeight();

    child.setSize(childWidth, childHeight);
    child.setVisible(true);
}

From source file:org.openstreetmap.josm.Main.java

static public void saveGuiGeometry() {
    // save the current window geometry
    String newGeometry = "";
    try {/*from   www.j a  v a 2  s  .c  o m*/
        if (((JFrame) parent).getExtendedState() == JFrame.NORMAL) {
            Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle bounds = parent.getBounds();
            int width = (int) bounds.getWidth();
            int height = (int) bounds.getHeight();
            int x = (int) bounds.getX();
            int y = (int) bounds.getY();
            if (width > screenDimension.width)
                width = screenDimension.width;
            if (height > screenDimension.height)
                width = screenDimension.height;
            if (x < 0)
                x = 0;
            if (y < 0)
                y = 0;
            newGeometry = width + "x" + height + "+" + x + "+" + y;
        }
    } catch (Exception e) {
        System.out.println("Failed to save GUI geometry: " + e);
    }
    pref.put("gui.geometry", newGeometry);
}

From source file:org.pentaho.reporting.libraries.designtime.swing.LibSwingUtil.java

public static String rectangleToString(final Rectangle rectangle) {
    final StringBuilder buffer = new StringBuilder();
    buffer.append(rectangle.getX());// w w w .ja  v a  2 s.c  o  m
    buffer.append(",");
    buffer.append(rectangle.getY());
    buffer.append(",");
    buffer.append(rectangle.getWidth());
    buffer.append(",");
    buffer.append(rectangle.getHeight());
    return buffer.toString();
}

From source file:org.photovault.dcraw.AHDInterpolateOp.java

@Override
public Rectangle mapDestRect(Rectangle src, int srcIndex) {
    Rectangle ret = null;//from   ww w . j  ava2s .  co m
    if (downSample == 1) {
        ret = new Rectangle((int) src.getMinX() - 3, (int) src.getMinY() - 3, (int) src.getWidth() + 6,
                (int) src.getHeight() + 6);
    } else {
        ret = new Rectangle((int) src.getMinX() * downSample, (int) src.getMinY() * downSample,
                (int) src.getWidth() * downSample, (int) src.getHeight() * downSample);
    }
    return ret;
}

From source file:org.photovault.dcraw.AHDInterpolateOp.java

@Override
public Rectangle mapSourceRect(Rectangle source, int srcIndex) {
    Rectangle ret = new Rectangle((int) source.getMinX() + 3, (int) source.getMinY() + 3,
            (int) source.getWidth() - 6, (int) source.getHeight() - 6);
    return ret;//from   w  ww. ja va 2  s.c o m
}

From source file:org.photovault.dcraw.AHDInterpolateOp.java

/**
 * Conpute a rectangle of destination image using AHD interpolation
 * @param img Array of soruce images (containing just one image in this case
 * @param dst Raster for the results/*w w  w.  ja v  a  2s . c om*/
 * @param area Area of dst that needs to be computed
 */
private void computeRectAHD(PlanarImage[] img, WritableRaster dst, Rectangle area) {
    log.debug("entry: computeAHD " + area);
    long entryTime = System.currentTimeMillis();

    // RandomIterFactory.create( img[0], area);
    int minx = Math.max((int) area.getMinX() - 3, 0);
    int miny = Math.max((int) area.getMinY() - 3, 0);
    int maxx = Math.min((int) area.getMaxX() + 3, getWidth() - 1);
    int maxy = Math.min((int) area.getMaxY() + 3, getHeight() - 1);
    Rectangle enlargedArea = new Rectangle(minx, miny, maxx - minx + 1, maxy - miny + 1);
    int[][][][] rgb = new int[2][(int) enlargedArea.getWidth()][(int) enlargedArea.getHeight()][3];
    int[][][][] lab = new int[2][(int) enlargedArea.getWidth()][(int) enlargedArea.getHeight()][3];
    byte[][][] homo = new byte[2][(int) enlargedArea.getWidth()][(int) enlargedArea.getHeight()];

    RandomIter riter = RandomIterFactory.create(img[0], enlargedArea);

    double xyz[] = new double[3];
    // Copy the data to temp array
    for (int y = 0; y < maxy - miny; y++) {
        for (int x = 0; x < maxx - minx; x++) {
            int color = fc(y + miny, x + minx);
            if (color == 3)
                color = 1;
            rgb[0][x][y][color] = rgb[1][x][y][color] = (int) (mult[color]
                    * riter.getSample(x + minx, y + miny, 0));

        }
    }

    // Interpolate green
    for (int y = 2; y < maxy - miny - 2; y++) {
        int firstColor = fc(y + miny, minx + 3);
        int startCol = minx + 3 - (firstColor % 2);
        for (int x = startCol - minx; x < maxx - minx - 2; x += 2) {
            int c = fc(y + miny, x + minx);
            if (c == 3) {
                c = 1;
            }
            int tc = rgb[0][x][y][c];
            int eg = rgb[0][x - 1][y][1];
            int wg = rgb[0][x + 1][y][1];
            int sg = rgb[0][x][y + 1][1];
            int ng = rgb[0][x][y - 1][1];
            int ec = rgb[0][x - 2][y][c];
            int wc = rgb[0][x + 2][y][c];
            int sc = rgb[0][x][y + 2][c];
            int nc = rgb[0][x][y - 2][c];

            // Horizonally
            int green = 2 * (wg + tc + eg) - (wc + ec);
            green >>= 2;
            if (green < 0)
                green = 0;
            rgb[0][x][y][1] = green;
            // Vertically
            green = 2 * (ng + tc + sg) - (nc + sc);
            green >>= 2;
            if (green < 0)
                green = 0;
            rgb[1][x][y][1] = green;
        }
    }
    // Interpolate R & B
    for (int dir = 0; dir < 2; dir++) {
        for (int y = 3; y < maxy - miny - 3; y++) {
            for (int x = 3; x < maxx - minx - 3; x++) {
                int color = fc(y + miny, x + minx);
                if (color == 1 || color == 3) {
                    // We need to interpolate both red and blue
                    int c = fc(y + 1 + miny, x + minx);
                    int tg = rgb[dir][x][y][1];
                    int ng = rgb[dir][x][y - 1][1];
                    int sg = rgb[dir][x][y + 1][1];
                    int eg = rgb[dir][x + 1][y][1];
                    int wg = rgb[dir][x - 1][y][1];
                    int nc = rgb[dir][x][y - 1][c];
                    int sc = rgb[dir][x][y + 1][c];
                    int wo = rgb[dir][x - 1][y][2 - c];
                    int eo = rgb[dir][x + 1][y][2 - c];
                    int val = tg + ((wo + eo - ng - sg) >> 1);
                    if (val < 0)
                        val = 0;
                    rgb[dir][x][y][2 - c] = val;
                    val = tg + ((nc + sc - ng - sg) >> 1);
                    if (val < 0)
                        val = 0;
                    rgb[dir][x][y][c] = val;
                } else {
                    /*
                    This pixel is either red or blue so only one of those needs
                    to be interpolated
                     */
                    int c = 2 - color;
                    int tg = rgb[dir][x][y][1];
                    int nwg = rgb[dir][x - 1][y - 1][1];
                    int seg = rgb[dir][x + 1][y + 1][1];
                    int swg = rgb[dir][x - 1][y + 1][1];
                    int neg = rgb[dir][x + 1][y - 1][1];
                    int nwc = rgb[dir][x - 1][y - 1][c];
                    int nec = rgb[dir][x + 1][y - 1][c];
                    int swc = rgb[dir][x - 1][y + 1][c];
                    int sec = rgb[dir][x + 1][y + 1][c];
                    int val = tg + ((nwc + nec + sec + swc - nwg - neg - swg - seg) >> 2);
                    if (val < 0)
                        val = 0;
                    rgb[dir][x][y][c] = val;
                }
                xyz[0] = xyz[1] = xyz[2] = 0.5;
                // Convert to cielab
                for (int i = 0; i < 3; i++) {
                    xyz[0] += xyz_cam[0][i] * rgb[dir][x][y][i];
                    xyz[1] += xyz_cam[1][i] * rgb[dir][x][y][i];
                    xyz[2] += xyz_cam[2][i] * rgb[dir][x][y][i];
                }
                xyz[0] = cbrt[Math.max(0, (int) Math.min(xyz[0], 65535.0))];
                xyz[1] = cbrt[Math.max(0, (int) Math.min(xyz[1], 65535.0))];
                xyz[2] = cbrt[Math.max(0, (int) Math.min(xyz[2], 65535.0))];
                lab[dir][x][y][0] = Math.max(0, (int) (64.0 * (116.0 * xyz[1] - 16.0)));
                lab[dir][x][y][1] = 0x8000 + 10 * (int) (64.0 * 500.0 * (xyz[0] - xyz[1]));
                lab[dir][x][y][2] = 0x8000 + 10 * (int) (64.0 * 200.0 * (xyz[1] - xyz[2]));
            }
        }
    }

    // Calculate the homogeneity maps
    int ldiff[][] = new int[2][4];
    int abdiff[][] = new int[2][4];
    int dx[] = { -1, 1, 0, 0 };
    int dy[] = { 0, 0, -1, 1 };
    for (int y = 2; y < maxy - miny - 2; y++) {
        for (int x = 2; x < maxx - minx - 2; x++) {
            for (int d = 0; d < 2; d++) {
                for (int i = 0; i < 4; i++) {
                    ldiff[d][i] = Math.abs(lab[d][x][y][0] - lab[d][x + dx[i]][y + dy[i]][0]);
                    int da = lab[d][x][y][1] - lab[d][x + dx[i]][y + dy[i]][1];
                    int db = lab[d][x][y][1] - lab[d][x + dx[i]][y + dy[i]][1];
                    abdiff[d][i] = da * da + db * db;
                }
            }
            int leps = Math.min(Math.max(ldiff[0][0], ldiff[0][1]), Math.max(ldiff[1][2], ldiff[1][3]));
            int abeps = Math.min(Math.max(abdiff[0][0], abdiff[0][1]), Math.max(abdiff[1][2], abdiff[1][3]));
            for (int d = 0; d < 2; d++) {
                for (int i = 0; i < 4; i++) {
                    if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps) {
                        homo[d][x][y]++;
                    }
                }
            }
        }
    }

    int dstMinx = Math.max((int) area.getMinX(), 5);
    int dstMiny = Math.max((int) area.getMinY(), 5);
    int dstMaxy = Math.min((int) area.getMaxY(), getHeight() - 5);
    int dstMaxx = Math.min((int) area.getMaxX(), getWidth() - 5);
    for (int row = dstMiny; row < dstMaxy; row++) {
        for (int col = dstMinx; col < dstMaxx; col++) {
            int hm0 = 0, hm1 = 0;
            for (int i = row - miny - 1; i <= row - miny + 1; i++) {
                for (int j = col - minx - 1; j <= col - minx + 1; j++) {
                    hm0 += homo[0][j][i];
                    hm1 += homo[1][j][i];
                }
            }
            if (hm0 < hm1) {
                dst.setPixel(col, row, rgb[1][col - minx][row - miny]);
            } else if (hm0 > hm1) {
                dst.setPixel(col, row, rgb[0][col - minx][row - miny]);
            } else {
                for (int i = 0; i < 3; i++) {
                    rgb[0][col - minx][row - miny][i] += rgb[1][col - minx][row - miny][i];
                    rgb[0][col - minx][row - miny][i] /= 2;
                }
                dst.setPixel(col, row, rgb[0][col - minx][row - miny]);
            }
        }
    }
    long dur = System.currentTimeMillis() - entryTime;
    log.debug("exit: computeRectAHD in " + dur + "ms");
}

From source file:org.photovault.dcraw.AHDInterpolateOp.java

/**
 * Compute a rectangle of destination image by downsampling original
 * @param img Array of soruce images (containing just one image in this case
 * @param dst Raster for the results//from  ww  w  . java  2s. co m
 * @param area Area of dst that needs to be computed
 */
private void computeDownsample(PlanarImage[] img, WritableRaster dst, Rectangle area) {
    log.debug("entry: computeDownsample " + area);
    long entryTime = System.currentTimeMillis();

    // Current line of image
    int rgb[][] = new int[(int) area.getWidth()][3];
    int sampleCount[][] = new int[(int) area.getWidth()][3];

    Rectangle srcArea = mapDestRect(area, 0);
    int srcMinx = (int) srcArea.getMinX();
    int srcMaxx = (int) srcArea.getMaxX();
    int srcMiny = (int) srcArea.getMinY();
    int srcMaxy = (int) srcArea.getMaxY();
    int dstY = (int) area.getMinY();
    int dstMinx = (int) area.getMinX();
    int sampleY = 0;
    RandomIter riter = RandomIterFactory.create(img[0], srcArea);

    for (int y = srcMiny; y < srcMaxy; y++) {
        int sampleX = 0;
        int dstX = 0;
        for (int x = srcMinx; x < srcMaxx; x++) {
            int val = riter.getSample(x, y, 0);
            int color = fc(y, x);
            color = (color == 3) ? 1 : color;
            rgb[dstX][color] += val;
            sampleCount[dstX][color]++;
            sampleX++;
            if (sampleX >= downSample) {
                dstX++;
                sampleX = 0;
            }
        }
        sampleY++;
        if (sampleY >= downSample) {
            for (int x = 0; x < rgb.length; x++) {
                for (int c = 0; c < 3; c++) {
                    int count = sampleCount[x][c];
                    if (count == 0) {
                        throw new IllegalStateException("zero samples for color component");
                    }
                    rgb[x][c] /= count;
                    rgb[x][c] = (int) (rgb[x][c] * mult[c]);
                }
                dst.setPixel(dstMinx + x, dstY, rgb[x]);
            }
            for (int x = 0; x < rgb.length; x++) {
                for (int c = 0; c < 3; c++) {
                    rgb[x][c] = 0;
                    sampleCount[x][c] = 0;
                }
            }
            sampleY = 0;
            dstY++;
        }
    }
    long dur = System.currentTimeMillis() - entryTime;
    log.debug("exit: computeDownsample in " + dur + "ms");
}

From source file:org.photovault.swingui.PhotoCollectionThumbView.java

/**
 * //from   w  w w  . j av a  2  s.c o  m
 *
 * @param mouseEvent a <code>MouseEvent</code> value
 */
public void mouseReleased(MouseEvent mouseEvent) {
    firstMouseEvent = null;
    if (dragType == DRAG_TYPE_SELECT && photos != null) {

        // Find out thumbails inside the selection rectangle

        // First lets restrict search to those rows that intersect with selection
        int topRow = (int) dragSelectionRect.getMinY() / rowHeight;
        int bottomRow = ((int) dragSelectionRect.getMaxY() / rowHeight) + 1;
        int startPhoto = topRow * columnsToPaint;
        int endPhoto = bottomRow * columnsToPaint;
        if (endPhoto > photos.size()) {
            endPhoto = photos.size();
        }

        // Find out which photos are selected
        for (int n = startPhoto; n < endPhoto; n++) {
            /*
             Performance optimization: Since getPhotoBounds() needs access 
             to photo thumbnail which may not yet be loaded we will do first 
             a rough check of if the table cell is in the selection area.
             */
            Rectangle cellRect = getPhotoCellBounds(n);
            if (dragSelectionRect.intersects(cellRect)) {
                Rectangle photoRect = getPhotoBounds(n);
                if (dragSelectionRect.intersects(photoRect)) {
                    selection.add(photos.get(n));
                    repaintPhoto(photos.get(n));
                }
            }
        }
        fireSelectionChangeEvent();
        // Redrw the selection area so that the selection rectangle is not shown anymore
        Rectangle repaintRect = dragSelectionRect;
        if (lastDragSelectionRect != null) {
            repaintRect = dragSelectionRect.union(lastDragSelectionRect);
        }
        repaint((int) repaintRect.getX() - 1, (int) repaintRect.getY() - 1, (int) repaintRect.getWidth() + 2,
                (int) repaintRect.getHeight() + 2);

        dragSelectionRect = null;
        lastDragSelectionRect = null;
        // Notify the mouse click handler that it has to do nothing
        dragJustEnded = true;
    }
}

From source file:org.photovault.swingui.PhotoCollectionThumbView.java

protected void handleSelectionDragEvent(MouseEvent e) {
    dragSelectionRect = new Rectangle(firstMouseEvent.getX(), firstMouseEvent.getY(), 0, 0);
    dragSelectionRect.add(e.getX(), e.getY());

    // Determine which area needs to be redrawn. If there is a selection marker rectangle already drawn
    // the redraw are must be union of the previous and current areas (current area can be also smaller!
    Rectangle repaintRect = dragSelectionRect;
    if (lastDragSelectionRect != null) {
        repaintRect = dragSelectionRect.union(lastDragSelectionRect);
    }//from  ww w  .j  ava 2s.c  o  m
    repaint((int) repaintRect.getX() - 1, (int) repaintRect.getY() - 1, (int) repaintRect.getWidth() + 2,
            (int) repaintRect.getHeight() + 2);
}

From source file:org.pmedv.blackboard.commands.ConvertToSymbolCommand.java

@Override
public void execute(ActionEvent e) {

    // which editor do we have?

    final BoardEditor editor = EditorUtils.getCurrentActiveEditor();

    final List<Item> items = editor.getSelectedItems();
    final List<Item> preserveItems = new ArrayList<Item>();

    for (Item item : items) {
        if (item instanceof Symbol || item instanceof Resistor || item instanceof Diode) {
            ErrorUtils//from   ww w  .  ja  v  a  2s  . c  o m
                    .showErrorDialog(new IllegalStateException(resources.getResourceByKey("error.onlyshapes")));
            return;
        }
    }

    // first we need to check all items and determine min_x, min_y, max_x, max_y as well as height and width

    int min_x = Integer.MAX_VALUE;
    int min_y = Integer.MAX_VALUE;
    int max_x = Integer.MIN_VALUE;
    int max_y = Integer.MIN_VALUE;

    for (Item item : items) {

        if (item instanceof Line) {

            final Line line = (Line) item;
            final Rectangle boundingBox = line.getBoundingBox();

            int x1 = (int) boundingBox.getLocation().getX();
            int y1 = (int) boundingBox.getLocation().getY();
            int x2 = x1 + (int) boundingBox.getWidth();
            int y2 = y1 + (int) boundingBox.getHeight();

            if (x1 < min_x)
                min_x = x1;
            if (y1 < min_y)
                min_y = y1;
            if (x2 > max_x)
                max_x = x2;
            if (y2 > max_y)
                max_y = y2;
        } else {
            if (item.getXLoc() + item.getWidth() > max_x)
                max_x = item.getXLoc() + item.getWidth();
            if (item.getYLoc() + item.getHeight() > max_y)
                max_y = item.getYLoc() + item.getHeight();
            if (item.getXLoc() < min_x)
                min_x = item.getXLoc();
            if (item.getYLoc() < min_y)
                min_y = item.getYLoc();
        }

    }

    int width = max_x - min_x;
    int height = max_y - min_y;

    final Symbol symbol = new Symbol();
    symbol.setWidth(width);
    symbol.setHeight(height);

    // sort items by z-index
    Collections.sort(items);

    symbol.setLayer(items.get(0).getLayer());

    for (final Item item : items) {

        // preserve old position and set new position and render, since we need to convert the coordinates
        // of the item to the newly calculated drawing area

        if (item instanceof Line) {

            final Line line = (Line) item;

            line.getOldstart().setLocation(line.getStart().getX(), line.getStart().getY());
            line.getOldEnd().setLocation(line.getEnd().getX(), line.getEnd().getY());

            line.getStart().setLocation(line.getStart().getX() - min_x, line.getStart().getY() - min_y);
            line.getEnd().setLocation(line.getEnd().getX() - min_x, line.getEnd().getY() - min_y);
        } else {

            item.setOldXLoc(item.getXLoc());
            item.setOldYLoc(item.getYLoc());

            item.setXLoc(item.getXLoc() - min_x);
            item.setYLoc(item.getYLoc() - min_y);
        }

        item.setOpacity(item.getOpacity());

        // restore position
        if (item instanceof Line) {

            final Line line = (Line) item;

            line.getStart().setLocation(line.getOldstart().getX(), line.getOldstart().getY());
            line.getEnd().setLocation(line.getOldEnd().getX(), line.getOldEnd().getY());
        } else {
            item.setXLoc(item.getOldXLoc());
            item.setYLoc(item.getOldYLoc());
        }

        symbol.getItems().add(item);

    }

    // now get a new index      
    int max = 0;

    for (final Layer layer : editor.getModel().getLayers()) {
        for (Item item : layer.getItems()) {
            if (item.getIndex() > max)
                max = item.getIndex();
        }
    }

    max++;
    symbol.setIndex(max);

    // remove original items and replace with created symbol 

    if (editor.getSelectedItems().size() > 1) {
        editor.clearSelectionBorder();

        preserveItems.addAll(editor.getSelectedItems());

        for (Item item : editor.getSelectedItems()) {
            for (Layer layer : editor.getModel().getLayers())
                layer.getItems().remove(item);
        }

        editor.getSelectedItems().clear();

        symbol.setXLoc(min_x);
        symbol.setYLoc(min_y);

        editor.getModel().getCurrentLayer().getItems().add(symbol);
        editor.setSelectedItem(symbol);

        UndoManager undoManager = editor.getUndoManager();
        if (!undoManager.addEdit(new ConvertToSymbolEdit(preserveItems, symbol))) {
            log.info("Could not add edit " + this.getClass());
        }
        editor.setFileState(FileState.DIRTY);

        AppContext.getContext().getBean(RedoCommand.class).setEnabled(undoManager.canRedo());
        AppContext.getContext().getBean(UndoCommand.class).setEnabled(undoManager.canUndo());

        editor.refresh();
    }

}