Hack Forums
[Java] JavaFX - Having problems with adding/making custom shapes as GUI elements. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] JavaFX - Having problems with adding/making custom shapes as GUI elements. (/showthread.php?tid=4911700)



JavaFX - Having problems with adding/making custom shapes as GUI elements. - GreyCoder - Jul 18, 2015

Hello. I made a window in javafx using scenebuilder and now I want to make 2d object and put it in there.
Because everything I made (buttons, textfields, etc) was done in SceneBuilder I didn't know how to add this 2d object there.
Idea was to make custom 2d buttons(for example) and put it there.
I tried something like this:

Any suggestions? And how to make custom gui shapes?

Code
public class Main extends Application {
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        Canvas canvas = new Canvas(300, 250);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        drawShapes(gc);
        Parent fxmlRoot= FXMLLoader.load(getClass().getResource("gui.fxml"));
        fxmlRoot.getChildrenUnmodifiable().add(canvas);
        primaryStage.setScene(new Scene(fxmlRoot));
        
        primaryStage.show();
        
    }
    
    private 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);
        gc.strokeOval(60, 60, 30, 30);
        gc.fillRoundRect(110, 60, 30, 30, 10, 10);
        gc.strokeRoundRect(160, 60, 30, 30, 10, 10);
        gc.fillArc(10, 110, 30, 30, 45, 240, ArcType.OPEN);
        gc.fillArc(60, 110, 30, 30, 45, 240, ArcType.CHORD);
        gc.fillArc(110, 110, 30, 30, 45, 240, ArcType.ROUND);
        gc.strokeArc(10, 160, 30, 30, 45, 240, ArcType.OPEN);
        gc.strokeArc(60, 160, 30, 30, 45, 240, ArcType.CHORD);
        gc.strokeArc(110, 160, 30, 30, 45, 240, ArcType.ROUND);
        gc.fillPolygon(new double[]{10, 40, 10, 40},
                       new double[]{210, 210, 240, 240}, 4);
        gc.strokePolygon(new double[]{60, 90, 60, 90},
                         new double[]{210, 210, 240, 240}, 4);
        gc.strokePolyline(new double[]{110, 140, 110, 140},
                          new double[]{210, 210, 240, 240}, 4);
        
    }

    public static void main(String[] args) {
        launch(args);
        
    
    }
    
}