Example usage for java.awt Rectangle translate

List of usage examples for java.awt Rectangle translate

Introduction

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

Prototype

public void translate(int dx, int dy) 

Source Link

Document

Translates this Rectangle the indicated distance, to the right along the X coordinate axis, and downward along the Y coordinate axis.

Usage

From source file:GraphicsUtil.java

static public Rectangle getTextBounds(Graphics g, String text, int x, int y, int halign, int valign) {
    if (g == null)
        return new Rectangle(x, y, 0, 0);
    FontMetrics mets = g.getFontMetrics();
    int width = mets.stringWidth(text);
    int ascent = mets.getAscent();
    int height = ascent + mets.getDescent();
    Rectangle ret = new Rectangle(x, y, width, height);

    switch (halign) {
    case H_CENTER:
        ret.translate(-(width / 2), 0);
        break;/*  w ww  .ja v  a2 s  .c om*/
    case H_RIGHT:
        ret.translate(-width, 0);
        break;
    default:
        ;
    }
    switch (valign) {
    case V_TOP:
        break;
    case V_CENTER:
        ret.translate(0, -(ascent / 2));
        break;
    case V_BASELINE:
        ret.translate(0, -ascent);
        break;
    case V_BOTTOM:
        ret.translate(0, -height);
        break;
    default:
        ;
    }
    return ret;
}

From source file:Main.java

public static void scrollToCenter(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;//from ww  w . j ava  2s.c  o  m
    }
    JViewport viewport = (JViewport) table.getParent();
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
    Rectangle viewRect = viewport.getViewRect();
    rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);

    int centerX = (viewRect.width - rect.width) / 2;
    int centerY = (viewRect.height - rect.height) / 2;
    if (rect.x < centerX) {
        centerX = -centerX;
    }
    if (rect.y < centerY) {
        centerY = -centerY;
    }
    rect.translate(centerX, centerY);
    viewport.scrollRectToVisible(rect);
}

From source file:VASSAL.Info.java

/**
 * Get size of screen accounting for the screen insets (i.e. Windows taskbar)
 *
 * @return/*from   ww  w .j a  v  a  2 s . c  o m*/
 */
public static Rectangle getScreenBounds(Component c) {
    final Rectangle bounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    final GraphicsConfiguration config = c.getGraphicsConfiguration();
    final Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config);
    bounds.translate(insets.left, insets.top);
    bounds.setSize(bounds.width - insets.left - insets.right, bounds.height - insets.top - insets.bottom);
    return bounds;
}

From source file:JTableHelper.java

public static void scrollToCenter(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;/*from   w  w w.jav  a 2  s .  c  om*/
    }
    JViewport viewport = (JViewport) table.getParent();
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);
    Rectangle viewRect = viewport.getViewRect();
    rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);

    int centerX = (viewRect.width - rect.width) / 2;
    int centerY = (viewRect.height - rect.height) / 2;

    if (rect.x < centerX) {
        centerX = -centerX;
    }
    if (rect.y < centerY) {
        centerY = -centerY;
    }
    rect.translate(centerX, centerY);
    viewport.scrollRectToVisible(rect);
}

From source file:org.apache.jmeter.config.gui.ArgumentsPanel.java

/**
 * @param table {@link JTable}//from  w  ww  .  ja  va 2 s .  co m
 * @return number of visible rows
 */
private static int getNumberOfVisibleRows(JTable table) {
    Rectangle vr = table.getVisibleRect();
    int first = table.rowAtPoint(vr.getLocation());
    vr.translate(0, vr.height);
    return table.rowAtPoint(vr.getLocation()) - first;
}

From source file:Main.java

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

    Rectangle r = new Rectangle(10, 10, 50, 40);

    r.translate(20, 20);

    g2.fill(r);// w w  w . j a  v a2  s. c o m
}

From source file:de.dakror.villagedefense.game.entity.Entity.java

public Rectangle getBump(boolean includePos) {
    if (!includePos)
        return bump;
    else {/*from   w  w  w .jav a 2s  . com*/
        Rectangle rect = (Rectangle) bump.clone();
        rect.translate((int) x, (int) y);
        return rect;
    }
}

From source file:de.fhg.igd.mapviewer.waypoints.CustomWaypointPainter.java

/**
 * Find way-points in a rectangular area defined by the given
 * {@link GeoPosition}s//from  w  w  w . ja v a 2 s.  c  om
 * 
 * @param topLeft the top left position
 * @param bottomRight the bottom right position
 * @param worldRect the bounding box in world pixel coordinates
 * @param converter the pixel converter
 * @param zoom the zoom level
 * 
 * @return the way-points in the area
 */
