This puzzler is mainly directed at the students of the free JavaFX with Passion! course, and it contains concepts that have been covered in the course so far. The first student in that course who submits a correct solution will be awarded a free token for the Pro JavaFX early access eBook. If you're not enrolled in that course, feel free to submit a solution to this puzzler as well, because I'll give a free eBook for the first correct solution from a non-JavaFX-with-Passion student as well.
This first puzzler is pretty simple, but future puzzlers will be increasingly difficult. Here are the requirements:
- As shown in the screenshot above, the JavaFX program must have a slider and some text that rotates when the slider is adjusted.
- The rotation will be from 0 to 360 degrees, depending on the position of the slider.
- The text will consist of the string "Angle: " followed by the angle of rotation.
- The text must have a bold font weight, and be blue in color.
- The point at which the text rotates must be at the center point (both horizontally and vertically) of the text.
To enter your solution, submit a comment to this blog post that contains the source code pasted into it. To validate that the code meets the requirements, I will compile and run it with JavaFX SDK 1.1. It must run without any code editing, and must meet the above requirements.
I will feature the two winners and their solutions in an upcoming blog post. Let the games begin!
Regards,
Jim Weaver
JavaFXpert.com
/* JavaFX with Passion! Puzzler #1 */
import javafx.stage.*;
import javafx.scene.Scene;
import javafx.scene.text.*;
import javafx.scene.paint.Color;
import javafx.scene.layout.VBox;
import javafx.ext.swing.SwingSlider;
var width: Number = 320;
var height: Number = 320;
var slider: SwingSlider = SwingSlider {
minimum: 0
maximum: 360
value: 0
vertical: false
translateX: bind (width - slider.boundsInLocal.width) / 2
}
var text: Text = Text {
font: Font { name: "Arial Bold", size: 32 }
content: bind "Angle: {slider.value}"
rotate: bind slider.value
fill: Color.BLUE
translateX: bind (width - text.boundsInLocal.width) / 2
translateY: bind height / 2
}
Stage {
width: bind width with inverse
height: bind height with inverse
title: "Rotate Puzzler"
scene: Scene {
content: VBox {
content: [ slider, text ]
}
}
}
Posted by: acw1668 from HK | March 23, 2009 at 10:55 PM
it's look like my previous post reformatted by your blog engine, so "== 10 and num < 100)
return "Angle: 0{num}"
else
return "Angle: {num}";
}
def text:Text = Text {
font: font
fill: Color.BLUE
translateX: bind tx
translateY: bind ty
style: "font-weight:bold"
rotate: bind angle
content: bind formatString(angle)
}
def scene:Scene = Scene {
content: [ slider, text ] }
Stage {
title: "RotSlider"
width: bind width with inverse
height: bind height with inverse
scene: scene
}
Posted by: hakim | March 23, 2009 at 09:45 PM
import javafx.ext.swing.SwingSlider;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.transform.Rotate;
/**
* @author Hakim
*/
def font = Font{name:"Arial", size: 40, embolden:true}
def angle = bind slider.value;
var width:Number = 350;
var height:Number = 300;
var pivoty:Number = bind text.boundsInParent.height/2;
var tx = bind (width - text.boundsInLocal.width)/2;
var ty = bind (height - text.boundsInLocal.height)/2;
def slider = SwingSlider {
width: bind width*2/3
translateX: bind width/6
minimum: 0
maximum: 360
value: 0
vertical: false
}
function formatString(num:Integer):String{
if (num = 10 and num < 100)
return "Angle: 0{num}"
else
return "Angle: {num}";
}
def text:Text = Text {
font: font
fill: Color.BLUE
translateX: bind tx
translateY: bind ty
style: "font-weight:bold"
rotate: bind angle
content: bind formatString(angle)
}
def scene:Scene = Scene {
content: [ slider, text ] }
Stage {
title: "RotSlider"
width: bind width with inverse
height: bind height with inverse
scene: scene
}
Posted by: hakim | March 23, 2009 at 09:19 PM
/*
* Main.fx
*
* Created on 23/03/2009, 21:18:42 (BRAZIL TIME) on Ubuntu :D
*/
package anglefx;
import javafx.ext.swing.SwingSlider;
import javafx.scene.paint.Color;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* @author bulinha
*/
var angle = 0;
Stage {
title: "Rotate Puzzler"
width: 250
height: 250
scene: Scene {
content: [
SwingSlider {
minimum: 0
maximum: 360
value: bind angle with inverse
vertical: false
width: 200;
translateX: 25
},
Text {
font: Font {
size: 30
embolden: true
}
fill: Color.BLUE;
translateX: 50,
translateY: 120,
rotate: bind angle
content: bind "angle: {angle}"
}
]
}
}
Posted by: Luis Henrique M. Carvalho | March 23, 2009 at 08:32 PM