JavaFX: draw canvas by a pressed button through the Controller -


i'd use button in order tu things drawn on javafx canvas.

package sample;  import javafx.fxml.fxml; import javafx.scene.canvas.canvas; import javafx.scene.canvas.graphicscontext; import javafx.scene.control.button; import javafx.scene.paint.color; import javafx.scene.shape.arctype; import javafx.scene.control.label;  public class controller {      @fxml     canvas canvas1;      @fxml     label label;      public void onbuttonpress(){         system.out.println("test");         canvas canvas1 = new canvas(300, 250);         graphicscontext gc = canvas1.getgraphicscontext2d();         drawshapes(gc);         label.settext("test");     }      public void drawshapes(graphicscontext gc) {         gc.setfill(color.green);         gc.setstroke(color.blue);         gc.setlinewidth(5);         gc.strokeline(40, 10, 10, 40);         gc.filloval(10, 60, 30, 30);     } } 

when press button, nothing happens. canvas in xml file defined follows:

 <canvas fx:id="canvas1" height="200.0" layoutx="30.0" layouty="14.0" width="552.0" /> 

may me?

you're creating new canvas, , drawing on it, instead of drawing on canvas created in fxml file.

remove line

 canvas canvas1 = new canvas(300, 250); 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -