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

Answer:

JavaFX


Graphics (Review)

A single Red Rectangle, centered in the window

A JavaFX program extends the Application class. Your program must include a start() method to override the abstract start() method of that class. The method takes a parameter that is a reference to a Stage, which represents the window that the program controls.

The Stage is created by the graphics system and handed to your start() method. Your program does not have to create it.

A Stage displays a Scene, which contains a scene graph made up of the graphics objects to be displayed.

The the root of the scene graph is often a Group. Here is a program that draws a single rectangle. The scene graph consists of a Group which contains a Rectangle object.



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

public class DrawRectangle extends Application
{ 
  final double sceneWidth=400, sceneHeight= 300;  
  final double centerX=sceneWidth/2, centerY=sceneHeight/2;
 
  public void start(Stage stage) 
  { 
    double scale = 0.9;
    Group  root = new Group( );   
    double width=sceneWidth*scale, height=sceneHeight*scale;
    
    Rectangle rect = new Rectangle( centerX-width/2, centerY-height/2, width, height );
    rect.setStrokeWidth( 2.0 );
    rect.setStroke( Color.RED );
    rect.setFill( Color.TRANSPARENT );
    root.getChildren().add( rect );   
    Scene scene = new Scene(root, sceneWidth, sceneHeight, Color.GHOSTWHITE ); 
    stage.setTitle("One Rectangle"); 
    stage.setScene(scene); 
    stage.show(); 
  }      
  
} 

Here is the constructor for Rectangle objects.

Rectangle(double x, double y, double width, double height)

Create a rectangle with specified width and height with upper left corner at (x,y)

Parameters for the Red Rectangle, centered in the window

The program centers the rectangle in the scene by placing the upper left corner at half the width and half the height away from the center of the scene.


QUESTION 2:

In a JavaFX scene, where is the origin (0,0)?


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