Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
アニメーションで画面遷移する奴
package sample.controllable.animator;
import javafx.animation.FadeTransition;
import javafx.scene.Node;
import javafx.util.Duration;
import sample.controllable.ControllablePane;
import sample.controllable.NodeTransitionAnimator;
public class FadeAnimator implements NodeTransitionAnimator {
private int duration;
public FadeAnimator(int duration) {
this.duration = duration;
}
@Override
public void transition(ControllablePane pane, Node before, Node after) {
if(before != null) {
after.setOpacity(0.0);//一瞬1.0のまま表示されるのを防ぐ
FadeTransition fadeOut = new FadeTransition(new Duration(duration), before);
fadeOut.setFromValue(1.0);
fadeOut.setToValue(0.0);
fadeOut.setOnFinished(e -> {
pane.getChildren().remove(0);
pane.getChildren().add(0, after);
FadeTransition fadeIn = new FadeTransition(new Duration(duration), after);
fadeIn.setFromValue(0.0);
fadeIn.setToValue(1.0);
fadeIn.play();
});
fadeOut.play();
} else {
after.setOpacity(0.0);//一瞬1.0のまま表示されるのを防ぐ
pane.getChildren().add(after);
FadeTransition fadeIn = new FadeTransition(new Duration(duration), after);
fadeIn.setFromValue(0.0);
fadeIn.setToValue(1.0);
fadeIn.play();
}
}
}
package sample.controllable.animator;
import javafx.scene.Node;
import sample.controllable.ControllablePane;
import sample.controllable.NodeTransitionAnimator;
public class NullAnimator implements NodeTransitionAnimator {
@Override
public void transition(ControllablePane pane, Node before, Node after) {
if(before == null) {
pane.getChildren().add(after);
} else {
pane.getChildren().remove(0);
pane.getChildren().add(0, after);
}
}
}
package sample.controllable;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.layout.StackPane;
import sample.controllable.animator.NullAnimator;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Map;
public class ControllablePane extends StackPane {
private Map<String, Node> nodes = new HashMap<>();
private NodeTransitionAnimator animator = new NullAnimator();
public void setAnimator(NodeTransitionAnimator animator) {
this.animator = animator;
}
//FXMLをロード
public void loadNode(String id, InputStream resource) {
try {
FXMLLoader loader = new FXMLLoader();
Parent loadNode = loader.load(resource);
Object obj = loader.getController();
if (obj instanceof ControllablePaneController) {
ControllablePaneController screen = (ControllablePaneController) obj;
screen.setScreenParent(this);
}
nodes.put(id, loadNode);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
//FXMLをアンロード
public void unloadNode(String id) {
nodes.remove(id);
}
//画面遷移
public void setNode(String id) {
if(nodes.get(id) == null) {
throw new NullPointerException(id + " is not registered.");
}
animatePane(getChildren().isEmpty() ? null : getChildren().get(0), nodes.get(id));
}
//アニメーションさせる
private void animatePane(Node before, Node after) {
animator.transition(this, before, after);
}
}
package sample.controllable;
public interface ControllablePaneController {
void setScreenParent(ControllablePane controllablePane);
}
package sample.controllable;
import javafx.scene.Node;
public interface NodeTransitionAnimator {
void transition(ControllablePane pane, Node before, Node after);
}
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import sample.controllable.ControllablePane;
import sample.controllable.animator.FadeAnimator;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private ControllablePane controllablePane;
@Override
public void initialize(URL location, ResourceBundle resources) {
controllablePane.loadNode(Screen1Controller.ID, getClass().getResourceAsStream("screen1.fxml"));
controllablePane.loadNode(Screen2Controller.ID, getClass().getResourceAsStream("screen2.fxml"));
controllablePane.loadNode(Screen3Controller.ID, getClass().getResourceAsStream("screen3.fxml"));
controllablePane.setAnimator(new FadeAnimator(300));
controllablePane.setNode(Screen1Controller.ID);
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("画面遷移");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?import javafx.scene.layout.AnchorPane?>
<?import sample.controllable.ControllablePane?>
<AnchorPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml">
<ControllablePane fx:id="controllablePane" prefHeight="400.0" prefWidth="600.0"/>
</AnchorPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Screen1Controller">
<children>
<Button onAction="#goToScreen2" layoutX="102.0" layoutY="350.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="272.0" text="画面2へ" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
<Button onAction="#goToScreen3" layoutX="272.0" layoutY="278.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="326.0" text="画面3へ" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
<Label layoutX="285.0" layoutY="6.0" maxWidth="1.7976931348623157E308" minWidth="0.0" text="画面1" textAlignment="CENTER" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="6.0">
<font>
<Font size="32.0" />
</font>
</Label>
</children>
</AnchorPane>
package sample;
import sample.controllable.ControllablePaneController;
import sample.controllable.ControllablePane;
public class Screen1Controller implements ControllablePaneController {
private ControllablePane controllablePane;
public static String ID = "Screen1";
@Override
public void setScreenParent(ControllablePane controllablePane) {
this.controllablePane = controllablePane;
}
public void goToScreen2() {
controllablePane.setNode(Screen2Controller.ID);
}
public void goToScreen3() {
controllablePane.setNode(Screen3Controller.ID);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Screen2Controller">
<children>
<Button onAction="#goToScreen1" layoutX="102.0" layoutY="350.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="272.0" text="画面1へ" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
<Button onAction="#goToScreen3" layoutX="272.0" layoutY="278.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="326.0" text="画面3へ" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
<Label layoutX="285.0" layoutY="6.0" maxWidth="1.7976931348623157E308" minWidth="0.0" text="画面2" textAlignment="CENTER" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="6.0">
<font>
<Font size="32.0" />
</font>
</Label>
</children>
</AnchorPane>
package sample;
import sample.controllable.ControllablePaneController;
import sample.controllable.ControllablePane;
public class Screen2Controller implements ControllablePaneController {
private ControllablePane controllablePane;
public static String ID = "Screen2";
@Override
public void setScreenParent(ControllablePane controllablePane) {
this.controllablePane = controllablePane;
}
public void goToScreen1() {
controllablePane.setNode(Screen1Controller.ID);
}
public void goToScreen3() {
controllablePane.setNode(Screen3Controller.ID);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Screen3Controller">
<children>
<Button onAction="#goToScreen1" layoutX="102.0" layoutY="350.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="272.0" text="画面1へ" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
<Button onAction="#goToScreen2" layoutX="272.0" layoutY="278.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="326.0" text="画面2へ" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
<Label layoutX="285.0" layoutY="6.0" maxWidth="1.7976931348623157E308" minWidth="0.0" text="画面3" textAlignment="CENTER" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="6.0">
<font>
<Font size="32.0" />
</font>
</Label>
</children>
</AnchorPane>
package sample;
import sample.controllable.ControllablePaneController;
import sample.controllable.ControllablePane;
public class Screen3Controller implements ControllablePaneController {
private ControllablePane controllablePane;
public static String ID = "Screen3";
@Override
public void setScreenParent(ControllablePane controllablePane) {
this.controllablePane = controllablePane;
}
public void goToScreen1() {
controllablePane.setNode(Screen1Controller.ID);
}
public void goToScreen2() {
controllablePane.setNode(Screen2Controller.ID);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment