Over a million developers have joined DZone.

Simple JavaFX 2.0 Text Example

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Oracle announced at JavaOne 2010 that they would be deprecating JavaFX Script and changing JavaFX to support standard Java APIs. In my previous post Hello JavaFX 2.0: Introduction by Command Line, I demonstrated that JavaFX 2.0 has definitely put the 'Java' back into JavaFX. This post continues that theme, showing examples of rendering various text effects, fonts, and sizes via JavaFX 2.0 using "pure Java" APIs. The example in this post can be compiled with the javac compiler and run with the java application launcher (assuming appropriate JavaFX library is available in classpath in both cases).

The JavaFX APIs for programmatically handling text in JavaFX 2.0 are easy to use for basic applications, but provide support for more sophisticated effects as the developer becomes comfortable with the APIs. The examples shown in the single application featured in this post are relatively straightforward and likely require little explanation.

package dustin.examples;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * Simple demonstration of JavaFX text support.
 * 
 * @author Dustin
 */
public class JavaFxText extends Application
{
   /**
    * Start demonstration of JavaFX text capabilities.
    * 
    * @param stage Stage for JavaFX application.
    * @throws Exception Exception in JavaFX application.
    */
   @Override
   public void start(final Stage stage) throws Exception
   {
      stage.setTitle("Simplistic Example of JavaFX 2.0 Text Capabilities");
      final Group rootGroup = new Group();
      final Scene scene =
         new Scene(rootGroup, 800, 400, Color.BEIGE);
 
      final Text text1 = new Text(25, 25, "(2007) JavaFX based on F3");
      text1.setFill(Color.CHOCOLATE);
      text1.setFont(Font.font(java.awt.Font.SERIF, 25));
      rootGroup.getChildren().add(text1);

      final Text text2 = new Text(25, 50, "(2010) JavaFX Script Deprecated");
      text2.setFill(Color.DARKBLUE);
      text2.setFont(Font.font(java.awt.Font.SANS_SERIF, 30));
      rootGroup.getChildren().add(text2);

      final Text text3 = new Text(25, 75, "(2011) JavaFX to be Open Sourced!");
      text3.setFill(Color.TEAL);
      text3.setFont(Font.font(java.awt.Font.MONOSPACED, 35));
      rootGroup.getChildren().add(text3);

      final Text text4 = new Text(25, 125, "(2011) JavaFX to be Standardized");
      text4.setFill(Color.CRIMSON);
      text4.setFont(Font.font(java.awt.Font.DIALOG, 40));
      final Effect glow = new Glow(1.0);
      text4.setEffect(glow);
      rootGroup.getChildren().add(text4);

      final Text text5 = new Text(25, 175, "(Now) Time for JavaFX 2.0!");
      text5.setFill(Color.DARKVIOLET);
      text5.setFont(Font.font(java.awt.Font.SERIF, FontWeight.EXTRA_BOLD, 45));
      final Light.Distant light = new Light.Distant();
      light.setAzimuth(-135.0);
      final Lighting lighting = new Lighting();
      lighting.setLight(light);
      lighting.setSurfaceScale(9.0);
      text5.setEffect(lighting);
      rootGroup.getChildren().add(text5);

      final Text text6 = new Text(25, 225, "JavaFX News at JavaOne!");
      text6.setFill(Color.DARKGREEN);
      text6.setBlendMode(BlendMode.COLOR_BURN);
      text6.setFont(Font.font(java.awt.Font.DIALOG_INPUT, FontWeight.THIN, 45));
      final Reflection reflection = new Reflection();
      reflection.setFraction(1.0);
      text6.setEffect(reflection);
      rootGroup.getChildren().add(text6);
 
      stage.setScene(scene);
      stage.show();
   }

   /**
    * Main JavaFX application launching method.
    * 
    * @param arguments Command-line arguments: none expected.
    */
   public static void main(final String[] arguments)
   {
      Application.launch(arguments);
   }
}

All of the interesting code occurs in the overridden start(Stage) method of this class that extends Application. Different effects (glow, lighting, reflection), font sizes, and font types are demonstrated in this example. The output from running this application from the command line is shown next.

The example highlighted in this post demonstrates a subset of JavaFX 2.0's support for text rendering. It also is another example of how JavaFX 2.0 has made JavaFX readily available via Java APIs.

 

From http://marxsoftware.blogspot.com/2011/12/simple-javafx-20-text-example.html

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}