Skip to content
Snippets Groups Projects
Commit 43e98dda authored by kaller01's avatar kaller01
Browse files

init

parents
No related branches found
No related tags found
No related merge requests found
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("JavaFX nodes");
// Horizontal box, nodes will stack on x axis
HBox root = new HBox(); // Create root element for scene, ex group
root.setMinWidth(600);
root.setMinHeight(600);
root.setSpacing(20);
root.setBackground(new Background(new BackgroundFill(Color.GREEN, null, null))); //Look for a green box
//These are nodes that can be put in a layout, read docs.
TextField field = new TextField();
Button button = new Button("Click me!");
//Canvas is special, because you can draw on it.
Canvas canvas = new Canvas();
canvas.setHeight(200);
canvas.setWidth(200);
//Using the graphics context, you can draw. Canvas has x and y coordinates.
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
//Vertical box, nodes will stack on y axis.
VBox box = new VBox();
box.setMinHeight(300);
box.setMinWidth(300);
box.setMaxHeight(400);
box.setBackground(new Background(new BackgroundFill(Color.PINK, null, null))); //Look for a pink box!
// This is a listener, when you press the button this code will execute
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
String text = field.getText();
Label label = new Label(text);
box.getChildren().add(label);
field.setText("");
//Special case, try it
if(text.equals("TDDC77")){
gc.setFill(Color.LIGHTBLUE);
gc.fillOval(50, 50, 100, 100);
}
}
});
Scene scene = new Scene(root); // Create the scene with root element
primaryStage.setScene(scene);
root.getChildren().add(field); //put field in green box
root.getChildren().add(button); //put button in green box
root.getChildren().add(box); //put pink box in green box
root.getChildren().add(canvas); //put canvas in green box
box.getChildren().add(new Label("Hello world")); //put label in pink box
//Last thing to do is show
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment