Java Code Examples for javax.swing.JComponent.getVisibleRect()

The following are Jave code examples for showing how to use getVisibleRect() of the javax.swing.JComponent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: OpenGL-Bullet-engine   File: ScrollUtil.java   View Source Code Vote up 5 votes
/**
 * Scroll to specified location. e.g.
 * <tt>scroll(component, LEFT, BOTTOM);</tt>.
 *
 * @param c
 *            JComponent to scroll.
 * @param horizontal
 *            Horizontal location. Should take the value: LEFT, HCENTER or
 *            RIGHT.
 * @param vertical
 *            Vertical location. Should take the value: TOP, VCENTER or
 *            BOTTOM.
 */
public static void scroll(JComponent c, int horizontal, int vertical){
	Rectangle visible=c.getVisibleRect();
	Rectangle bounds=c.getBounds();
	
	switch(vertical){
	case TOP:
		visible.y=0;
		break;
	case VCENTER:
		visible.y=(bounds.height-visible.height)/2;
		break;
	case BOTTOM:
		visible.y=bounds.height-visible.height+OFFSET;
		break;
	}
	
	switch(horizontal){
	case LEFT:
		visible.x=0;
		break;
	case HCENTER:
		visible.x=(bounds.width-visible.width)/2;
		break;
	case RIGHT:
		visible.x=bounds.width-visible.width+OFFSET;
		break;
	}
	
	// When scrolling to bottom or right of viewport, add an OFFSET value.
	// This is because without this certain components (e.g. JTable) would
	// not scroll right to the bottom (presumably the bounds calculation
	// doesn't take the table header into account.  It doesn't matter if
	// OFFSET is a huge value (e.g. 10000) - the scrollRectToVisible method
	// still works correctly.
	
	c.scrollRectToVisible(visible);
}
 
Example 2
Project: incubator-netbeans   File: CenteredZoomAnimator.java   View Source Code Vote up 5 votes
@Override public void tick(double progress) {
    double nextZoom = progress >= 1.0 ? targetZoom :
        (sourceZoom + progress * (targetZoom - sourceZoom));

    Scene scene = getScene();
    JComponent view = scene.getView ();

    if (view != null) {
        Point viewLocation = view.getVisibleRect ().getLocation();
        Dimension viewSize = view.getVisibleRect ().getSize();
        Point oldCenter = scene.convertSceneToView (center);

        ((DependencyGraphScene)scene).setMyZoomFactor (nextZoom);
        scene.validate (); // HINT - forcing to change preferred size of the JComponent view

        Point newCenter = scene.convertSceneToView (center);
        Rectangle viewBounds = view.getVisibleRect();
        Point visibleCenter = new Point((int)viewBounds.getCenterX(), (int)viewBounds.getCenterY());
        newCenter.x += Math.round((newCenter.x - visibleCenter.x) * progress);
        newCenter.y += Math.round((newCenter.y - visibleCenter.y) * progress);

        view.scrollRectToVisible (new Rectangle (
                newCenter.x - oldCenter.x + viewLocation.x,
                newCenter.y - oldCenter.y + viewLocation.y,
                viewSize.width,
                viewSize.height
        ));
    } else {
        ((DependencyGraphScene)scene).setMyZoomFactor (nextZoom);
    }
}
 
Example 3
Project: MaxSim   File: ExtendedSatelliteComponent.java   View Source Code Vote up 5 votes
private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
        return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

    this.repaint();
}
 
Example 4
Project: MaxSim   File: CustomizablePanAction.java   View Source Code Vote up 5 votes
private boolean pan (Widget widget, Point newLocation) {
    if (scrollPane == null  ||  scene != widget.getScene ())
        return false;
    newLocation = scene.convertSceneToView (widget.convertLocalToScene (newLocation));
    SwingUtilities.convertPointToScreen (newLocation, scene.getView ());
    JComponent view = scene.getView ();
    Rectangle rectangle = view.getVisibleRect ();
    rectangle.x += lastLocation.x - newLocation.x;
    rectangle.y += lastLocation.y - newLocation.y;
    view.scrollRectToVisible (rectangle);
    lastLocation = newLocation;
    return true;
}
 