public Set<W> findWaypoints(GeoPosition topLeft, GeoPosition bottomRight, final Rectangle worldRect,
        final PixelConverter converter, final int zoom) {
    BoundingBox searchBox;
    try {
        searchBox = createSearchBB(topLeft, bottomRight);
        final BoundingBox verifyBox = searchBox;

        Set<W> wps = waypoints.query(searchBox, new Verifier<W, BoundingBox>() {

            @Override
            public boolean verify(W wp, BoundingBox second) {
                try {
                    Point2D wpPixel = converter.geoToPixel(wp.getPosition(), zoom);
                    int dx = (int) wpPixel.getX();
                    int dy = (int) wpPixel.getY();

                    worldRect.translate(-dx, -dy);
                    try {
                        Area area = wp.getMarker().getArea(zoom);
                        if (area != null) {
                            return area.containedIn(worldRect);
                        } else {
                            // something that has not been painted yet may
                            // not be selected
                            return false;
                        }
                    } finally {
                        worldRect.translate(dx, dy);
                    }
                } catch (IllegalGeoPositionException e) {
                    log.warn("Could not convert waypoint position to pixel", e);
                    // fall back to simple method
                    return verifyBox.covers(wp.getBoundingBox());
                }
            }

        });

        if (wps == null) {
            return new HashSet<W>();
        } else {
            return wps;
        }
    } catch (IllegalGeoPositionException e) {
        return new HashSet<W>();
    }
}

From source file:net.sf.jabref.gui.maintable.MainTable.java

public void scrollToCenter(int rowIndex, int vColIndex) {
    if (!(this.getParent() instanceof JViewport)) {
        return;//  www .ja  v a  2  s. c  om
    }

    JViewport viewport = (JViewport) this.getParent();

    // This rectangle is relative to the table where the
    // northwest corner of cell (0,0) is always (0,0).
    Rectangle rect = this.getCellRect(rowIndex, vColIndex, true);

    // The location of the view relative to the table
    Rectangle viewRect = viewport.getViewRect();

    // Translate the cell location so that it is relative
    // to the view, assuming the northwest corner of the
    // view is (0,0).
    rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);

    // Calculate location of rect if it were at the center of view
    int centerX = (viewRect.width - rect.width) / 2;
    int centerY = (viewRect.height - rect.height) / 2;

    // Fake the location of the cell so that scrollRectToVisible
    // will move the cell to the center
    if (rect.x < centerX) {
        centerX = -centerX;
    }
    if (rect.y < centerY) {
        centerY = -centerY;
    }
    rect.translate(centerX, centerY);

    // Scroll the area into view.
    viewport.scrollRectToVisible(rect);

    revalidate();
    repaint();
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

protected Rectangle computeRectangles(PositionManager pman, BBoxManager bbman) {

    DecimalFormat mz_df = new DecimalFormat("0.0");

    Rectangle all_bbox = null;// w  ww. j a va  2  s.  c o m
    rectangles = new HashMap<AnnotationObject, Rectangle>();
    rectangles_text = new HashMap<AnnotationObject, Rectangle>();
    rectangles_complete = new HashMap<AnnotationObject, Rectangle>();
    for (AnnotationObject a : theDocument.getAnnotations()) {

        // set scale
        theGlycanRenderer.getGraphicOptions().setScale(theOptions.SCALE_GLYCANS * theDocument.getScale(a));

        // compute bbox
        Point2D anchor = dataToScreenCoords(theDocument.getAnchor(a));
        Rectangle bbox = theGlycanRenderer.computeBoundingBoxes(a.getStructures(), false, false, pman, bbman,
                false);

        int x = (int) anchor.getX() - bbox.width / 2;
        int y = (int) anchor.getY() - bbox.height - theOptions.ANNOTATION_MARGIN
                - theOptions.ANNOTATION_MZ_SIZE;
        bbman.translate(x - bbox.x, y - bbox.y, a.getStructures());
        bbox.translate(x - bbox.x, y - bbox.y);

        // save bbox
        rectangles.put(a, bbox);

        // compute text bbox        
        String mz_text = mz_df.format(a.getPeakPoint().getX());
        Dimension mz_dim = textBounds(mz_text, theOptions.ANNOTATION_MZ_FONT, theOptions.ANNOTATION_MZ_SIZE);
        Rectangle text_bbox = new Rectangle((int) anchor.getX() - mz_dim.width / 2,
                (int) anchor.getY() - 2 * theOptions.ANNOTATION_MARGIN / 3 - mz_dim.height, mz_dim.width,
                mz_dim.height);

        // save text bbox
        rectangles_text.put(a, text_bbox);
        rectangles_complete.put(a, expand(union(bbox, text_bbox), theOptions.ANNOTATION_MARGIN / 2));

        // update all bbox
        all_bbox = union(all_bbox, bbox);
        all_bbox = union(all_bbox, text_bbox);
    }
    if (all_bbox == null)
        return new Rectangle(0, 0, 0, 0);
    return all_bbox;
}