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

The following examples show how to use javax.swing.JComponent#getVisibleRect() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: gcs   File: UIUtilities.java    License: Mozilla Public License 2.0 6 votes vote down vote up
/**
 * @param component The component to generate an image of.
 * @return The newly created image.
 */
public static Img getImage(JComponent component) {
    Img offscreen = null;
    synchronized (component.getTreeLock()) {
        Graphics2D gc = null;
        try {
            Rectangle bounds = component.getVisibleRect();
            offscreen = Img.create(component.getGraphicsConfiguration(), bounds.width, bounds.height, Transparency.TRANSLUCENT);
            gc = offscreen.getGraphics();
            gc.translate(-bounds.x, -bounds.y);
            component.paint(gc);
        } catch (Exception exception) {
            Log.error(exception);
        } finally {
            if (gc != null) {
                gc.dispose();
            }
        }
    }
    return offscreen;
}
 
Example 2
Source Project: netbeans   File: CenteredZoomAnimator.java    License: Apache License 2.0 5 votes vote down vote up
@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
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
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
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
private static void showPopupNotification(String message) {
  JComponent component = WindowManager.getInstance().findVisibleFrame().getRootPane();
  if (component == null) {
    return;
  }
  Rectangle rect = component.getVisibleRect();
  JBPopupFactory.getInstance()
      .createHtmlTextBalloonBuilder(
          message,
          MessageType.WARNING,
          new HyperlinkAdapter() {
            @Override
            protected void hyperlinkActivated(HyperlinkEvent e) {
              PluginUtils.installOrEnablePlugin(JUNIT_PLUGIN_ID);
            }
          })
      .setFadeoutTime(-1)
      .setHideOnLinkClick(true)
      .setHideOnFrameResize(false)
      .setHideOnClickOutside(false)
      .setHideOnKeyOutside(false)
      .setDisposable(ApplicationManager.getApplication())
      .createBalloon()
      .show(
          new RelativePoint(component, new Point(rect.x + 30, rect.y + rect.height - 10)),
          Balloon.Position.above);
}
 
Example 7
Source Project: RipplePower   File: UIRes.java    License: Apache License 2.0 5 votes vote down vote up
public static Rectangle getVisibleBoundsOnScreen(JComponent component) {
	Rectangle visibleRect = component.getVisibleRect();
	Point onScreen = visibleRect.getLocation();
	SwingUtilities.convertPointToScreen(onScreen, component);
	visibleRect.setLocation(onScreen);
	return visibleRect;
}
 
Example 8
Source Project: netbeans   File: ZoomManager.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Set the zoom factor for the Scene mananged by this ZoomManager
 * instance. The value represents a percentage (e.g. 100%) and
 * must be a positive number. Any value outside of the defined
 * range (<tt>MIN_ZOOM_PERCENT</tt> and <tt>MAX_ZOOM_PERCENT</tt>)
 * will be forced into that range.
 *
 * @param  percent  the percent value (e.g. 50 for half-size,
 *                  200 for double-size).
 * @param  center   the point at which to zoom in and keep centered.
 */
public void setZoom(int percent, Point center) {
    if (percent < MIN_ZOOM_PERCENT) {
        percent = MIN_ZOOM_PERCENT;
    } else if (percent > MAX_ZOOM_PERCENT) {
        percent = MAX_ZOOM_PERCENT;
    }

    // Find the current center point prior to zooming.
    Point sceneCenter = scene.convertViewToScene(center);
    zoomPercentage = percent;
    // Convert the percent value to the zoom factor Scene is expecting
    // (a double that acts as the multiplier to the component sizes and
    // locations, such that 0.5 is 50%, 1.0 is 100%, and 2.0 is 200%.
    double factor = ((double) percent) / 100.0d;
    scene.setZoomFactor(factor);
    // Setting the zoom factor alone is not enough, must force
    // validation and repainting of the scene for it to work.
    scene.validate();
    scene.repaint();

    // Find the new center point and scroll the view after zooming.
    Point newViewCenter = scene.convertSceneToView(sceneCenter);
    JComponent view = scene.getView();
    Rectangle visRect = view.getVisibleRect();
    visRect.x = newViewCenter.x - (center.x - visRect.x);
    visRect.y = newViewCenter.y - (center.y - visRect.y);
    Dimension viewSize = view.getSize();
    if (visRect.x + visRect.width > viewSize.width) {
        visRect.x = viewSize.width - visRect.width;
    }
    if (visRect.y + visRect.height > viewSize.height) {
        visRect.y = viewSize.height - visRect.height;
    }
    if (visRect.x < 0) {
        visRect.x = 0;
    }
    if (visRect.y < 0) {
        visRect.y = 0;
    }
    view.scrollRectToVisible(visRect);
    view.revalidate();
    view.repaint();

    // Notify registered listeners so they may update their state.
    fireZoomEvent(percent);
}
 
Example 9
@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 10
@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);
    }
}