how to update view in Group in javaFX? -
i have graph this. want update view when change slider. however, elements stacked that when change slider. can new view.
my app's structrue:
circle , quadcurve -> group -> scrollpane -> borderpane -> scene -> stage
this app demonstrates trying achieve. comments in code.
main
import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.parent; import javafx.scene.scene; import javafx.stage.stage; /** * * @author blj0011 */ public class javafxapplication6 extends application { @override public void start(stage stage) throws exception { parent root = fxmlloader.load(getclass().getresource("fxmldocument.fxml")); scene scene = new scene(root); stage.setscene(scene); stage.show(); } /** * @param args command line arguments */ public static void main(string[] args) { launch(args); } }
controller
import java.net.url; import java.util.resourcebundle; import javafx.event.actionevent; import javafx.fxml.fxml; import javafx.fxml.initializable; import javafx.scene.group; import javafx.scene.control.slider; import javafx.scene.paint.color; import javafx.scene.shape.circle; /** * * @author blj0011 */ public class fxmldocumentcontroller implements initializable { @fxml private group groupmain; @fxml private circle cirmain, circenter, cirradius1, cirradius2, cirradius3; @fxml private slider slidermain; @override public void initialize(url url, resourcebundle rb) { // todo } @fxml void handlebutton(actionevent event) { double radius = cirmain.getradius();//get transparent maincircle radius system.out.println(radius + " : " + cirmain.getlayoutx() + " : " + cirmain.getlayouty()); cirradius1.setradius(20); cirradius1.setlayoutx(radius * math.cos(50) + cirmain.getlayoutx());//set cirradius1 x location on circumference of cirmain cirradius1.setlayouty(radius * math.sin(50) + cirmain.getlayouty());//set cirradius1 y location on circumference of cirmain cirradius1.setfill(color.red); cirradius2.setradius(10); cirradius2.setlayoutx(radius * math.cos(175) + cirmain.getlayoutx());//set cirradius2 x location on circumference of cirmain cirradius2.setlayouty(radius * math.sin(175) + cirmain.getlayouty());//set cirradius2 y location on circumference of cirmain cirradius2.setfill(color.yellow); cirradius3.setradius(15); cirradius3.setlayoutx(radius * math.cos(300) + cirmain.getlayoutx());//set cirradius3 x location on circumference of cirmain cirradius3.setlayouty(radius * math.sin(300) + cirmain.getlayouty());//set cirradius3 y location on circumference of cirmain cirradius3.setfill(color.green); slidermain.setvalue(radius);//set slider value current cirmain radius //this listener moves circle inward or outward depending on slider. changes radius of cirmain's radius slidermain.valueproperty().addlistener((obs, oldval, newval)->{ cirmain.setradius(newval.doublevalue()); cirradius1.setlayoutx(newval.doublevalue() * math.cos(50) + cirmain.getlayoutx()); cirradius1.setlayouty(newval.doublevalue() * math.sin(50) + cirmain.getlayouty()); cirradius2.setlayoutx(newval.doublevalue() * math.cos(175) + cirmain.getlayoutx()); cirradius2.setlayouty(newval.doublevalue() * math.sin(175) + cirmain.getlayouty()); cirradius3.setlayoutx(newval.doublevalue() * math.cos(300) + cirmain.getlayoutx()); cirradius3.setlayouty(newval.doublevalue() * math.sin(300) + cirmain.getlayouty()); }); } }
fxml
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.control.button?> <?import javafx.scene.control.slider?> <?import javafx.scene.layout.anchorpane?> <?import javafx.scene.shape.circle?> <anchorpane maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="518.0" prefwidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication6.fxmldocumentcontroller"> <children> <circle fx:id="cirmain" fill="#ededed00" layoutx="300.0" layouty="200.0" radius="175.0" stroke="black" stroketype="inside" /> <circle fx:id="circenter" fill="dodgerblue" layoutx="300.0" layouty="200.0" radius="14.0" stroke="black" stroketype="inside" /> <circle fx:id="cirradius1" fill="dodgerblue" layoutx="177.0" layouty="156.0" radius="25.0" stroke="black" stroketype="inside" /> <circle fx:id="cirradius2" fill="dodgerblue" layoutx="412.0" layouty="166.0" radius="35.0" stroke="black" stroketype="inside" /> <circle fx:id="cirradius3" fill="dodgerblue" layoutx="237.0" layouty="290.0" radius="27.0" stroke="black" stroketype="inside" /> <button layoutx="288.0" layouty="479.0" mnemonicparsing="false" onaction="#handlebutton" text="button" /> <slider fx:id="slidermain" blockincrement="1.0" layoutx="34.0" layouty="418.0" max="200.0" prefheight="38.0" prefwidth="551.0" showticklabels="true" showtickmarks="true" /> </children> </anchorpane>
Comments
Post a Comment