Tuesday, May 24, 2016

2. Six Lines

A Group node is used to paint 6 lines on the screen.


There are 2 horizontal red lines, 2 vertical green lines, and 2 diagonal lines. Both diagonal lines are dashed, while the others are solid.


package ex02;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeLineCap;
import javafx.stage.Stage;

public class Ex02 extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        primaryStage.setTitle("Example 2. Six Lines");        

        Group root = new Group();
        
        Scene scene = new Scene(root, 640, 480, Color.CYAN);
        
        // Red line - top and bottom
        Line redTop = new Line(10, 10, 630, 10);
        redTop.setStroke(Color.RED);
        redTop.setStrokeWidth(10);
        Line redBot = new Line(10, 470, 630, 470);
        redBot.setStroke(Color.RED);
        redBot.setStrokeWidth(10);
        root.getChildren().addAll(redTop,redBot);
        
        // Green Line -left and right
        Line greenLeft = new Line(10, 30, 10, 450);
        greenLeft.setStroke(Color.GREEN);
        greenLeft.setStrokeWidth(10);
        greenLeft.setStrokeLineCap(StrokeLineCap.ROUND);
        Line greenRight = new Line(630, 30, 630, 450);
        greenRight.setStroke(Color.GREEN);
        greenRight.setStrokeWidth(10);
        greenRight.setStrokeLineCap(StrokeLineCap.ROUND);
        root.getChildren().addAll(greenLeft,greenRight);
        
        // Yellow lines - diagonal lines
        Line yellow1 = new Line(30, 30, 610, 450);
        yellow1.setStroke(Color.YELLOW);
        yellow1.setStrokeWidth(10);
        yellow1.getStrokeDashArray().addAll(20d, 10d, 20d);
        Line yellow2 = new Line(30, 450, 610, 30);
        yellow2.setStroke(Color.YELLOW);
        yellow2.setStrokeWidth(10);
        yellow2.getStrokeDashArray().addAll(20d, 10d, 20d);
        root.getChildren().addAll(yellow1,yellow2);
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

This is the output:


No comments:

Post a Comment