Skip to content

Instantly share code, notes, and snippets.

@alexanderankin
Created November 16, 2015 16:50
Show Gist options
  • Save alexanderankin/7a5355162644db48e859 to your computer and use it in GitHub Desktop.
Save alexanderankin/7a5355162644db48e859 to your computer and use it in GitHub Desktop.
JavaFX FXML Basic Example
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package animations;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author toor
*/
public class Animations extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader spinnerSceneLoader = new FXMLLoader(getClass().getResource("Spinner.fxml"));
Parent root = (Parent) spinnerSceneLoader.load();
SpinnerController ctrlrPointer = (SpinnerController) spinnerSceneLoader.getController();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.text.TextAlignment?>
<AnchorPane id="AnchorPane" prefHeight="320" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="animations.SpinnerController">
<children>
<Label layoutX="0" layoutY="5" minWidth="400" style="-fx-alignment: center;">
<font>
<Font size="15" />
</font>
<text>Hello World</text>
</Label>
<Button fx:id="button" contentDisplay="CENTER" defaultButton="true" layoutX="161.0" layoutY="272.0" onAction="#handleButtonAction" text="Click Me!" />
<Label fx:id="label" layoutX="10" layoutY="30" minHeight="20" minWidth="70">
<text>You haven't spun yet!</text>
</Label>
<Polygon fill="#009918" layoutX="200" layoutY="35" stroke="BLACK" strokeType="OUTSIDE">
<points>
<Double fx:value="-5.0" />
<Double fx:value="4.0" />
<Double fx:value="5.0" />
<Double fx:value="4.0" />
<Double fx:value="0.0" />
<Double fx:value="10.0" />
</points>
</Polygon>
<Group fx:id="spinnerPolygon" layoutX="200.0" layoutY="145.0" rotate="0">
<children>
<Circle fill="DODGERBLUE" layoutX="0" layoutY="5" radius="100.0" stroke="BLACK" strokeType="INSIDE" />
<Line endX="100.0" layoutX="0" layoutY="5" />
<Line endX="100.0" layoutX="-100.0" layoutY="5" />
<Line endX="100.0" layoutX="-50.0" layoutY="-45.0" rotate="90.0" />
<Line endX="50.0" layoutX="0" layoutY="55.0" rotate="90.0" startX="-50.0" />
<!-- This is a comment -->
<Label layoutX="20" layoutY="-20" minHeight="20" minWidth="20" text="0" />
<Label layoutX="20" layoutY="20" minHeight="20" minWidth="20" text="1" />
<Label layoutX="-20" layoutY="20" minHeight="20" minWidth="20" text="2" />
<Label layoutX="-20" layoutY="-20" minHeight="20" minWidth="20" text="3" />
</children>
</Group>
</children>
</AnchorPane>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package animations;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.animation.RotateTransition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.util.Duration;
/**
*
* @author toor
*/
public class SpinnerController implements Initializable {
private double angleTurned = 0.0;
@FXML private Group spinnerPolygon;
@FXML private Label label;
@FXML private Button button;
@FXML
private void handleButtonAction(ActionEvent event) {
RotateTransition spinSpinner;
spinSpinner = new RotateTransition(Duration.millis(1000), spinnerPolygon);
double randomNum = angleToSpin();
angleTurned = (angleTurned + randomNum) % 360.0;
spinSpinner.setByAngle(randomNum);
spinSpinner.play();
int newLoc = landinglocation(angleTurned);
System.out.println("newloc: " + newLoc);
System.out.println("angleTurned " + angleTurned);
spinSpinner.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
label.setText("you are on quartile: " + newLoc);
}
});
}
private double angleToSpin() {
Random r = new Random();
return (r.nextDouble() * 720);
}
private int landinglocation (double d) {
return (int) Math.floor((360.0 - d) / 90);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment