Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaFX 2 Working with Text
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.effect.InnerShadow;
import javafx.scene.effect.Reflection;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
*
* @author zoranpavlovic.blogspot.com
*/
public class TextMain extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Working with Text and Text Effects");
//Creating VBOX
VBox vb = new VBox();
vb.setPadding(new Insets(10, 50, 50, 50));
vb.setSpacing(20);
//Reflection effect
Reflection r = new Reflection();
r.setFraction(0.7f);
Text txtReflection = new Text("Reflection");
txtReflection.setFill(Color.RED);
txtReflection.setFont(Font.font(null, FontWeight.BOLD, 36));
txtReflection.setEffect(r);
//DropShadow effect
DropShadow dropShadow = new DropShadow();
dropShadow.setOffsetX(5);
dropShadow.setOffsetY(5);
Text txtDropShadow = new Text("DropShadow");
txtDropShadow.setFill(Color.RED);
txtDropShadow.setFont(Font.font(null, FontWeight.BOLD, 36));
txtDropShadow.setEffect(dropShadow);
//Blur text
Text txtBlur = new Text();
txtBlur.setText("Blurry Text with JavaFX 2");
txtBlur.setFill(Color.RED);
txtBlur.setFont(Font.font(null, FontWeight.BOLD, 36));
txtBlur.setEffect(new GaussianBlur());
//InnerShadow
InnerShadow is = new InnerShadow();
Text txtInnerShadow = new Text();
txtInnerShadow.setEffect(is);
txtInnerShadow.setText("InnerShadow");
txtInnerShadow.setFill(Color.RED);
txtInnerShadow.setFont(Font.font(null, FontWeight.BOLD, 36));
//Adding text to StackPane
vb.getChildren().addAll(txtDropShadow, txtBlur, txtInnerShadow, txtReflection);
//Adding StackPane to the scene
Scene scene = new Scene(vb);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.