Example 5
Project: MaxSim   File: ExtendedSatelliteComponent.java   View Source Code Vote up 5 votes
private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
        return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

}
 
Example 6
Project: MaxSim   File: CustomizablePanWidgetAction.java   View Source Code Vote up 5 votes
private boolean pan (Widget widget, Point newLocation) {
    if (scrollPane == null  ||  scene != widget.getScene ())
        return false;
    newLocation = scene.convertSceneToView (widget.convertLocalToScene (newLocation));
    SwingUtilities.convertPointToScreen (newLocation, scene.getView ());
    JComponent view = scene.getView ();
    Rectangle rectangle = view.getVisibleRect ();
    rectangle.x += lastLocation.x - newLocation.x;
    rectangle.y += lastLocation.y - newLocation.y;
    view.scrollRectToVisible (rectangle);
    lastLocation = newLocation;
    return true;
}
 
Example 7
Project: MaxSim   File: ExtendedSatelliteComponent.java   View Source Code Vote up 5 votes
private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
        return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

}
 
Example 8
Project: MaxSim   File: CfgScene.java   View Source Code Vote up 5 votes
private void centerSelection() {
    Point sceneCenter = null;
    Collection<CfgNode> nodes = this.selectedNodes;
    if (nodes.size() == 0) {
        nodes = this.getNodes();
    }

    for (CfgNode n : nodes) {
        if (sceneCenter == null) {
            sceneCenter = this.findWidget(n).getLocation();
            continue;
        }
        Point location = this.findWidget(n).getLocation();
        sceneCenter.x = (location.x + sceneCenter.x) / 2;
        sceneCenter.y = (location.y + sceneCenter.y) / 2;
    }

    JComponent view = this.getView();
    if (view != null) {
        Rectangle viewBounds = view.getVisibleRect();

        Point viewCenter = this.convertSceneToView(sceneCenter);

        view.scrollRectToVisible(new Rectangle(
                viewCenter.x - viewBounds.width / 2,
                viewCenter.y - viewBounds.height / 2,
                viewBounds.width,
                viewBounds.height));
    }
}
 
Example 9
Project: openjdk-jdk10   File: ExtendedSatelliteComponent.java   View Source Code Vote up 5 votes
private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
        return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

    this.repaint();
}
 
Example 10
Project: openjdk-jdk10   File: CustomizablePanAction.java   View Source Code Vote up 5 votes
private boolean pan (Widget widget, Point newLocation) {
    if (scrollPane == null  ||  scene != widget.getScene ())
        return false;
    newLocation = scene.convertSceneToView (widget.convertLocalToScene (newLocation));
    SwingUtilities.convertPointToScreen (newLocation, scene.getView ());
    JComponent view = scene.getView ();
    Rectangle rectangle = view.getVisibleRect ();
    rectangle.x += lastLocation.x - newLocation.x;
    rectangle.y += lastLocation.y - newLocation.y;
    view.scrollRectToVisible (rectangle);
    lastLocation = newLocation;
    return true;
}
 
Example 11
Project: openjdk-jdk10   File: ExtendedSatelliteComponent.java   View Source Code Vote up 5 votes
private void moveVisibleRect(Point center) {
    JComponent component = scene.getView();
    if (component == null) {
        return;
    }
    double zoomFactor = scene.getZoomFactor();
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;

    int cx = (int) ((double) (center.x - vx) / scale * zoomFactor);
    int cy = (int) ((double) (center.y - vy) / scale * zoomFactor);

    Rectangle visibleRect = component.getVisibleRect();
    visibleRect.x = cx - visibleRect.width / 2;
    visibleRect.y = cy - visibleRect.height / 2;
    component.scrollRectToVisible(visibleRect);

}
 
