Explain the Properties of 2D objects in JavaFX


For all the 2-Dimensional objects, you can set various properties.

  • Stroke Type − The stroke type property specifies/defines the type of the boundary line of a shape. You can set the stroke type using the setStrokeType() method of the Shape class.

    JavaFX supports three kinds of strokes represented by three constants of the Enum named StrokeType namely, StrokeType.INSIDE, StrokeType.OUTSIDE, StrokeType.CENTERED.

  • Stroke Width − The stroke width property specifies/defines the width of the boundary line of a shape. You can set the value to the width of the boundary using the setWidth() method of the Shape class.

  • Fill − The fill property specifies/defines the color with which the interior area of the shape is to be filled. You can fill a particular shape with the desired color using the fill() method of the Shape class.

  • Stroke − The stroke property specifies/defines the color of the boundary of a shape. You can set the color of the boundary using the setStroke() method of the javafx.scene.shape.Shape class.

  • Stroke Line Cap − The Stroke Line Cap specifies/defines the end cap style of the line. You can set the Stroke Line Cap value using the setStrokeLineCap() method of the javafx.scene.shape.Shape class.

    Java FX supports three kinds of stroke line caps represented by three constants of the Enum named StrokeLineCap namely, BUTT, ROUND, SQUARE.

  • strokeLineJoin − In shapes formed by joining more than one line, the stroke line join property specifies/defines the shape of the joint of the two lines. You can set the stoke line join using the setStrokeLineJoin() method.

    Java FX supports three kinds of stroke line joins represented by three constants of the Enum named StrokeLineJoin namely, BEVEL, MITER, ROUND.

  • Stroke Miter Limit − The Stroke Miter Limit property specifies/defines the limit for the stroke line join in the StrokeLineJoin.MITER style. You can set this value using the setStrokeMiterLimit() method of the javafx.scene.shape.Shape class.

    This method accepts a double value and limits the stroke miter limit to the given value. If the given value is less than 1.0. It is considered as 1.0.

  • Smooth − The smooth property specifies whether antialiasing hints are used or not. You can set the value to this property using the setSmooth() method of the javafx.scene.shape.Shape class.

  • strokeDashOffset − If the stroke used is a dashing pattern the strokeDashOffset property specifies the offset into the dashing pattern.

Example

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.shape.StrokeLineJoin;
import javafx.scene.shape.StrokeType;
public class ShapeProperties extends Application {
   public void start(Stage stage) {
      //Drawing a Rectangle
      Rectangle rect = new Rectangle(100.0, 75.0, 400.0, 150.0);      
      //2D shape properties
      rect.setStrokeWidth(25.0);
      rect.setStrokeType(StrokeType.OUTSIDE);
      rect.setStroke(Color.BLUE);
      rect.setStrokeMiterLimit(8.0);
      rect.setStrokeLineCap(StrokeLineCap.ROUND);
      rect.setStrokeLineJoin(StrokeLineJoin .MITER);
      rect.setSmooth(false);
      //Preparing the Stage object
      Group root = new Group(rect);
      Scene scene = new Scene(root, 600, 300);
      stage.setTitle("Shape Properties Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 13-Apr-2020

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements