Java, JavaFX, Groovy, Grails …
JavaFX Magic 8 Ball
Originally posted by me on ericonjavafx.com
package magicanswers;
import javafx.lang.FX;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import magicanswers.Oracle;
var oracle = Oracle { }
var mediaUrl:String = "http://ericonjavafx.com/Magic8Ball/Opium.wav";
if(FX.getArgument("mediaURL") != null) {
mediaUrl = FX.getArgument("mediaURL").toString();
}
var music:MediaPlayer = MediaPlayer {
volume:.4;
media: Media {
source: mediaUrl
}
autoPlay: true
repeatCount:MediaPlayer.REPEAT_FOREVER
}
music.paused;
var stop_music:ImageView = ImageView {
opacity: 1.0
visible: true
x: 0
y: 675
fitWidth:100
preserveRatio: true
blocksMouse:true
image: Image {
url: "{__DIR__}Sound.png"
},
onMousePressed: function(e) {
if(music.paused){
music.play();
}else {
music.pause();
}
}
};
var scene:Scene;
Stage {
title: "Magic Eight Ball"
scene:
scene = Scene {
height: 700
width: 900
fill:null;
content: [
Group{
content:[ ImageView {
opacity: 1.0
visible: true
image: Image {
url: "{__DIR__}Background.png"
},
fitWidth:bind scene.width;
fitHeight:bind scene.height;
},oracle,
stop_music
]
}
]
}
};
package magicanswers;
import java.util.Random;
import javafx.animation.*;
import javafx.scene.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
public class MessageObject extends CustomNode {
def rnd : Random = new Random();
public var content:String;
var image:Image = Image {
url: "{__DIR__}Triangle.png"
}
var imageView=ImageView {
image: bind image
visible: true
cache:true}
public override function create(): Node {
return Group {
content: [
imageView,
Text{
translateX: bind imageView.translateX + 31
translateY: bind imageView.translateY + 85
content:content;
font: Font {
size: 15
}
wrappingWidth: 100
textAlignment: TextAlignment.LEFT
fill:Color.LIGHTBLUE;
},
]
cache:true;
}
}
public var fade = Timeline {
def sx1 =.9; def sx2 = .5;
def sy1 =.9; def sy2 = .5;
def tx1 = 437; def tx2 = 437 ;
def ty1 = 300; def ty2 = 300;
def ro1 = this.rotate; def ro2 = rnd.nextDouble() * 400;
keyFrames: [
at(0s) {
this.opacity => .1 tween Interpolator.LINEAR;
this.scaleX => sx2 tween Interpolator.LINEAR;
this.scaleY => sy2 tween Interpolator.LINEAR;
this.translateX => tx2 tween Interpolator.LINEAR;
this.translateY => ty2 tween Interpolator.LINEAR;
this.rotate => ro2 tween Interpolator.LINEAR;
},
at(2s) {
this.opacity =>.5 tween Interpolator.LINEAR;
},
at(4s) {
this.opacity => 1.0 tween Interpolator.LINEAR;
this.scaleX => sx1 tween Interpolator.LINEAR;
this.scaleY => sy1 tween Interpolator.LINEAR;
this.translateX => tx1 tween Interpolator.LINEAR;
this.translateY => ty1 tween Interpolator.LINEAR;
this.rotate => ro1 tween Interpolator.LINEAR;
},
at(8s) {
this.opacity => .1 tween Interpolator.LINEAR;
},
KeyFrame {
time: 8s
action: function() {
delete this from (this.parent as Group).content;
}
}
]
}
};
package magicanswers;
import java.util.Random;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.animation.transition.AnimationPath;
import javafx.animation.transition.PathTransition;
import javafx.scene.CustomNode;
import javafx.scene.effect.light.DistantLight;
import javafx.scene.effect.Lighting;
import javafx.scene.effect.Reflection;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.Node;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.HLineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
public class Oracle extends CustomNode {
def rnd : Random = new Random();
//it appears that 9 words is the limit
var pool_of_answers = ["Who Knows","Absolutely","Maybe", "Yes", "No","Unclear at this time","Without a doubt"];
var answer_to_question = "";
var word:MessageObject;
var node:Node;
var flag_changeover:Boolean = false;
def path = [
MoveTo {
x: 500,
y:600 },
HLineTo{
x:525 },
HLineTo{
x:475}
ClosePath{ } ];
def animation = PathTransition
{
node: this;
path: AnimationPath.createFromPath(Path{
elements: path});
interpolate: true;
duration: .3s
repeatCount: 4
}
var image:Image = Image {
url: "{__DIR__}8ball.png"
preserveRatio: true
backgroundLoading: true
}
var image2:Image = Image {
url: "{__DIR__}8ball_view.png"
preserveRatio: true
backgroundLoading: true
}
public var changeover = Timeline {
keyFrames: [
at(0s) {
this.opacity => .9 tween Interpolator.LINEAR;
},
at(2s) {
this.opacity => 0 tween Interpolator.LINEAR;
},
at(4s) {
this.opacity => 1.0 tween Interpolator.LINEAR;
},
KeyFrame {
time: 2.0s
action: function() {
image=image2;
flag_changeover = true;
}
}
KeyFrame {
time:4.2s
action: function() {
generateMessage();
}
}
]
}
public override function create(): Node {
node = Group {
cache:true;
content: [
ImageView {
x: 250
y: 190
image: bind image
visible: true
cache:true
}
] // content
effect: Reflection {
topOffset:0.1;
fraction: 0.85
topOpacity:.3
bottomOpacity:0.1
input:Lighting {
light: DistantLight {
azimuth: -135
}
surfaceScale: 9
}
}
onMousePressed: function (event) {
if(not flag_changeover){
changeover.playFromStart();
}
else if(not animation.running and not word.fade.running ){
generateMessage();
}
}
}
}
function generateMessage(){
animation.playFromStart();
answer_to_question = pool_of_answers[
rnd.nextInt(sizeof pool_of_answers)];
word = MessageObject {
content: bind answer_to_question
};
insert word into (this.parent as Group).content;
word.fade.play();
}
}Get this code at GitHub.
| Print article | This entry was posted by Eric Warriner on December 18, 2009 at 7:11 pm, and is filed under JavaFX. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
