JavaFX 기초
1. JavaFX 프로그램의 기본 구조
간단한 JavaFX 프로그램 코드를 짜면서 기본 구조에 대해 알아보겠다!
(모든 JavaFX 프로그램은 javafx.application.Application 을 상속받는 class 안에 정의된다.
Listing 14.1
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class MyJavaFX extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // scene을 만들고 scene 안에 버튼을 위치시킨다. Button btOK = new Button("OK"); Scene scene = new Scene(btOK, 200, 250); primaryStage.setTitle("MyJavaFX"); // stage 제목을 붙인다. primaryStage.setScene(scene); // stage 안에 scene 을 위치시킨다. primaryStage.show(); // stage 을 display } /** * The main method is only needed for the IDE with limited JavaFX support. * Not needed for running from the command line. */ public static void main(String[] args) { Application.launch(args); } }
import javafx.application.Application; // extends Application을 하기 위해 import javafx.scene.Scene; // Scene scene = new Scene(btOK, 200, 250); 부분에서 필요 import javafx.scene.control.Button; // Button btOK = new Button("OK"); import javafx.stage.Stage; // Stage primaryStage 부분에서 필요
launch 함수는 Application 클래스에 정의되어 있고 main 함수 안에서 쓰여지지만, javafx에서는 프로그램을 진행(run)하기 위해서 굳이 main 함수가 있을 필요가 없
Button 객체는 Scene(node, width, height) 생성자를 이용해서 scene에 위치될 수 있다.
Stage 와 Scene, node를 영화관에 비유하자면, stage는 scene을 보여주기 위한 플랫폼이고 node는 scene안에서의 배우라고 생각하면 된다.
Listing 14.2
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class MultipelStageDemo extends Application{ @Override public void start(Stage primaryStage) { //scene 만들고 button을 scene에 바로 추가 Scene scene = new Scene(new Button("OK"), 200, 250); primaryStage.setTitle("MyJavaFXㅇㅇ"); // stage 제목 붙임 primaryStage.setScene(scene); //stage 안에 scene 추가 primaryStage.show(); // stage 보여주기 Stage stage = new Stage(); // 새로운 stage 만듬 stage.setTitle("Second Stage"); // stage 제목 붙임 // button 추가와 함께 scene 을 stage 에 추가 stage.setScene(new Scene(new Button("New Stage"), 100, 100)); stage.show(); // stage 를 보여주기 } }
14.2는 stage를 2개 만든 코드이며, 여기서는 main 함수가 빠졌다.
Listing 14.3
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; import javafx.scene.layout.StackPane; public class Test extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // scene 만들고 scene 안에 버튼 추기 StackPane pane = new StackPane(); pane.getChildren().add(new Button("OK")); Scene scene = new Scene(pane, 200, 50); primaryStage.setTitle("Button in a pane"); // stage 제목 붙임 primaryStage.setScene(scene); // stage안에 scene 추가 primaryStage.show(); // display } }
Listing 14.4
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; public class ShowCircle extends Application{ @Override // Override the start method in the Application class // Create a circle and set its properties public void start(Stage primaryStage) { Circle circle = new Circle(); circle.setCenterX(100); circle.setCenterY(100); // 중심의 좌표 (100, 100) circle.setRadius(50); // 구의 반지름 : 50 circle.setStroke(Color.BLACK); circle.setFill(Color.WHITE); //Create a pane to hold the circle Pane pane = new Pane(); pane.getChildren().add(circle); // Create a scene and place it in the stage Scene scene = new Scene(pane, 200, 200); primaryStage.setTitle("ShowCircle"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); //display } }
Listing 14.5
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; public class Test extends Application{ @Override public void start(Stage primaryStage) { Pane pane = new Pane(); Circle circle = new Circle(); circle.centerXProperty().bind(pane.widthProperty().divide(2)); circle.centerYProperty().bind(pane.heightProperty().divide(2)); circle.setRadius(50); circle.setStroke(Color.BLACK); circle.setFill(Color.WHITE); pane.getChildren().add(circle); Scene scene = new Scene(pane, 250, 250); primaryStage.setTitle("ShowCircleCentered"); primaryStage.setScene(scene); primaryStage.show(); } }
bind 함수는 javafx.beans.property.Property인터페이스 안에 정의되어 있다.
Listing 14.7
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class NodeStyleRotateDemo extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // scene 을 만들고 button 을 scene 에 위치시킨다. StackPane pane = new StackPane(); // 새로운 pane의 class 등장??? Button btOK = new Button("OK"); btOK.setStyle("-fx-border-color: blue;"); pane.getChildren().add(btOK); pane.setRotate(45); pane.setStyle("-fx-border-color: red; -fx-background-color: lightgray;"); Scene scene = new Scene(pane, 200, 250); primaryStage.setTitle("NodeStyleRotateDemo"); // stage title 설정 primaryStage.setScene(scene); // scene 을 stage에 놓는다. primaryStage.show(); // display }
}
} }
Q. How to create a Color object with a random color? - frome StackOverFlow
Use the random library:
import java.util.Random;
Then create a random generator:
Random rand = new Random();
As colours are separated into red green and blue, you can create a new random colour by creating random primary colours:
// Java 'Color' class takes 3 floats, from 0 to 1.
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Then to finally create the colour, pass the primary colours into the constructor:
Color randomColor = new Color(r, g, b);
You can also create different random effects using this method, such as creating random colours with more emphasis on certain colours ... pass in less green and blue to produce a "pinker" random colour.
// Will produce a random colour with more red in it (usually "pink-ish")
float r = rand.nextFloat();
float g = rand.nextFloat() / 2f;
float b = rand.nextFloat() / 2f;
Or to ensure that only "light" colours are generated, you can generate colours that are always > 0.5 of each colour element:
// Will produce only bright / light colours:
float r = rand.nextFloat() / 2f + 0.5;
float g = rand.nextFloat() / 2f + 0.5;
float b = rand.nextFloat() / 2f + 0.5;
There are various other colour functions that can be used with the Color
class, such as making the colour brighter:
randomColor.brighter();
Listing 14.8
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; import javafx.scene.shape.Circle; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.stage.Stage; import javafx.scene.paint.Color; public class FontDemo extends Application { @Override // Override start method in the Application Class public void start(Stage primaryStage) { // circle을 가진 pane 생성 Pane pane = new StackPane(); // circle 생성하고 특징 생성 Circle circle = new Circle(); circle.setRadius(50); circle.setStroke(Color.BLACK); circle.setFill(new Color(0.5, 0.5, 0.5, 0.1)); pane.getChildren().add(circle); // pane에 circle 추가 // label 생성 & 특징 set Label label = new Label("JavaFX"); label.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 20)); pane.getChildren().add(label); // scene 을 생성하고 stage 에 위치시킴 Scene scene = new Scene(pane); primaryStage.setTitle("FontDemo"); // stage 제목 붙임 primaryStage.setScene(scene); // stage 에 scene 을 위치 primaryStage.show(); // display } }
Listing 14.9
Listing 14.10