multithreading - Converting from Timeline to Thread -
i trying create program runs off of thread instead of timeline. here modified program down below. not sure why not work. tips appreciated. thread uses task in order start animation. help.
import javafx.animation.keyframe; import javafx.animation.timeline; import javafx.application.application; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.layout.borderpane; import javafx.scene.layout.hbox; import javafx.scene.layout.pane; import javafx.scene.paint.color; import javafx.scene.shape.arc; import javafx.scene.shape.arctype; import javafx.scene.shape.circle; import javafx.stage.stage; import javafx.util.duration; public class ch30 extends application { @override // override start method in application class public void start(stage primarystage) { fanpane fan = new fanpane(); hbox hbox = new hbox(5); button btpause = new button("pause"); button btresume = new button("resume"); button btreverse = new button("reverse"); hbox.setalignment(pos.center); hbox.getchildren().addall(btpause, btresume, btreverse); borderpane pane = new borderpane(); pane.setcenter(fan); pane.setbottom(hbox); // create scene , place in stage scene scene = new scene(pane, 200, 200); primarystage.settitle("exercise15_28"); // set stage title primarystage.setscene(scene); // place scene in stage primarystage.show(); // display stage runnable first = new begin(); thread t1 = new thread(first); t1.start(); //timeline animation = new timeline( //new keyframe(duration.millis(100), e -> fan.move())); //animation.setcyclecount(timeline.indefinite); //animation.play(); // start animation scene.widthproperty().addlistener(e -> fan.setw(fan.getwidth())); scene.heightproperty().addlistener(e -> fan.seth(fan.getheight())); //btpause.setonaction(e -> first.wait()); btresume.setonaction(e -> first.run()); btreverse.setonaction(e -> fan.reverse()); } /** * main method needed ide limited * javafx support. not needed running command line. */ public static void main(string[] args) { launch(args); } } class fanpane extends pane { private double w = 200; private double h = 200; private double radius = math.min(w, h) * 0.45; private arc arc[] = new arc[4]; private double startangle = 30; private circle circle = new circle(w / 2, h / 2, radius); public fanpane() { circle.setstroke(color.black); circle.setfill(color.white); getchildren().add(circle); (int = 0; < 4; i++) { arc[i] = new arc(w / 2, h / 2, radius * 0.9, radius * 0.9, startangle + * 90, 35); arc[i].setfill(color.red); // set fill color arc[i].settype(arctype.round); getchildren().addall(arc[i]); } } private double increment = 5; public void reverse() { increment = -increment; } public void move() { setstartangle(startangle + increment); } public void setstartangle(double angle) { startangle = angle; setvalues(); } public void setvalues() { radius = math.min(w, h) * 0.45; circle.setradius(radius); circle.setcenterx(w / 2); circle.setcentery(h / 2); (int = 0; < 4; i++) { arc[i].setradiusx(radius * 0.9); arc[i].setradiusy(radius * 0.9); arc[i].setcenterx(w / 2); arc[i].setcentery(h / 2); arc[i].setstartangle(startangle + * 90); } } public void setw(double w) { this.w = w; setvalues(); } public void seth(double h) { this.h = h; setvalues(); } } class begin implements runnable { private int times = 1000; fanpane fan = new fanpane(); public begin(){ // times = t; } @override public void run(){ //for (int = 0; < times; i++) //{ fan.move(); } }
your thread needs loop, repeatedly pauses , updates ui. if want use explicit thread instead of more obvious timeline
, need write code loop yourself.
the update of ui cannot called background thread creating: update ui on fx application thread must wrap call method(s) update ui in platform.runlater(...)
.
Comments
Post a Comment