0

I have a class that is supposed to draw the Sierpinski triangle to a given degree, determined by increase/decrease buttons to alter that degree. However, the degree (variable x) always reverts to 0 after I process the button press.

Where is my problem?

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Recursion4 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane pane = new BorderPane();
        Button plus = new Button("+");
        Button minus = new Button("-");
        HBox h = new HBox(5);
        Pane p = new Pane();
        h.getChildren().addAll(plus, minus);
        h.setSpacing(10);
        pane.setBottom(h);
        pane.setCenter(p);
        h.setAlignment(Pos.CENTER);
        Scene scene = new Scene(pane, 900, 900);
        primaryStage.setTitle("Triangles");
        primaryStage.setScene(scene);
        primaryStage.show();
        int x = 0;
        draw(450, 300, 300, 600, 600, 600, x, p);
        plus.setOnAction(e -> {
             plus(x, p);

        });
        minus.setOnAction(e -> {
            minus(x, p);
        });

    }

    public static void main(String[] args) {
        launch(args);
        int x = 0;
    }

    public static void plus(int x, Pane p) {
        p.getChildren().clear();
        ++x;
        draw(450, 300, 300, 600, 600, 600, x, p);

    }

    public static void minus(int x, Pane p) {
        p.getChildren().clear();
        if (x == 0) {
            draw(450, 300, 300, 600, 600, 600, x, p);
        } else {
            x--;
            draw(450, 300, 300, 600, 600, 600, x, p);

        }
    }

    public static void draw(int x1, int y1, int x2, int y2, int x3, int y3, Pane p) {
        Line l1 = new Line(x1, y1, x2, y2);
        Line l2 = new Line(x2, y2, x3, y3);
        Line l3 = new Line(x3, y3, x1, y1);
        p.getChildren().add(l1);
        p.getChildren().add(l2);
        p.getChildren().add(l3);
    }

    public static void draw(int x1, int y1, int x2, int y2, int x3, int y3, int z, Pane p) {
        if (z == 0) {
            draw(x1, y1, x2, y2, x3, y3, p);
        } else {
            draw(x1, y1, x2, y2, x3, y3, p);
            int xa, ya, xb, yb, xc, yc; 
            xa = (x1 + x2) / 2; 
            ya = (y1 + y2) / 2;
            xb = (x1 + x3) / 2;
            yb = (y1 + y3) / 2;
            xc = (x2 + x3) / 2;
            yc = (y2 + y3) / 2;

            draw(x1, y1, xa, ya, xb, yb, z - 1,p); 
            draw(xa, ya, x2, y2, xc, yc, z - 1,p);
            draw(xb, yb, xc, yc, x3, y3, z - 1,p);

        }
    }
}
Prune
  • 76,765
  • 14
  • 60
  • 81
STRAN
  • 73
  • 1
  • 8

1 Answers1

0

Each of main, plus, and minus has a local variable x (note the local declarations). Nowhere do you refer to the object attribute x, which seems to be what you want to alter. Use this.x instead of declaring a local one.

Prune
  • 76,765
  • 14
  • 60
  • 81