java - Threading Stopwatches -
i trying create 4 stopwatches run concurrently. start when program begins.
i want user able use q,w,e,r keys speed stopwatches (from left right following keyboard pattern). i've established pretty wont let press other key r key speed 4th clock.
i want implement threading allow this. how go it?
heres code:
public class stopwatches extends application { @override public void start(stage primarystage) { time time1 = new time(); time time2 = new time(); time time3 = new time(); time time4 = new time(); text text1 = new text("00:00:00"); text text2 = new text("00:00:00"); text text3 = new text("00:00:00"); text text4 = new text("00:00:00"); text1.setfont(font.font("times", 35)); text2.setfont(font.font("times", 35)); text3.setfont(font.font("times", 35)); text4.setfont(font.font("times", 35)); timeline textclock1 = createtextstopwatch(time1, text1); textclock1.play(); timeline textclock2 = createtextstopwatch(time2, text2); textclock2.play(); timeline textclock3 = createtextstopwatch(time3, text3); textclock3.play(); timeline textclock4 = createtextstopwatch(time4, text4); textclock4.play(); borderpane pane = new borderpane(); tilepane tilepane = new tilepane(); tilepane.setpadding(new insets(15,5,5,5)); tilepane.setalignment(pos.center); tilepane.sethgap(40.0); tilepane.getchildren().addall(text1, text2, text3, text4); pane.setcenter(tilepane); scene scene = new scene(pane, 1500, 400); scene.setonkeypressed(e -> { if (e.getcode() == q ){ textclock1.setrate(20.0); } }); scene.setonkeyreleased(e -> { if (e.getcode() == q ){ textclock1.setrate(1.0); } }); scene.setonkeypressed(e -> { if (e.getcode() == w ){ textclock2.setrate(20.0); } }); scene.setonkeyreleased(e -> { if (e.getcode() == w ){ textclock2.setrate(1.0); } }); scene.setonkeypressed(e -> { if (e.getcode() == e ){ textclock3.setrate(20.0); } }); scene.setonkeyreleased(e -> { if (e.getcode() == e ){ textclock3.setrate(1.0); } }); scene.setonkeypressed(e -> { if (e.getcode() == r ){ textclock4.setrate(20.0); } }); scene.setonkeyreleased(e -> { if (e.getcode() == r ){ textclock4.setrate(1.0); } }); primarystage.settitle("stop watch"); primarystage.setscene(scene); primarystage.show(); } public timeline createtextstopwatch(time time, text text) { timeline animation = new timeline( new keyframe(duration.millis(1000), e -> { time.increase(); text.settext(time.tostring()); })); animation.setcyclecount(timeline.indefinite); return animation; } public static void main(string[] args) { launch(args); } }
ive made second attempt @ implementing multithreading, wont work.
public class stopwatches extends application { @override public void start(stage primarystage) { time time1 = new time(); time time2 = new time(); time time3 = new time(); time time4 = new time(); text text1 = new text("00:00:00"); text text2 = new text("00:00:00"); text text3 = new text("00:00:00"); text text4 = new text("00:00:00"); text1.setfont(font.font("times", 35)); text2.setfont(font.font("times", 35)); text3.setfont(font.font("times", 35)); text4.setfont(font.font("times", 35)); timeline textclock1 = createtextstopwatch(time1, text1); textclock1.play(); timeline textclock2 = createtextstopwatch(time2, text2); textclock2.play(); timeline textclock3 = createtextstopwatch(time3, text3); textclock3.play(); timeline textclock4 = createtextstopwatch(time4, text4); textclock4.play(); borderpane pane = new borderpane(); tilepane tilepane = new tilepane(); tilepane.setpadding(new insets(15,5,5,5)); tilepane.setalignment(pos.center); tilepane.sethgap(40.0); tilepane.getchildren().addall(text1, text2, text3, text4); pane.setcenter(tilepane); scene scene = new scene(pane, 1500, 400); runnable task1 = new increasetime(textclock1); runnable task2 = new decreasetime(textclock1); runnable task3 = new increasetime(textclock2); runnable task4 = new decreasetime(textclock2); runnable task5 = new increasetime(textclock3); runnable task6 = new decreasetime(textclock3); runnable task7 = new increasetime(textclock4); runnable task8 = new decreasetime(textclock4); thread thread1 = new thread(task1); thread thread2 = new thread(task2); thread thread3 = new thread(task3); thread thread4 = new thread(task4); thread thread5 = new thread(task5); thread thread6 = new thread(task6); thread thread7 = new thread(task7); thread thread8 = new thread(task8); scene.setonkeypressed(e -> { if (e.getcode() == q ){ thread1.start(); } }); scene.setonkeyreleased(e -> { if (e.getcode() == q ){ thread2.start(); } }); scene.setonkeypressed(e -> { if (e.getcode() == w ){ thread3.start(); } }); scene.setonkeyreleased(e -> { if (e.getcode() == w ){ thread4.start(); } }); scene.setonkeypressed(e -> { if (e.getcode() == e ){ thread5.start(); } }); scene.setonkeyreleased(e -> { if (e.getcode() == e ){ thread6.start(); } }); scene.setonkeypressed(e -> { if (e.getcode() == r ){ thread7.start(); } }); scene.setonkeyreleased(e -> { if (e.getcode() == r ){ thread8.start(); } }); primarystage.settitle("stop watch"); primarystage.setscene(scene); primarystage.show(); } public timeline createtextstopwatch(time time, text text) { timeline animation = new timeline( new keyframe(duration.millis(1000), e -> { time.increase(); text.settext(time.tostring()); })); animation.setcyclecount(timeline.indefinite); return animation; } public static void main(string[] args) { launch(args); } } class increasetime implements runnable { timeline textclock; public increasetime(timeline textclock) { this.textclock = textclock; } @override public void run() { textclock.setrate(20.0); } } class decreasetime implements runnable { timeline textclock; public decreasetime(timeline textclock) { this.textclock = textclock; } @override public void run() { textclock.setrate(1.0); } }
i forgot show secondary class makes time objects, here is:
public class time { int value = 0; int getsecond() { return value % 60; } int getminute() { return (value / 60) % 60; } int gethour() { return value / 3600; } void reset() { value = 0; } void increase() { value++; } @override public string tostring() { return gettwodigitstring(gethour()) + ":" + gettwodigitstring(getminute()) + ":" + gettwodigitstring(getsecond()); } static string gettwodigitstring(int v) { if (v < 10) return "0" + v; else return "" + v; } }
the app getting of button presses q,w,e,r.
problem setonkeyreleased methods setrate on textclock4. believe think getting r button presses.
where sounds want
q operate on textclock1 w operate on textclock2 e operate on textclock3 r operate on textclock4
a debugging tip add print statement console scene on button presses receives. begin breaking down , narrowing down in code issue be.
Comments
Post a Comment