Rotating a NetBeans Visual Library Widget

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!

Comments:

Nice, I'm trying this with a LabelWidget, not working.... ???

Posted by guest on June 18, 2012 at 01:13 PM PDT #

Hi Geertjan,
Useful post, as always.

I'd like to make a remark, it's a little bit off topic though.
I've noticed that adding a widget from palette by drag and drop is the most popular way of doing this. Also I remember your blog entry describing adding widget by double click on scene (Plotter app).
Could you please describe how could it (adding widget to scene) be accomplished by e.g. clicking on a button, or by choosing menu item.

Posted by netbeans user on December 16, 2012 at 01:04 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today