JAVACreate a new JavaFX class. create a new Java class, Board, which extends jav
ID: 3871235 • Letter: J
Question
JAVACreate a new JavaFX class.
create a new Java class, Board, which extends javafx.Application, that draws a 600x519 pixel window. Set the window title to "Board".
Draw a triangle
Create an upright 200x200x200 equilateral triangle within the scene you made in step 1. Use the Polygon class. Make the vertices centered around (0,0), and afterward use setLayoutX() and setLayoutY() to recenter the triangle in the middle of your window. Set the fill color of the triangle to LIGHTGREY.
Hint: Relative to the center of the triangle, the apex of the triangle should be at (0.0, -86.6), and the right and left corners of the base should be (100.0, 86.6) and (-100.0, 86.6), where 100 = 200/2 and 86.6 = sqrt((200*200)-(100*100))/2;
Create an inner class
Create an inner class, Triangle, which extends Polygon and has a constructor with signature Triangle(double x, double y, double side). This should create an upright equalateral triangle, centered at (x,y), with sides of length side.
Comment out the code you wrote in step 2 (that drew the triangle with Polygon), and replace it with the creation of a Triangle. Set the fill color of the new triangle to LIGHTGREY. Don't forget to add your triangle to the root group.
Fill the board with triangles
Fill the board with a grid of triangles, storing each triangle in an ArrayList<Triangle>. You should fit exactly three rows (each 173.2 pixels apart), each row having five triangles (three upright and two inverted). You will need to use the setRotate() method to invert some of the triangles.
Create a white boarder
Reduce the size of each triangle to 196 pixels, while maintaining the spacing as if they were size 200. This should create the effect of a white border around each triangle.
Explanation / Answer
Triangle.java
import javafx.scene.shape.Polygon;
public class Triangle extends Polygon {
public double x;
public double y;
private double side;
public Triangle(double x, double y, double side) {
this.x = x;
this.y = y;
this.side = side;
this.getPoints().addAll(x, y, (x+side),y,(x+side/2),y -(Math.sqrt(side*side - (side/2)*(side/2))));
this.setFill(javafx.scene.paint.Color.LIGHTGREY);
}
//Calculates the distance between a point (x,y) and the centre of the triangle
private double distance(double x, double y){
double centrex = this.x + this.side/2;
double centrey = this.y + Math.tan(Math.PI/6)*(centrex-this.x);
return (Math.sqrt((centrex-x)*(centrex-x)+(centrey-y)*(centrey-y)));
}
}
Board.java
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.ArrayList;
public class Board extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Board");
Group root = new Group();
Scene scene = new Scene(root,600,519);
primaryStage.setScene(scene);
ArrayList<Triangle> myarray = new ArrayList<Triangle>(20);
int ycount = (int) 86.6*2;
int xcount = 0;
for (int i = 0; i < 4; i++){
for (int x = 0; x < 5; x++){
Triangle tri = new Triangle(xcount,ycount,194);
if (xcount%200 ==100){
tri.setRotate(180);
}
myarray.add(tri);
xcount+=100;
}
xcount = 0;
ycount += 86.3*2;
}
for (Triangle x:myarray){
root.getChildren().add(x);
}
scene.setOnKeyTyped(event -> {
if (event.getCharacter().equals("q"))
Platform.exit();
});
primaryStage.show();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.