
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Explain the Stroke property of 2D shapes in JavaFX
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.
This method accepts a Color value as a parameter and sets the given color to the boundary of a shape.
By default, the value of this property for the shapes (objects) line, path and polyline is null and, for all the remaining shapes the default value of this property is Color.BLACK.
Example
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.Polygon; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; public class StrokeExample extends Application { public void start(Stage stage) { Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12); Text label1 = new Text("Original Image"); label1.setFont(font); label1.setX(250.0); label1.setY(125.0); Polygon rhombus1 = new Polygon(300.0, 0.0, 250.0, 50.0, 300.0, 100.0, 350.0, 50.0); Text label2 = new Text("Stroke for a line"); label2.setFont(font); label2.setX(50.0); label2.setY(275.0); //Constructing a line Line line = new Line(); line.setStartX(50.0); line.setStartY(200.0); line.setEndX(150.0); line.setEndY(200.0); line.setStrokeWidth(10.0); line.setStroke(Color.DARKBLUE); Text label3 = new Text("Stroke: Blue"); label3.setFont(font); label3.setX(250.0); label3.setY(275.0); Polygon rhombus3 = new Polygon(300.0, 150.0, 250.0, 200.0, 300.0, 250.0, 350.0, 200.0); rhombus3.setStroke(Color.BLUE); rhombus3.setStrokeWidth(7.0); Text label4 = new Text("Stroke: Red"); label4.setFont(font); label4.setX(450.0); label4.setY(275.0); Polygon rhombus4 = new Polygon(490.0, 150.0, 440, 200.0, 490.0, 250.0, 540.0, 200.0); rhombus4.setStroke(Color.RED); rhombus4.setStrokeWidth(7.0); //Creating a Group object Group root = new Group(label1, label2, label3, label4, rhombus1, line, rhombus3, rhombus4); //Creating a scene object Scene scene = new Scene(root, 595, 310); //Setting title to the Stage stage.setTitle("Stroke Example"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- Explain the Stroke Type property of 2D shapes in JavaFX
- Explain the Stroke Width property of 2D shapes in JavaFX
- Explain the Stroke Line Join property of 2D shapes in JavaFX
- Explain the Stroke Miter Limit property of 2D shapes in JavaFX
- Explain the Stroke Line Cap property of 2D shapes in JavaFX
- Explain the stroke Dash Offset property of 2D shapes in JavaFX
- Explain the Fill property of 2D shapes in JavaFX
- Explain the smooth property of 2D shapes in JavaFX
- Explain the Union operation on 2D shapes in JavaFX
- Explain the Intersect operation on 2D shapes in JavaFX
- Explain the Subtract operation on 2D shapes in JavaFX
- Explain the cull face property of 3D shapes in JavaFX
- Explain the material face property of 3D shapes in JavaFX
- Explain the drawing mode face property of 3D shapes in JavaFX
- What are various 2D shapes provided by JavaFX?

Advertisements