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

Answer:

double[] vertices =  
{ w/4, h/4, 3*w/4, h/4, w/4, 3*h/4, 3*w/4, 3*h/4 };  

or

double[] verts =  
{ 50, 50, 150, 50, 50, 150, 150, 150 };   

Text

An acute and an obtuse angle

Sometimes you want text in your graphics. Here is the program that labels the angles in the above.

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 TextDemo extends Application
{
  public void start( Stage primaryStage )
  {
    double sceneWidth = 400.0, sceneHeight = 300.0;
           
    double[] verticesA = { 150,50,  50,175, 175,175}; 
    double[] verticesO = { 225,50, 275,175, 375,175}; 
    
    Polyline angleA = new Polyline( verticesA );
    angleA.setStrokeWidth( 2.0 );
    Polyline angleO = new Polyline( verticesO );
    angleO.setStrokeWidth( 2.0 );
    
    Text textA = new Text(  70, 200, "Acute Angle");
    textA.setFont( Font.font( "Arial", FontWeight.BOLD, FontPosture.ITALIC, 20 ) );

    Text textO = new Text( 230, 200, "Obtuse Angle");
    textO.setFont( Font.font( "Arial", FontWeight.BOLD, FontPosture.ITALIC, 20 ) );
    
    Pane pane = new Pane( angleA, angleO, textA, textO );
    Scene scene = new Scene( pane, sceneWidth, sceneHeight, Color.FLORALWHITE );
    
    primaryStage.setTitle("Acute Angle Demo");
    primaryStage.setScene( scene );
    primaryStage.show();
  }
}

A constructor looks like this:

Text(double x, double y, String text)

The lower left corner of the text is (x, y). The text will appear with the default font and size, usually too small. To set the characteristics of the Text, use the static method font() of the Font class.

textA.setFont( Font.font( "Arial", 
    FontWeight.BOLD, FontPosture.ITALIC, 20 );

The first parameter is the name of the font. This may be something like "Arial", "Courier New", "Times New Roman", "Comic Sans MS" and many others. The entire font name must be used. Merely saying "Courier" will not work (unlike some other web aps.)

The second parameter is the weight of the font. This may be FontWeight.LIGHT, FontWeight.NORMAL, FontWeight.BOLD and several others.

The third parameter is the posture of the font. This may be FontPosture.REGULAR, or FontPosture.ITALIC.

The last parameter is the size of the font in points. Larger values give you larger characters, but the actual size of the font depends on the settings of your monitor and the transformations that may be applied to your image.

The computer that runs the program must have the font installed. Otherwise, you get a default font. To get a list of available fonts, run this program:

import javafx.scene.text.*; // Needed for Text and Font
class FontLister
{
  public static void main ( String[] args )
  {
    for ( String font : 
      javafx.scene.text.Font.getFamilies() )
          System.out.println( font );
  }
}

Oracle Documentation for Text.

Oracle Documentation for Font.


QUESTION 13:

Hello World

Here is a familiar program. Complete it, so it matches the above output. You may have to guess at a few values.

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( 50, 100, "Hello World!");
    
    textH.setFont( Font.font( , FontWeight., 
                   FontPosture.,  ) );
    
    Pane pane = new Pane( textH );
    Scene scene = new Scene( pane, sceneWidth, sceneHeight, Color.FLORALWHITE );
    
    primaryStage.setTitle("Hello");
    primaryStage.setScene( scene );
    primaryStage.show();
  }
}

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