0

i am new to scenebuilder and fx and need some help. I have the class Puffer , TestMain and MainViewController. I tried to paint on the canvas and it worked.I tried Puffer and it worked too. Now I wanted to use scenebuilder and have my problems.I would like to draw the Group from Puffer on the canvas with a button and don't know what's the best solution.I already tried to modify Puffer without any result.

public class Puffer {
    
    Group root = new Group();
    
public void draw() {
    for (int x = 0; x < 800; x++) {
        for (int y = 0; y < 600; y++) {
            Complex z0 = new Complex(0.11, 0.123); //!!!!!!!!!!!!! under 1
            Complex c = new Complex(x / 400.0 - 2, 1 - y / 400.0);

            if (isBounded(iterate(z0, c))) {
                drawPoint(x, y, root);
            }
        }
    }

//    primaryStage.setScene(new Scene(root, 800, 600));
//    primaryStage.show();
}


public void drawPoint(int x, int y, Group root) {
    int max = 255;
    int min = 0;
    int range = max - min + 1;


    Line line = new Line(x, y, x, y);
   // line.setStroke(Color.rgb((int)(Math.random() * range) + min,(int)(Math.random() * range) + min,(int)(Math.random() * range) + min));
    root.getChildren().add(line);
}

public Complex iterate(Complex z0, Complex c) {
    Complex zn = z0;

    for (int i = 0; i < 20; i++) {
        zn.square().add(c);
    }

    return zn;
}

public boolean isBounded(Complex zn) {
    return zn.module() < 2;
}

public class MainViewController {
    @FXML
    Canvas canvas;

    // Event Listener on Button.onAction
    @FXML
    public void btnOkClicked(ActionEvent event) {
        System.out.println("Test");
    
    }
    
     @FXML
        public void drawCanvas(){

         
//          GraphicsContext gc = canvas.getGraphicsContext2D();
//          gc.setLineWidth(3);
//          gc.setStroke(Color.BLACK);
//          System.out.println("drawCanvas");
//
//          try {
//              canvas.setOnMousePressed(event -> {
//                  System.out.println("Mouse click");
//                  gc.beginPath();
//                  gc.lineTo(event.getSceneX(), event.getSceneY());
//                  gc.stroke();
//              });
//
//              canvas.setOnMouseDragged(event -> {
//                  System.out.println("Mouse dragged");
//                  gc.lineTo(event.getSceneX(), event.getSceneY());
//                  gc.stroke();
//              });
//          }catch (Exception e){
//              System.out.println(e);
//              System.exit(0);
//          }

        }

 public class TestMain extends Application {

    @Override
    public void start(Stage s1) throws Exception {
    try {
        Parent root = FXMLLoader.load(getClass().getResource("MainView.fxml"));
        Scene scene = new Scene(root);
        
        s1.setTitle("Test");
        s1.setScene(scene);
        s1.show();
        
    }catch(Exception ex) {
        ex.printStackTrace();
    }
        
        
        
    }
    
    public static void main(String[] args) {
        launch(args);
    }
       
jewelsea
  • 150,031
  • 14
  • 366
  • 406
wonkas42
  • 1
  • 2
  • Should i maybe safe the lines somehow from puffer and then "paint" them in the Controller class ? – wonkas42 Jul 17 '22 at 22:10
  • I think you'll have to convert things like `group.getChildren().add(line)` to `gc.strokeLine()` in your code. – trashgod Jul 17 '22 at 22:41
  • 4
    SceneBuilder is not a drawing app, it is a design and layout app. Don’t try to misuse the tool for a purpose for which it was not intended. Instead draw on the canvas in code using the technique which trashgod outlined or call the snapshot method on the group from the puffer class after it has finished drawing, then paint the resultant image on the canvas. If you can add some context on why you need the canvas at all and can’t just display the puffer group directly, that may help you get a useful answer. – jewelsea Jul 18 '22 at 00:03
  • @trashgod @ jewelsea thank you for the help – wonkas42 Jul 18 '22 at 11:59
  • 1
    O(n^2) nodes scales poorly; I switched to `Canvas` around 10^4. You might look at the MandelbrotSet in [Demos and Samples](https://www.oracle.com/java/technologies/downloads/) or the approach outlined [here](https://stackoverflow.com/a/44141878/230513). – trashgod Jul 18 '22 at 12:39

0 Answers0