Java Code Examples for org.netbeans.api.visual.widget.Scene#validate()

The following examples show how to use org.netbeans.api.visual.widget.Scene#validate() . 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 File: OffscreenRenderingTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private BufferedImage dumpSceneOffscreenRendering (Scene scene) {
    // validate the scene with a off-screen graphics
    BufferedImage emptyImage = new BufferedImage (1, 1, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D emptyGraphics = emptyImage.createGraphics ();
    scene.validate (emptyGraphics);
    emptyGraphics.dispose ();

    // now the scene is calculated using the emptyGraphics, all widgets should be layout and scene has its size resolved
    // paint the scene with a off-screen graphics
    Rectangle viewBounds = scene.convertSceneToView (scene.getBounds ());
    BufferedImage image = new BufferedImage (viewBounds.width, viewBounds.height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D graphics = image.createGraphics ();
    double zoomFactor = scene.getZoomFactor ();
    graphics.scale (zoomFactor, zoomFactor);
    scene.paint (graphics);
    graphics.dispose ();

    return image;
}
 
Example 2
Source File: CenteredZoomAnimator.java    From netbeans with 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
Source File: AnchorNotificationTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testNotify () {
    StringBuffer log = new StringBuffer ();
    Scene scene = new Scene ();

    Widget w = new Widget (scene);
    scene.addChild (w);

    ConnectionWidget c = new ConnectionWidget (scene);
    scene.addChild (c);
    TestAnchor testAnchor = new TestAnchor (w, log);
    c.setSourceAnchor (testAnchor);
    c.setTargetAnchor (testAnchor);

    JFrame frame = showFrame (scene);

    c.setSourceAnchor (null);
    c.setTargetAnchor (null);
    scene.validate ();

    frame.setVisible (false);
    frame.dispose ();

    assertEquals (log.toString (),
            "notifyEntryAdded\n" +
            "notifyUsed\n" +
            "notifyRevalidate\n" +
            "notifyEntryAdded\n" +
            "notifyRevalidate\n" +
            "notifyRevalidate\n" +
            "compute\n" +
            "compute\n" +
            "notifyEntryRemoved\n" +
            "notifyRevalidate\n" +
            "notifyEntryRemoved\n" +
            "notifyUnused\n" +
            "notifyRevalidate\n"
            );
}
 
Example 4
Source File: MouseCenteredZoomAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public State mouseWheelMoved (Widget widget, WidgetMouseWheelEvent event) {
    Scene scene = widget.getScene ();

    int modifiers = scene.getInputBindings ().getZoomActionModifiers ();
    if ((event.getModifiers () & modifiers) != modifiers)
        return State.REJECTED;

    int amount = event.getWheelRotation ();

    double scale = 1.0;
    while (amount > 0) {
        scale /= zoomMultiplier;
        amount --;
    }
    while (amount < 0) {
        scale *= zoomMultiplier;
        amount ++;
    }

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

        Point center = widget.convertLocalToScene (event.getPoint ());
        Point mouseLocation = scene.convertSceneToView (center);

        scene.setZoomFactor (scale * scene.getZoomFactor ());
        scene.validate (); // HINT - forcing to change preferred size of the JComponent view

        center = scene.convertSceneToView (center);

        view.scrollRectToVisible (new Rectangle (
                center.x - (mouseLocation.x - viewBounds.x),
                center.y - (mouseLocation.y - viewBounds.y),
                viewBounds.width,
                viewBounds.height
        ));
    } else
        scene.setZoomFactor (scale * scene.getZoomFactor ());

    return State.CONSUMED;
}
 
Example 5
Source File: CenteredZoomAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public State mouseWheelMoved (Widget widget, WidgetMouseWheelEvent event) {
    Scene scene = widget.getScene ();

    int modifiers = scene.getInputBindings ().getZoomActionModifiers ();
    if ((event.getModifiers () & modifiers) != modifiers)
        return State.REJECTED;

    int amount = event.getWheelRotation ();

    double scale = 1.0;
    while (amount > 0) {
        scale /= zoomMultiplier;
        amount --;
    }
    while (amount < 0) {
        scale *= zoomMultiplier;
        amount ++;
    }

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

        Point center = GeomUtil.center (viewBounds);
        center = scene.convertViewToScene (center);

        scene.setZoomFactor (scale * scene.getZoomFactor ());
        scene.validate (); // HINT - forcing to change preferred size of the JComponent view
        
        center = scene.convertSceneToView (center);

        view.scrollRectToVisible (new Rectangle (
            center.x - viewBounds.width / 2,
            center.y - viewBounds.height / 2,
            viewBounds.width,
            viewBounds.height
        ));
    } else
        scene.setZoomFactor (scale * scene.getZoomFactor ());

    return State.CONSUMED;
}
 
Example 6
Source File: SceneExporter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Takes the Scene and writes an image file according to the constraints 
 * defined by the caller. This returns a BufferedImage of the Scene even if 
 * the file can not be written.
 * @param scene The Scene to be exported as an image.
 * @param file The file used to store the exported image. If null, then it is 
 * assumed that the raw image is to be returned only and not written to a file.
 * @param SceneExporter.ImageType The image type to be exported for the image map.
 * @param SceneExporter.ZoomType Defines the strategy
 * by which to set the exporting scale factor. Note that certain parameters are
 * nullified by the choice of ZoomType. For instance, if ZoomType.CUSTOM_SIZE is
 * not chosen, then the width and height parameters are not used.
 * @param visibleAreaOnly Eliminates all zoom features. If true, the exported
 * image will be a created from the visible area of the scene.
 * @param selectedOnly Create an image including only the objects selected on
 * the scene. Note that this feature requires that the scene is an instance of
 * an ObjectScene since it is the implementation that allows for object selection.
 * @param quality And integer value between 0-100. This is for JPG images only. Parameter is not used if
 * an image type other than jpg is selected.
 * @param width Directly sets the horizontal dimension of the exported image.
 * This is only used when the zoomType is ZoomType.CUSTOM_SIZE
 * @param height Directly sets the vertical dimension of the exported image.
 * This is only used when the zoomType is ZoomType.CUSTOM_SIZE.
 * @return image The raw image that was written to the file.
 * @throws java.io.IOException If for some reason the file cannot be written, 
 * an IOExeption will be thrown.
 */
public static BufferedImage createImage(Scene scene, File file, ImageType imageType, ZoomType zoomType,
        boolean visibleAreaOnly,
        boolean selectedOnly,
        int quality,
        int width,
        int height) throws IOException {

    if (scene == null) {
        return null;
    }
    if (!scene.isValidated()) {
        if (scene.getView() != null) {
            scene.validate();
        } else {
            BufferedImage emptyImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D emptyGraphics = emptyImage.createGraphics();
            scene.validate(emptyGraphics);
        }
    }
    Scene2Image s2i = new Scene2Image(scene, file);
    BufferedImage image = s2i.createImage(imageType, zoomType, visibleAreaOnly, selectedOnly, quality, width, height, false);

    return image;
}