java - Timeline animation JavaFX -
i know why screen not clearing after every move of chart. thats code :
mainclass, remove there "time" , put lambda expresion in drawing method updatescene, dont know yet how :/
import javafx.application.application; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.paint.color; import javafx.stage.stage; public class mainclass extends application{ private drawing draw; private double time = 0; public static void main(string[] args) { launch(args); } @override public void start(final stage stage) { group root = new group(); root.setstyle("-fx-background-color: rgb(35, 39, 50);"); stage.setscene(new scene(root, 1000, 1000, color.rgb(35, 39, 50))); while(time < 2 ) { draw = new drawing(root, time); time+=0.1; draw.updatescene(); stage.show(); } } }
axes class describing how should draw axes :
import javafx.beans.binding.bindings; import javafx.geometry.side; import javafx.scene.chart.numberaxis; import javafx.scene.layout.pane; public class axes extends pane{ private numberaxis xaxis; private numberaxis yaxis; public axes(int width, int height, double xmin, double xmax, double xtickunit, double ymin, double ymax, double ytickunit) { setminsize(pane.use_pref_size, pane.use_pref_size); setprefsize(width, height); setmaxsize(pane.use_pref_size, pane.use_pref_size); xaxis = new numberaxis(xmin, xmax, xtickunit); xaxis.setside(side.bottom); xaxis.setminortickvisible(false); xaxis.setprefwidth(width); xaxis.setlayouty(height/2); yaxis = new numberaxis(ymin, ymax, ytickunit); yaxis.setside(side.left); yaxis.setminortickvisible(false); yaxis.setprefheight(height); yaxis.layoutxproperty().bind(bindings.subtract((width / 2) + 1, yaxis.widthproperty())); getchildren().setall(xaxis, yaxis); } public numberaxis getxaxis() { return xaxis; } public numberaxis getyaxis() { return yaxis; } }
scaling , drawing need in chart.
import java.util.function.function; import javafx.scene.layout.pane; import javafx.scene.paint.color; import javafx.scene.shape.lineto; import javafx.scene.shape.moveto; import javafx.scene.shape.path; import javafx.scene.shape.rectangle; public class chart extends pane { public chart(function<double, double> f, double xmin, double xmax, double xinc, axes axes ) { path path = new path(); path.setstroke(color.orange.derivecolor(0, 1, 1,0.5)); path.setstrokewidth(1); path.setclip(new rectangle(0, 0, axes.getprefwidth(), axes.getprefheight())); double x = xmin; double y = f.apply(x); path.getelements().add(new moveto( mapx(x, axes), mapy(y, axes) )); x+=xinc; while(x < xmax) { if(x == xmax) { x = xmin; y = f.apply(x); path.getelements().add(new moveto( mapx(x, axes), mapy(y, axes) )); } y = f.apply(x); path.getelements().add(new lineto( mapx(x, axes), mapy(y, axes) )); x+=xinc; } setminsize(pane.use_pref_size, pane.use_pref_size); setprefsize(axes.getprefwidth(), axes.getprefheight()); setmaxsize(pane.use_pref_size, pane.use_pref_size); getchildren().setall(axes, path); } private double mapx(double x, axes axes) { double fx = axes.getprefwidth() / 2; double sx = axes.getprefwidth() / (axes.getxaxis().getupperbound() - axes.getxaxis().getlowerbound()); return x * fx + sx; } private double mapy(double y, axes axes) { double fy = axes.getprefheight() / 2; double sy = axes.getprefheight() / (axes.getyaxis().getupperbound() - axes.getyaxis().getlowerbound()); return -y * sy + fy; } }
drawing class responsible making animation , drawing chart pattern want to.
import javafx.animation.keyframe; import javafx.animation.timeline; import javafx.scene.group; import javafx.util.duration; public class drawing { private double t = 0; private double l = 1; private group root; public drawing(final group root, double t) { this.root = root; this.t = t; } public void updatescene() { final chart chart = new chart(x -> math.exp(-(math.pow((x-t), 2)))*math.cos((2*math.pi*(x-t))/l), -1, 1, 0.01, new axes(1000, 1000, -1, 1, 0.1, -1, 1, 0.1) ); timeline timeline = new timeline(timeline.indefinite, new keyframe(new duration(1000), x -> { root.getchildren().add(chart); })); timeline.setautoreverse(true); timeline.play(); } }
screen showing got after compilation. question up, dunno causes not cleaning after each timeline stage.
- if want reasonable answer question, should not throw code on instead boil down code essential reproduce problem.
- the overall concept of program looks horribly wrong me. if want move chart move not create new one.
- why call stage.show() in loop?
- without having time @ code i'd problem constant recreation of charts.
Comments
Post a Comment