Example 12
Project: MaxSim   File: ExtendedSatelliteComponent.java   View Source Code Vote up 4 votes
@Override
public void paint(Graphics g) {
    Graphics2D gr = (Graphics2D) g;
    super.paint(g);
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;


    if (image == null || vw != imageWidth || vh != imageHeight) {

        imageWidth = vw;
        imageHeight = vh;
        image = this.createImage(imageWidth, imageHeight);
        Graphics2D ig = (Graphics2D) image.getGraphics();
        ig.scale(scale, scale);
 double oldFactor = scene.getZoomFactor();
 scene.setZoomFactor(scale);
        scene.paint(ig);
 scene.setZoomFactor(oldFactor);
    }

    gr.drawImage(image, vx, vy, this);

    JComponent component = scene.getView();
    double zoomFactor = scene.getZoomFactor();
    Rectangle viewRectangle = component != null ? component.getVisibleRect() : null;
    if (viewRectangle != null) {
        Rectangle window = new Rectangle(
                (int) ((double) viewRectangle.x * scale / zoomFactor),
                (int) ((double) viewRectangle.y * scale / zoomFactor),
                (int) ((double) viewRectangle.width * scale / zoomFactor),
                (int) ((double) viewRectangle.height * scale / zoomFactor));
        window.translate(vx, vy);
        gr.setColor(new Color(200, 200, 200, 128));
        gr.fill(window);
        gr.setColor(Color.BLACK);
        gr.drawRect(window.x, window.y, window.width - 1, window.height - 1);
    }
}
 
Example 13
Project: MaxSim   File: ExtendedSatelliteComponent.java   View Source Code Vote up 4 votes
@Override
public void paint(Graphics g) {
    Graphics2D gr = (Graphics2D) g;
    super.paint(g);
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;


    if (image == null || vw != imageWidth || vh != imageHeight) {

        imageWidth = vw;
        imageHeight = vh;
        image = this.createImage(imageWidth, imageHeight);
        Graphics2D ig = (Graphics2D) image.getGraphics();
        ig.scale(scale, scale);
        scene.paint(ig);
    }

    gr.drawImage(image, vx, vy, this);

    JComponent component = scene.getView();
    double zoomFactor = scene.getZoomFactor();
    Rectangle viewRectangle = component != null ? component.getVisibleRect() : null;
    if (viewRectangle != null) {
        Rectangle window = new Rectangle(
                (int) ((double) viewRectangle.x * scale / zoomFactor),
                (int) ((double) viewRectangle.y * scale / zoomFactor),
                (int) ((double) viewRectangle.width * scale / zoomFactor),
                (int) ((double) viewRectangle.height * scale / zoomFactor));
        window.translate(vx, vy);
        gr.setColor(new Color(200, 200, 200, 128));
        gr.fill(window);
        gr.setColor(Color.BLACK);
        gr.drawRect(window.x, window.y, window.width - 1, window.height - 1);
    }
}
 
Example 14
Project: MaxSim   File: ExtendedSatelliteComponent.java   View Source Code Vote up 4 votes
@Override
public void paint(Graphics g) {
    Graphics2D gr = (Graphics2D) g;
    super.paint(g);
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;


    if (image == null || vw != imageWidth || vh != imageHeight) {

        imageWidth = vw;
        imageHeight = vh;
        image = this.createImage(imageWidth, imageHeight);
        Graphics2D ig = (Graphics2D) image.getGraphics();
        ig.scale(scale, scale);
        scene.paint(ig);
    }

    gr.drawImage(image, vx, vy, this);

    JComponent component = scene.getView();
    double zoomFactor = scene.getZoomFactor();
    Rectangle viewRectangle = component != null ? component.getVisibleRect() : null;
    if (viewRectangle != null) {
        Rectangle window = new Rectangle(
                (int) ((double) viewRectangle.x * scale / zoomFactor),
                (int) ((double) viewRectangle.y * scale / zoomFactor),
                (int) ((double) viewRectangle.width * scale / zoomFactor),
                (int) ((double) viewRectangle.height * scale / zoomFactor));
        window.translate(vx, vy);
        gr.setColor(new Color(200, 200, 200, 128));
        gr.fill(window);
        gr.setColor(Color.BLACK);
        gr.drawRect(window.x, window.y, window.width - 1, window.height - 1);
    }
}
 
