Skip to content

JavaFX Santa’s Hat

Happy holidays in a JavaFX Style.

Try online

It’s a application to put a Santa’s hat on a picture from web. To test it obviously I used my picture with John Hall “Maddog”, the closest person I know to Santa Claus. ;D

I tried the approach from Chris Campbell’s Effect Playground application but I needed get both photo image and hat at the same time. In this application I used the Jean-Francois Screenshot Maker approach, taking a screenshot of the desired area. Maybe not the best solution, but it works very well. I also used his Screencapture.java and Util.java codes.

package santahat;

import javafx.ext.swing.SwingTextField;
import javafx.scene.CustomNode;
import javafx.scene.input.MouseEvent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.ext.swing.SwingButton;

var imgview = ImageView {
   image: Image {
      url: "{__DIR__}help.png"
   }
}

var txtfield:SwingTextField = SwingTextField {
   columns: 45
   text: "your image url"
   editable: true
   action: function(){
      println(txtfield.text);
      imgview.image = Image {
         url: txtfield.text;
      }
   }
};

var santahat:ImageView = ImageView {
   x: 240 y: -5
   var startX = 0.0
   var startY = 0.0
   var   zoom = 1.0
   var  angle = 0.0

   scaleX: bind zoom
   scaleY: bind zoom
   rotate: bind angle

   onMousePressed: function( e: MouseEvent ):Void {
      startX = e.sceneX - santahat.translateX;
      startY = e.sceneY - santahat.translateY;
   }

   onMouseDragged: function( e: MouseEvent ):Void {
      santahat.translateX = e.sceneX - startX;
      santahat.translateY = e.sceneY - startY;
   }

   onMouseWheelMoved: function( e: MouseEvent ):Void {
      if(e.controlDown) {
         angle += 
         e.wheelRotation * 10;
      } else {
         zoom += 
         e.wheelRotation / 20;
      }
   }

   image: Image {
      url: "{__DIR__}santahat.png"
   }
}

// Based on the Jean's ScreenshotMaker
// http://javafx.com/samples/ScreenshotMaker/index.html
var stage:Stage = Stage {
   title: "Santa Hat"
   width: 510 height: 480
   scene: Scene {
      content: [
         VBox {
            content: [ txtfield, imgview,
               SwingButton {
                  text: "Save"
                  action: function() {
                     // Ugly. See the entire source at through the link in the blog post
                  }
               }]
         }, santahat]
   }
}

Downloads:

Published inenglish

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *