×

JavaFX Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis. It shears the given node along the axis. All methods needed for this purpose are present in the javafx.scene.transform.Shear class. Shearing transformation is used to change the slope of the given object. There are 2 types of transformation namely X-shear and Y-shear.

Properties and corresponding methods to create shear Transformation:

1. x      

This is the double type of property. It is used to represent the x coordinate of the shear property of the object and the setX( Double value ) method is used to set it.

2. y

This is a double type of property. It is used to represent the y coordinate of the shear property of the object and the setY( Double value ) method is used to set it.

3. pivotX      

This is a double type of property. It is used to represent the x coordinate of the pivot point of the shear property and the setPivotX( Double value ) method is used to set it.

4. pivotY

This is a double type of property. It is used to represent the y coordinate of the pivot point of the shear property and the setPivotY( Double value ) method is used to set it.

JavaFX Transformation – Shear Transformation on Rectangle:

Example:

import javafx.application.Application;  
import javafx.scene.Group;  
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.transform.Shear;
import javafx.stage.Stage;  


public class TransformUI extends Application {  


    @Override  
    public void start(Stage stage) throws Exception 
    { 
    
         Rectangle rect1 = new Rectangle( 60, 100, 150, 200);  
         Rectangle rect2 = new Rectangle( 350, 100, 150, 200);  
         Rectangle rect3 = new Rectangle( 640, 100, 150, 200);  
           
         Text text1 = new Text(" After X shear ");  
         Text text2 = new Text(" Original ");  
         Text text3 = new Text(" After Y shear ");  
           
         text1.setX( 70 );  
         text1.setY( 370 );  
         text2.setX( 380 );  
         text2.setY( 370 );  
         text3.setX( 640 );  
         text3.setY( 370 );  
 text1.setFont(Font.font(" calibri ", FontWeight.BOLD, FontPosture.ITALIC, 20));  
 text2.setFont(Font.font(" calibri ", FontWeight.BOLD, FontPosture.ITALIC, 20));  
 text3.setFont(Font.font(" calibri ", FontWeight.BOLD, FontPosture.ITALIC, 20));  
   
         rect1.setFill( Color.BLUE );  
         rect1.setStroke( Color.BLACK );  
         rect2.setFill( Color.DARKGRAY );  
         rect2.setStroke( Color.BLACK );  
         rect3.setFill( Color.PINK );  
         rect3.setStroke( Color.BLACK );  
           
         Shear shearX = new Shear();  
           
         
      shearX.setPivotX( 200 );   
     shearX.setPivotY( 250 );  
     shearX.setX( 0.3 );   
     shearX.setY( 0.0 );  
      rect1.getTransforms().add(shearX);  
       
         Shear shearY = new Shear();  
           
         shearY.setPivotX(600);  
         shearY.setPivotY(80);  
         shearY.setX(0.0);  
         shearY.setY(0.2);  
           
         rect3.getTransforms().add(shearY);  
         Group root = new Group();  
         root.getChildren().addAll( rect1, rect2, rect3, text1, text2, text3);  
         Scene scene = new Scene(root, 900, 450);  
         stage.setScene(scene);  
        stage.setTitle(" JavaFx Shear Effect example ");  
         stage.show();  
     }
      
    public static void main(String[] args) {  
        launch(args);  
    }  
}

Output:

JavaFX Effect – Transformation

Explanation:

In order to create shear transformation on rectangle to slant shape in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Group, javafx.scene.Scene, javafx.scene.paint.Color, javafx.scene.text.Font, javafx.scene.text.FontPosture, javafx.scene.text.FontWeight, javafx.scene.text.Text, javafx.scene.shape.Rectangle, javafx.scene.transform.Shear, javafx.stage.Stage. 

Then we have created one class named TransformUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a rectangle with and without the shear transformation, a Group object is created which is then passed to the Scene class object.

The rectangle is created using the constructor and various properties are set using setters. Also, the text is created using the constructor, and various properties are set. The shear transformation constructor is created and then using the setters various properties are set.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX Shear Effect example”. Also, it displays the rectangle with and without the shear transformation.


Related Topics

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 minutes read.

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

4 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

4 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Rotate Transition

In the JavaFX application, in order to generate the rotate transition, we have to apply rotation transition on any particular shape. It has the ability to rotate the shape along...

4 minutes read.

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 minutes read.

JavaFX Color Input

In the JavaFX application, in order to apply various color input effect, we use the ColorInput class. It has the ability to fill the specified shape with a given color...

4 minutes read.

JavaFX Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.