java - How to resize a canvas on JavaFX to fit the size -


i new javafx , encountered problem resizing canvas. trying canvas size fit contents place it. output should fragment 5/7, lot of white space right next numbers drew. tried resizing canvas using setwidth , setting canvas on small size begining nothing seems help.

here code:

import javafx.application.application; import javafx.scene.scene; import javafx.scene.canvas.canvas; import javafx.scene.canvas.graphicscontext; import javafx.scene.layout.hbox; import javafx.scene.text.font; import javafx.scene.text.fontweight; import javafx.stage.stage;  public class example extends application {      font fontlarge = font.font("droid sans", fontweight.bold, 15);      @override     public void start(stage stage) {          hbox root = new hbox();         scene scene = new scene(root);          root.getchildren().add(getcanvasofrationalnumber("5", "7"));         scene.setroot(root);         stage.setscene(scene);         stage.show();     }      public canvas getcanvasofrationalnumber(string num, string denom) {         final canvas rncanvas = new canvas(300, 55);         graphicscontext gc = rncanvas.getgraphicscontext2d();         gc.setfont(fontlarge);          gc.filltext(num, 0, 15);         gc.filltext("___", 0, 20);         gc.filltext(denom, 0, 40);          rncanvas.setwidth(15);         return rncanvas;     }      public static void main(string[] args) {         launch(args);     }  } 

edit: previous answer wrong, here's new one.

we need clarify on want. if understand correctly want window size isn't larger canvas size.

as jewelsea pointed out in comments can change canvas width , height. in fact doing in code.

i suggest choose different colors canvas , scene. if e. g.:

public class example extends application {      font fontlarge = font.font("droid sans", fontweight.bold, 15);      @override     public void start(stage stage) {          hbox root = new hbox();         scene scene = new scene(root, color.yellow);          root.getchildren().add(getcanvasofrationalnumber("5", "7"));         scene.setroot(root);         stage.setscene(scene);         stage.show();     }      public canvas getcanvasofrationalnumber(string num, string denom) {         final canvas rncanvas = new canvas(300, 55);         graphicscontext gc = rncanvas.getgraphicscontext2d();         gc.setfont(fontlarge);          gc.setfill(color.lightblue);         gc.fillrect(0, 0, 300, 55);          gc.setfill(color.blue);          gc.filltext(num, 0, 15);         gc.filltext("___", 0, 20);         gc.filltext(denom, 0, 40);          rncanvas.setwidth(15);         return rncanvas;     }      public static void main(string[] args) {         launch(args);     }  } 

you'll this

enter image description here

you see canvas has been resized. problem facing window can't smaller. that's white (or here yellow) space scene, not canvas. try setting width 1 , height 1, you'll still end this:

enter image description here

my guess window size can't become smaller because of min/max/close buttons. change style using initstyle method rid of buttons.

it comes down requirements are.


old (wrong) answer:

you can't modify width/height of canvas.

canvas image can drawn on using set of graphics commands provided graphicscontext.

a canvas node constructed width , height specifies size of image canvas drawing commands rendered. drawing operations clipped bounds of image.

your solution either copy canvas area new canvas size or don't use canvas @ all, instead use e. g. text node. depends on requirements.