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

Answer:

There is one node in the scene graph for each short line segment (plus one for the Group), for a total of 65 in the above program.


Rotation about Center

One blue line, rotated 45 degrees clockwise

Node objects (recall) have a rotation property that you can set to a value in degrees. When the Node is rendered, the rotation is applied to it, and its children, if any. Here is the program that produced the above picture.

This version of addStar() adds a line to the Group. The parameters of the line are its center and size.

The method constructs a horizontal Line. The endpoints of the line are easy to calculate in this case. Then the Line is rotated. The system does the trigonometry, not us!


 
import javafx.application.*;  
import javafx.stage.*;        
import javafx.scene.*;  
import javafx.scene.shape.*; 
import javafx.scene.paint.*;  
 
public class OneLineRotate extends Application
{ 
  
  void addStar( Group root, Color color, double centerX, double centerY, double size )
  { 
    double startX, endX, startY, endY;
    
    // horizontal line, centered at (centerX, centerY), of length size
    startX = centerX - size/2;
    endX = centerX + size/2;
    Line line = new Line( startX, centerY, endX, centerY );
    
    // set the rotation of the line to +45 degrees
    line. 
    
    line.setStrokeWidth( 2.0 );
    line.setStroke( color );
    root.getChildren().add( line );  
  
  }
  
  public void start(Stage stage) 
  { 
    double sceneWidth=400, sceneHeight= 300;  
    Group  root = new Group( );   
  
    addStar( root, Color.BLUE, sceneWidth/2, sceneHeight/2, sceneWidth*0.6 );
    
    Scene scene = new Scene(root, sceneWidth, sceneHeight, Color.SNOW ); 
    stage.setTitle("Line, rotated 45 deg clockwise"); 
    stage.setScene(scene); 
    stage.show(); 
  }      

}   


QUESTION 13:

But the program is not finished! Fill in the blank to finish the program. (And then run it, if you want.)

Use the setRotate() method.


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