Wednesday, May 14, 2014

JavaFX 8 Rectangle example

JavaFX 8 example to draw Rectangles with filled color, transparent with stroke, and no fill color specified, with stroke.

package javafx8_shape;

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

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Shape extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 500, Color.BLACK);

        //Filled rectangle
        Rectangle rect1 = new Rectangle(10, 10, 200, 200);
        rect1.setFill(Color.BLUE);
        
        //Transparent rectangle with Stroke
        Rectangle rect2 = new Rectangle(60, 60, 200, 200);
        rect2.setFill(Color.TRANSPARENT);
        rect2.setStroke(Color.RED);
        rect2.setStrokeWidth(10);
        
        //Rectangle with Stroke, no Fill color specified
        Rectangle rect3 = new Rectangle(110, 110, 200, 200);
        rect3.setStroke(Color.GREEN);
        rect3.setStrokeWidth(10);
        
        root.getChildren().addAll(rect1, rect2, rect3);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}






You can still use RectangleBuilder, but it is deprecated and will be removed in the next version.


package javafx8_shape;

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

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Shape extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 500, Color.BLACK);

        //Filled rectangle
        Rectangle rect1 = new Rectangle(10, 10, 200, 200);
        rect1.setFill(Color.BLUE);
        
        //Transparent rectangle with Stroke
        Rectangle rect2 = new Rectangle(60, 60, 200, 200);
        rect2.setFill(Color.TRANSPARENT);
        rect2.setStroke(Color.RED);
        rect2.setStrokeWidth(10);
        
        //Rectangle with Stroke, no Fill color specified
        Rectangle rect3 = new Rectangle(110, 110, 200, 200);
        rect3.setStroke(Color.GREEN);
        rect3.setStrokeWidth(10);
        
        //Create Rectangle using deprecated RectangleBuilder.
        Rectangle rect4 = RectangleBuilder.create()
              .fill(Color.WHITESMOKE)
              .x(160)
              .y(160)
              .width(200)
              .height(200)
              .strokeWidth(10)
              .stroke(Color.BLUEVIOLET)
              .build();
        
        root.getChildren().addAll(rect1, rect2, rect3, rect4);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}



No comments:

Post a Comment