Wednesday, May 25, 2016

9. 4 Triangles

Four triangles are drawn using the Polygon class. Since we pass 6 values into the constructor, this corresponds to 3 vertices.


The four triangles have a common point (x,y) or (200, 200), with a different color. The four are defined counterclockwise, with poly1 at upper-right, with color of red.


package ex09;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Ex09 extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        double x = 200, y = 200, w = 100;
        
        double[] triangle1 = {x,y, x+w,y, x,y-w};
        Polygon poly1 = new Polygon(triangle1);
        poly1.setFill(Color.RED);
        
        double[] triangle2 = {x,y, x,y-w, x-100,y};
        Polygon poly2 = new Polygon(triangle2);
        poly2.setFill(Color.GREEN);
        
        double[] triangle3 = {x,y, x-w,y, x,y+w};
        Polygon poly3 = new Polygon(triangle3);
        poly3.setFill(Color.BLUE);
        
        double[] triangle4 = {x,y, x+w,y, x,y+w};
        Polygon poly4 = new Polygon(triangle4);
        poly4.setFill(Color.FUCHSIA);
        
        Group root = new Group();
        root.getChildren().addAll(poly1,poly2,poly3,poly4);
        
        Scene scene = new Scene(root, 400, 400, Color.GHOSTWHITE);
        
        primaryStage.setTitle("Example 9. 4 Triangles");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

This is the output:


No comments:

Post a Comment