Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package l4; import javafx.application.*; import javafx.geometry.Point2D; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import

image text in transcribed
package l4;
import javafx.application.*;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
public class SierpinskiTriangle extends Application{
@Override
public void start(Stage primaryStage) {
SierpinskiTrianglePane trianglePane = new SierpinskiTrianglePane();
// TextField tfOrder = new TextField();
// tfOrder.setOnAction(e -> trianglePane.setOrder(Integer.parseInt(tfOrder.getText())));
// tfOrder.setPrefColumnCount(4);
//tfOrder.setAlignment(Pos.BOTTOM_RIGHT);
Button up = new Buttom("+");
Button down = new Button("-");
HBox hBox = new HBox(10);
hBox.getChildren().addAll(new Label("Enter an order: "), tfOrder);
hBox.setAlignment(Pos.CENTER);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(trianglePane);
borderPane.setBottom(hBox);
Scene scene = new Scene(borderPane, 200, 210);
primaryStage.setTitle("SierpinskiTriangle");
primaryStage.setScene(scene);
primaryStage.show();
scene.widthProperty().addListener(ov -> trianglePane.paint());
scene.heightProperty().addListener(ov -> trianglePane.paint());
}
static class SierpinskiTrianglePane extends Pane {
private int order = 0;
public void setOrder(int order) {
this.order = order;
paint();
}
SierpinskiTrianglePane() {
}
protected void paint() {
Point2D p1 = new Point2D(getWidth() / 2, 10);
Point2D p2 = new Point2D(10, getHeight() - 10);
Point2D p3 = new Point2D(getWidth() - 10, getHeight() - 10);
this.getChildren().clear();
displayTriangles(order, p1, p2, p3);
}
private void displayTriangles(int order, Point2D p1, Point2D p2, Point2D p3) {
if (order == 0) {
Polygon triangle = new Polygon();
triangle.getPoints().addAll(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY());
triangle.setStroke(Color.BLACK);
triangle.setFill(Color.WHITE);
this.getChildren().add(triangle);
}
else {
Point2D p12 = p1.midpoint(p2);
Point2D p23 = p2.midpoint(p3);
Point2D p31 = p3.midpoint(p1);
displayTriangles(order - 1, p1, p12, p31);
displayTriangles(order - 1, p12, p2, p23);
displayTriangles(order - 1, p31, p23, p3);
}
}
public static void main(String[] args) {
Application.launch(SierpinskiTriangle.class, args);
}
}
}
1) Revise Listing 18.9 to develop a program that lets the user use the + and - buttons to increase or decrease the current order by 1, as shown in text book pg 745 Figure 18.12a. The initial order is 0. If the current order is 0, the Decrease button is ignored.
in javafx
nglePane extends Pane { int order) { Sierp... e() { -O { Point 2D(getWidth() / 2, 10 Point 2D(10, getHeight() - Point 2D(getWidth() - 10,8 en().clear(); es(order, p1, P2, P3); A P1, P2, P3); Hay Triangles(int order, Point 2 e) { Enter an order: criangle = new Polygon(); getPoints().addAll(p1.getX(), p1.getY(), p2.getX(), p2.getY(), 03.getX(), p.getY(); .setStroke(Color.BLACK); . setFill(Color.WHITE); tChildren().add(triangle); 12 - pl. midpoint (p2); Dp23 - p2.midpoint (p3); 2D 31 - p.idpoint (pi); ay Triangles Corder - 1, p1, p12, p31); ayTriangles Corder - 1, p1, p2, p23) layTriangles Corder 1. 31, p23, PU te void main(strinell args) tion. Launch Sierpinski Triangle class, ares) Smart Insert

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Query Formulation And Administration Using Oracle And PostgreSQL

Authors: Michael Mannino

8th Edition

1948426951, 978-1948426954

More Books

Students also viewed these Databases questions