Okay so I've got an example working using the Swing Worker and even figured out how to get call from Java back into JavaFX, which I found in one of Michael Heinrichs blogs (Thanks! .... http://blogs.sun.com/michaelheinrichs/entry/using_javafx_objects_in_java)
NOTE: the AbstractSyncOperation is probably a more JavaFX-ey way of doing this but although it's in the javafxrt.jar it's not documented in the JavaFX docs. It's probably coming out in the 1.0 release next month.
What I've done is modified the "Bouncy Bubbles" example so that if you click on a Bubble the onMouseClicked() function blocks and waits five seconds and then changes the color of the Bubble to pink.
If you uncomment the following code then the whole UI freezes and simulates the problem you are having.
// Option 1: Long running action executes on event dispatch thread
try {
Thread.sleep(5000);
color = Color.PINK;
}
catch(ex : InterruptedException ) {
// wake up and carry on
}
But if you uncomment the following code the program still waits five second before changing the color of the bubble but it doesn't freeze the whole UI.
// Option 2: Long running action executed in it's own thread
var worker = new ColorMePinkWorker(this);
worker.execute();
How this works is that ColorMePinkWorker subclasses the SwingWorker class and when you call execute() it starts a new thread to perform your action inside of the doInBackground() method. When this method finishes the SwingWorker places itself on the Event Dispatch Queue and waits for the right moment to call the done() method which makes the UI color change.
The rest of the code follows....
- Richard.
/*
* Copyright (c) 2007, Sun Microsystems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
* * Neither the name of Sun Microsystems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package motion;
import javafx.scene.Node;
import javafx.scene.CustomNode;
import javafx.scene.geometry.Circle;
import javafx.scene.paint.Color;
import javafx.application.Frame;
import javafx.application.Stage;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.input.*;
import java.lang.*;
import java.util.Random;
/**
* @author Michal Skvor
*/
var spring : Number = 0.05;
var gravity : Number = 0.05;
var bubbles : Bubble[];
var width : Number = 200;
var height : Number = 200;
var timer : Timeline = Timeline {
repeatCount: Timeline.INDEFINITE
keyFrames :
KeyFrame {
time : 16ms
action : function() : Void {
for( bubble in bubbles ) {
bubble.collide( bubbles, spring, width, height );
bubble.move( gravity, width, height );
}
}
}
};
var rnd : Random = new Random();
for( i in [1..12] ) {
insert Bubble {
x : rnd.nextInt( width ), y : rnd.nextInt( height ), radius : rnd.nextInt( 10 ) + 10
color : Color.WHITE, opacity : 0.8
} into bubbles;
}
Frame {
stage : Stage {
fill : Color.GRAY
content : bind bubbles
};
visible : true
title : "Bouncy Bubbles"
width : 200
height : 232
closeAction : function() { java.lang.System.exit( 0 ); }
}
timer.start();
public class Bubble extends CustomNode, BubbleInterface {
public attribute x : Number;
public attribute y : Number;
public attribute radius : Number;
public attribute color : Color = Color.WHITE;
public attribute vx : Number;
public attribute vy : Number;
override attribute onMouseClicked = function(event: MouseEvent) : Void {
// Option 1: Long running action executes on event dispatch thread
try {
Thread.sleep(5000);
color = Color.PINK;
}
catch(ex : InterruptedException ) {
// wake up and carry on
}
// Option 2: Long running action executed in it's own thread
//var worker = new ColorMePinkWorker(this);
//worker.execute();
}
public function setColor() {
color = Color.PINK;
}
public function collide( bubbles : Bubble[], spring : Number, width : Number, height : Number ): Void {
for( bubble in bubbles ) {
var dx : Number = bubble.x - x;
var dy : Number = bubble.y - y;
var distance : Number = Math.sqrt( dx * dx + dy * dy );
var minDist : Number = bubble.radius + radius;
if( distance < minDist ) {
var angle : Number = Math.atan2( dy, dx );
var tx : Number = x + Math.cos( angle ) * minDist;
var ty : Number = y + Math.sin( angle ) * minDist;
var ax : Number = ( tx - bubble.x ) * spring;
var ay : Number = ( ty - bubble.y ) * spring;
vx -= ax;
vy -= ay;
bubble.vx += ax;
bubble.vy += ay;
}
}
}
public function move( gravity : Number, width : Number, height : Number ): Void {
vy += gravity;
x += vx;
y += vy;
if( x + radius > 200 ) {
x = width - radius;
vx *= - 0.9;
} else if( x - radius < 0 ) {
x = radius;
vx *= 0.9;
}
if( y + radius > 200 ) {
y = height - radius;
vy *= -0.9;
} else if( y - radius < 0 ) {
y = radius;
vy *= -0.9;
}
}
public function create(): Node {
return Circle {
centerX : bind x, centerY : bind y, radius : bind radius
fill : bind color
};
}
}
package motion;
public interface BubbleInterface {
public void setColor();
}
package motion;
import javax.swing.SwingWorker;
public class ColorMePinkWorker<String, Object> extends SwingWorker {
private BubbleInterface someBubble;
public ColorMePinkWorker(BubbleInterface someBubble) {
this.someBubble = someBubble;
}
@Override
public String doInBackground() {
// This method is called in a Thread created just for this class and
// outside of the event dispatch thread.
try {
Thread.sleep(5000);
// So... DON'T set the color here - because screen might be being redrawn
//someBubble.setColor();
}
catch(InterruptedException ex) {
// wake up and carry on
}
return null;
}
@Override
public void done() {
// Set the color here, because "now" is a threadsafe time to do so.
// This method is called from the event dispatch thread at a time when
// the UI framework is processing user actions and not redrawing the
// the screen.
someBubble.setColor();
}
}
|