go to previous page   go to home page   go to next page highlighting

Answer:

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.Scene;  
import javafx.scene.shape.*;
import javafx.scene.text.*; // Needed for Text and Font
import javafx.scene.paint.*;
import javafx.scene.layout.*; 

public class TextHelloWorld extends Application
{
  public void start( Stage primaryStage )
  {
    double sceneWidth = 300.0, sceneHeight = 200.0;
     
    Text textH = new Text( 45, 100, "Hello World!");
    textH.setFont( Font.font( "Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 40 ) );
    
    Pane pane = new Pane( textH );
    Scene scene = new Scene( pane, sceneWidth, sceneHeight, Color.FLORALWHITE );
    
    primaryStage.setTitle("Hello");
    primaryStage.setScene( scene );
    primaryStage.show();
  }
}

Rotation

Rotated Ellipse

By default, a Shape is aligned with the X and Y axis of the window. But what if you want an ellipse (or other figure) with axes not aligned with X and Y? You can ask for the shape to be rotated about its center. The rotation is given in degrees clockwise.

setRotate( double degrees )

Set the rotation angle of the Node to degrees. Rotation is clockwise about the center of the Node.

This sets the rotation property, it does not change the location or shape of the object. When the object is rendered (drawn on the screen), the rotation affects how the object appears. All Nodes have a rotation property, so rotation can be set for any subclass of Node, including Shape, Group, Region, and others.


import javafx.application.*;
import javafx.stage.*;
import javafx.scene.Scene;  
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.layout.*; 

public class EllipseRotate extends Application
{
  public void start( Stage primaryStage )
  {
    double width = 400.0, height = 350.0;
    double centerX = width/2; 
    double centerY = height/2; 
    double radiusX = width/3;   // note change
    double radiusY = height/4;
    
    Ellipse ellipse = new Ellipse( centerX,  centerY,  radiusX,  radiusY );
    ellipse.setFill( Color.AQUA );
    ellipse.setRotate( 30.0 );  // rotation in degrees about center of Ellipse
    
    Pane pane = new Pane( ellipse );
  
    Scene scene = new Scene( pane, width, height, Color.CORNSILK );
    
    primaryStage.setTitle("Rotate Ellipse 30 deg clockwise");
    primaryStage.setScene( scene );
    primaryStage.show();
  }
} 

There are other transformations available. As with rotation, they set a property of a Node and do not change the other properties. But they do change how the Node (and its children, if any) are rendered.

A shape may be translated (moved) from its current location.

setTranslateX( double pixelsX )

Move the center of the shape by pixelsX number of pixels along the X axis.

setTranslateY( double pixelsY )

Move the center of the shape by pixelsY number of pixels along the Y axis. (Recall that values on the Y axis increase going downward.)

A shape may be scaled (zoomed) in its X and Y dimensions.

setScaleX( double scaleX )

Scale X dimensions the shape by the scale factor. Scaling is done relative to the center of the shape, so a scale factor of 0.5 will leave the center where it is but shrink all X dimensions by one half. A scale factor of 2.0 will double the X dimensions.

setScaleY( double scaleY )

Scale Y dimensions the shape by the scale factor. Scaling is done relative to the center of the shape, so a scale factor of 0.5 will leave the center where it is but shrink all Y dimensions by one half. A scale factor of 2.0 will double the Y dimensions.

If the X and Y scale factors are the same, the entire shape is uniformly scaled as if zoomed by the scale factor.


QUESTION 14:

Square at lower Right

Here is a program that constructs a square with its upper left corner at (X=0, Y=0). The constructor used for Rectangle is the one that takes just two parameters: width and height. This automatically puts the upper left corner at (X=0, Y=0).

You can translate the square to some other location. Fix the program below so that the lower right corner of the square is close to the lower right corner of the window.

Leave a 5 pixel gap between the edges of the window and the square.


import javafx.application.*;
import javafx.stage.*;
import javafx.scene.Scene;  
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.layout.*; 

public class SquareTranslate extends Application
{
  public void start( Stage primaryStage )
  {
    double width = 400.0, height = width*0.75 ;
       
    double side = width/3.0; 
    double gap = 5.0;
        
    Rectangle rect = new Rectangle( side, side ); 
    rect.setFill( Color.CHOCOLATE );
    rect.setStroke( Color.BLACK );
    
    rect.setTranslateX(   );
    rect.setTranslateY(  );
     
    Pane pane = new Pane( rect ); 
    Scene scene = new Scene( pane, width, height, Color.LIGHTCYAN );
    
    primaryStage.setTitle("Translated Rectangle");
    primaryStage.setScene( scene );
    primaryStage.show();
  }
}

You might wish to copy and paste the program to a file and run it to see if your fix works. Your first attempt is almost certain to be wrong.


go to previous page   go to home page   go to next page