Sunday, October 26, 2014

JavaFX Nr.3

(Color and font) Write a program that displays five texts vertically, as shown in figure below. Set a random color and opacity for each text and set the font of each text to Times Roman, bold, italic, and 22 pixels.


Solution:


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TextJava extends Application {

@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 22);
Text[] texts = new Text[5];

for(int i = 0; i < texts.length; i++) {
texts[i] = new Text(20 * i, 50, "JAVA");
texts[i].setFont(font);
texts[i].setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));
texts[i].setRotate(90);
pane.getChildren().add(texts[i]);
}

Scene scene = new Scene(pane, 100, 100);
primaryStage.setTitle("JAVA with color");
primaryStage.setScene(scene);
primaryStage.show();

}

public static void main(String[] args) {
launch(args);
}
}

No comments:

Post a Comment

Author