Example 15
Project: openjdk-jdk10   File: ExtendedSatelliteComponent.java   View Source Code Vote up 4 votes
@Override
public void paint(Graphics g) {
    Graphics2D gr = (Graphics2D) g;
    super.paint(g);
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;


    if (image == null || vw != imageWidth || vh != imageHeight) {
        imageWidth = vw;
        imageHeight = vh;
        image = this.createImage(imageWidth, imageHeight);
        Graphics2D ig = (Graphics2D) image.getGraphics();
        ig.scale(scale, scale);
        double oldFactor = scene.getZoomFactor();
        scene.setZoomFactor(scale);
        scene.paint(ig);
        scene.setZoomFactor(oldFactor);
    }

    gr.drawImage(image, vx, vy, this);

    JComponent component = scene.getView();
    double zoomFactor = scene.getZoomFactor();
    Rectangle viewRectangle = component != null ? component.getVisibleRect() : null;
    if (viewRectangle != null) {
        Rectangle window = new Rectangle(
                (int) ((double) viewRectangle.x * scale / zoomFactor),
                (int) ((double) viewRectangle.y * scale / zoomFactor),
                (int) ((double) viewRectangle.width * scale / zoomFactor),
                (int) ((double) viewRectangle.height * scale / zoomFactor));
        window.translate(vx, vy);
        gr.setColor(new Color(200, 200, 200, 128));
        gr.fill(window);
        gr.setColor(Color.BLACK);
        gr.drawRect(window.x, window.y, window.width - 1, window.height - 1);
    }
}
 
Example 16
Project: openjdk-jdk10   File: ExtendedSatelliteComponent.java   View Source Code Vote up 4 votes
@Override
public void paint(Graphics g) {
    Graphics2D gr = (Graphics2D) g;
    super.paint(g);
    Rectangle bounds = scene.getBounds();
    Dimension size = getSize();

    double sx = bounds.width > 0 ? (double) size.width / bounds.width : 0.0;
    double sy = bounds.width > 0 ? (double) size.height / bounds.height : 0.0;
    double scale = Math.min(sx, sy);

    int vw = (int) (scale * bounds.width);
    int vh = (int) (scale * bounds.height);
    int vx = (size.width - vw) / 2;
    int vy = (size.height - vh) / 2;


    if (image == null || vw != imageWidth || vh != imageHeight) {

        imageWidth = vw;
        imageHeight = vh;
        image = this.createImage(imageWidth, imageHeight);
        Graphics2D ig = (Graphics2D) image.getGraphics();
        ig.scale(scale, scale);
        scene.paint(ig);
    }

    gr.drawImage(image, vx, vy, this);

    JComponent component = scene.getView();
    double zoomFactor = scene.getZoomFactor();
    Rectangle viewRectangle = component != null ? component.getVisibleRect() : null;
    if (viewRectangle != null) {
        Rectangle window = new Rectangle(
                (int) ((double) viewRectangle.x * scale / zoomFactor),
                (int) ((double) viewRectangle.y * scale / zoomFactor),
                (int) ((double) viewRectangle.width * scale / zoomFactor),
                (int) ((double) viewRectangle.height * scale / zoomFactor));
        window.translate(vx, vy);
        gr.setColor(new Color(200, 200, 200, 128));
        gr.fill(window);
        gr.setColor(Color.BLACK);
        gr.drawRect(window.x, window.y, window.width - 1, window.height - 1);
    }
}