1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| package library;
import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.collections.ObservableList; import javafx.geometry.Bounds; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage;
public class MainLoop extends Application {
private final int SIZE_W = 800; private final int SIZE_H = 600; private final long ONE_SECOND = 1000000000; private long currentTime = 0; private long lastTime = 0; private int fps = 0; private double delta = 0; private Text tFPS; private AnimationTimer mainLoop; private Polygon star; private int dirx = 1; private int diry = 1; @Override public void start(Stage stage) throws Exception { Pane root = new Pane(); root.setPrefSize(SIZE_W, SIZE_H); root.setBackground(new Background(new BackgroundFill(Color.BLACK, null, null))); initNodes(root.getChildren()); initMainLoop(root.getChildren()); Scene scene = new Scene(root); stage.setScene(scene); stage.setResizable(false); stage.setOnCloseRequest(event -> { mainLoop.stop(); System.exit(0); }); stage.show(); mainLoop.start(); } public void initNodes(ObservableList<Node> rootPane){ tFPS = new Text("FPS : "); tFPS.setTranslateX(SIZE_W-180); tFPS.setTranslateY(60); tFPS.setFill(Color.WHITE); tFPS.setFont(new Font(40)); rootPane.add(tFPS); initAnimation(rootPane); } public void initAnimation(ObservableList<Node> rootPane){ star = new Polygon(); Double[] starCoord = new Double[]{ 35.0, 120.5, 37.9, 129.1, 46.9, 129.1, 39.7, 134.5, 42.3, 143.1, 35.0 , 139.0, 27.7, 143.1, 30.3, 134.5, 23.1, 129.1, 32.1,129.1}; star.getPoints().addAll(starCoord); star.setTranslateX(100); star.setTranslateY(100); star.setFill(Color.RED); rootPane.add(star);
} public void initMainLoop(ObservableList<Node> rootPane){ lastTime = System.nanoTime(); mainLoop = new AnimationTimer() { @Override public void handle(long now) { currentTime = now; fps++; delta += currentTime-lastTime; updateAnimation(rootPane); if(delta > ONE_SECOND){ tFPS.setText("FPS : "+fps); delta -= ONE_SECOND; fps = 0; } lastTime = currentTime; } }; } public void updateAnimation(ObservableList<Node> rootPane){ Bounds starbounds = star.getBoundsInParent(); double posx = star.getTranslateX(); double posy = star.getTranslateY();
if((starbounds.getMaxX() >= SIZE_W) || (starbounds.getMinX() <= 0.0)){ dirx = -dirx; star.setScaleX(1); star.setScaleY(1); } if((starbounds.getMaxY() >= SIZE_H) || (starbounds.getMinY() <= 0.0)){ diry = -diry; star.setScaleX(1); star.setScaleY(1); } star.setTranslateX(posx+(3*dirx)); star.setTranslateY(posy+(3*diry)); star.setScaleX(star.getScaleX()+0.1); star.setScaleY(star.getScaleY()+0.1);
} public static void main(String[] args){ launch(args); }
} |