Rotating a NetBeans Visual Library Widget

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Thu, 14 Jun 2012 07:00:00 +0000 Indexed on 2012/06/15 15:24 UTC
Read the original article Hit count: 242

Filed under:

Trying to create a widget which, when clicked, rotates slightly further on each subsequent click:

Above, the bird where the mouse is visible has been clicked a few times and so has rotated a bit further on each click.

The code isn't quite right yet and I'm hoping someone will take this code, try it out, and help with a nice solution!

public class BirdScene extends Scene {

    public BirdScene() {
        addChild(new LayerWidget(this));
        getActions().addAction(ActionFactory.createAcceptAction(new AcceptProvider() {
            public ConnectorState isAcceptable(Widget widget, Point point, Transferable transferable) {
                Image dragImage = getImageFromTransferable(transferable);
                if (dragImage != null) {
                    JComponent view = getView();
                    Graphics2D g2 = (Graphics2D) view.getGraphics();
                    Rectangle visRect = view.getVisibleRect();
                    view.paintImmediately(visRect.x, visRect.y, visRect.width, visRect.height);
                    g2.drawImage(dragImage,
                            AffineTransform.getTranslateInstance(point.getLocation().getX(),
                            point.getLocation().getY()),
                            null);
                    return ConnectorState.ACCEPT;
                } else {
                    return ConnectorState.REJECT;
                }
            }
            public void accept(Widget widget, final Point point, Transferable transferable) {
                addChild(new BirdWidget(getScene(), getImageFromTransferable(transferable), point));
            }
        }));
    }

    private Image getImageFromTransferable(Transferable transferable) {
        Object o = null;
        try {
            o = transferable.getTransferData(DataFlavor.imageFlavor);
        } catch (IOException ex) {
        } catch (UnsupportedFlavorException ex) {
        }
        return o instanceof Image ? (Image) o : null;
    }

    private class BirdWidget extends IconNodeWidget {

        private int theta = 0;

        public BirdWidget(Scene scene, Image imageFromTransferable, Point point) {
            super(scene);
            setImage(imageFromTransferable);
            setPreferredLocation(point);
            setCheckClipping(true);
            getActions().addAction(ActionFactory.createMoveAction());
            getActions().addAction(ActionFactory.createSelectAction(new SelectProvider() {
                public boolean isAimingAllowed(Widget widget, Point localLocation, boolean invertSelection) {
                    return true;
                }
                public boolean isSelectionAllowed(Widget widget, Point localLocation, boolean invertSelection) {
                    return true;
                }
                public void select(final Widget widget, Point localLocation, boolean invertSelection) {
                    theta = (theta + 100) % 360;
                    repaint();
                    getScene().validate();
                }
            }));
        }

        @Override
        public void paintWidget() {
            final Image image = getImageWidget().getImage();
            Graphics2D g = getGraphics();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Rectangle bounds = getClientArea();
            AffineTransform newXform = g.getTransform();
            int xRot = image.getWidth(null) / 2;
            int yRot = image.getWidth(null) / 2;
            newXform.rotate(theta * Math.PI / 180, xRot, yRot);
            g.setTransform(newXform);
            g.drawImage(image, bounds.x, bounds.y, null);
        }
 
    }
 
}

The problem relates to refreshing the scene after the rotation. But it would help if someone would just take the code above, add it to their own application, try it out, see the problem for yourself, and develop it a bit further!

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE