Mona Lisa Puzzle (Part 1)

If Mona Lisa was a puzzle...

...this would define her:

class PuzzleScene extends ObjectScene {

    public PuzzleScene() {

        LayerWidget layerWidget = new LayerWidget(this);

        ImageIcon mona = ImageUtilities.loadImageIcon("org/mona/puzzle/board/puzzle.jpg", true);
        Image source = mona.getImage();
        int width = mona.getIconWidth();
        int height = mona.getIconHeight();

        Random generator = new Random();

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                Image image = Toolkit.getDefaultToolkit().createImage(
                        new FilteredImageSource(source.getSource(),
                        new CropImageFilter(j \* width / 3, i \* height / 4, (width / 3) + 1, height / 4)));
                layerWidget.addChild(new PuzzleWidget(
                        this, image, generator.nextInt(600), generator.nextInt(300)));
            }
        }

        addChild(layerWidget);

    }

    class PuzzleWidget extends IconNodeWidget {

        public PuzzleWidget(Scene scene, Image image, int x, int y) {
            super(scene);
            setImage(image);
            //In the end, we need our own move action, which will
            //check whether the piece, when dropped, is in the right place,
            //for the moment, just use the default move action:
            getActions().addAction(ActionFactory.createMoveAction());
            setPreferredLocation(new Point(x, y));
        }

    }
    
}

With thanks to this and this article.

Comments:

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
« August 2015
SunMonTueWedThuFriSat
      
8
9
19
22
23
29
31     
Today