Table des matières
Correction TP3.5
Rotation, transformation et TextFlow
La classe main. La seule spécificité ici est que je fait une rotation par rapport au centre du texte (grâce aux bornes du texte).
package com.mco; import javafx.application.Application; import javafx.geometry.Bounds; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.text.*; import javafx.scene.transform.Rotate; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { System.out.println("Poclices disponibles :"); for (String fontName: Font.getFamilies()) { System.out.println(fontName); } launch(args); } private static void textFlow(Group root) { String fontFamily = "Courier New"; TextFlow textFlow = new TextFlow(); //layout donne l'élément en haut à gauche. textFlow.setLayoutX(0); textFlow.setLayoutY(0); Text text1 = new Text("Salut "); text1.setFont(Font.font(fontFamily, 25)); text1.setFill(Color.RED); Text text2 = new Text("les"); text2.setFill(Color.ORANGE); text2.setFont(Font.font(fontFamily, FontWeight.BOLD, 50)); Text text3 = new Text(" gens"); text3.setFill(Color.GREEN); text3.setFont(Font.font(fontFamily, FontPosture.ITALIC, 12)); textFlow.getChildren().addAll(text1, text2, text3); Group group = new Group(); //on crée un groupe avec le TextFlow dedans group.getChildren().add(textFlow); root.getChildren().add(group); // on l'ajoute à root } public void start(Stage theStage) { theStage.setTitle("Hello, World!"); //mise en place du Stage : création du scene graph et du noeud racine. Group root = new Group(); //ce ne sera pas une feuille, donc de classe Group. Scene theScene = new Scene(root); //création du scene graph theStage.setScene(theScene); //on le place dans le stage //Création d'un text Text text = new Text(); Font comic = new Font("Comic sans MS", 50); text.setFont(comic); text.setFill(Color.color(.5,0,1.0,1.0)); text.setText("Hello World aussi !"); text.setX(0); text.setY(200); //on l'ajoute à notre scene graph via son père, ici le noeud root root.getChildren().add(text); // on effectue une rotation sur le texte. Bounds boundingBox = text.getLayoutBounds(); System.out.println(text.getLayoutBounds()); double middleX = (boundingBox.getMaxX() + boundingBox.getMinX()) / 2; double middleY = (boundingBox.getMaxY() + boundingBox.getMinY()) / 2; text.getTransforms().add(new Rotate(-45, middleX, middleY)); //un TextFlow textFlow(root); theStage.show(); } }
Double Pendule
Une classe Main
, Une classe Loop
et une classe DoublePendulum
permettant de gérer les mouvements du pendule.
Main.java
Pas de changement par rapport au TP3.
package com.mco; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { launch(args); } public void start(Stage theStage) { theStage.setTitle("Watch Hour"); Group root = new Group(); Scene theScene = new Scene(root); theStage.setScene(theScene); Canvas canvas = new Canvas(512, 512); root.getChildren().add(canvas); Loop mainLoop = new Loop(canvas); mainLoop.start(); theStage.show(); } }
Loop.java
package com.mco; import javafx.animation.AnimationTimer; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; public class Loop extends AnimationTimer { private Canvas canvas; private DoublePendulum doublePendulum; private long currentTime; final Color BACKGROUND_COLOR = Color.ALICEBLUE; final Color PENDULUM_COLOR = Color.BISQUE; final int WATCH_HAND_LINE_WIDTH = 10; public Loop(Canvas canvas) { currentTime = System.nanoTime(); doublePendulum = new DoublePendulum(100, Math.PI / 2, 1, 200, Math.PI - .0001, 5); this.canvas = canvas; } @Override public void handle(long now) { handleTime(now); drawStuff(); } private void handleTime(long now) { doublePendulum.update(now - currentTime); currentTime = now; } private void drawStuff() { clearCanvas(); drawWatchHand(); } private void clearCanvas() { GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFill(BACKGROUND_COLOR); gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); } private void drawWatchHand() { GraphicsContext gc = canvas.getGraphicsContext2D(); double middleX = canvas.getWidth() / 2; double middleY = canvas.getHeight() / 2; gc.beginPath(); gc.setLineWidth(WATCH_HAND_LINE_WIDTH); gc.setStroke(PENDULUM_COLOR); gc.moveTo(middleX, middleY); gc.lineTo(middleX + doublePendulum.x1(), middleY + doublePendulum.y1()); gc.lineTo(middleX + doublePendulum.x2(), middleY + doublePendulum.y2()); gc.stroke(); } }
DoublePendulum.java
package com.mco; public class DoublePendulum { private final static double G = 9.2; private double d2Theta1 =0, d2Theta2 = 0; private double dTheta1 = 0, dTheta2 = 0; private double theta1, theta2; private double l1, l2; private double mu; public DoublePendulum(double l1, double theta1, double m1, double l2, double theta2, double m2) { this.theta1 = theta1; this.theta2 = theta2; this.l1 = l1; this.l2 = l2; this.mu = 1 + m1 / m2; } public void update(long deltaInNanoSeconds) { double time = deltaInNanoSeconds * 1E-9; d2Theta1 = (G * (Math.sin(theta2) * Math.cos(theta1 - theta2) - mu * Math.sin(theta1)) - (l2 * dTheta2 * dTheta2 + l1 * dTheta1 * dTheta1 * Math.cos(theta1 - theta2)) * Math.sin(theta1 - theta2)) / (l1 * (mu - Math.cos(theta1 - theta2) * Math.cos(theta1 - theta2))); d2Theta2 = (mu * G * (Math.sin(theta1) * Math.cos(theta1 - theta2) - Math.sin(theta2)) + (mu * l1 * dTheta1 * dTheta1 + l2 * dTheta2 * dTheta2 * Math.cos(theta1 - theta2)) * Math.sin(theta1 - theta2)) / (l2 * (mu - Math.cos(theta1 - theta2) * Math.cos(theta1 - theta2))); dTheta1 += d2Theta1 * time; dTheta2 += d2Theta2 * time; theta1 += dTheta1 * time; theta2 += dTheta2 * time; } public double x1() { return l1 * Math.sin(theta1); } public double y1() { return l1 * Math.cos(theta1); } public double x2() { return x1() + l2 * Math.sin(theta2); } public double y2() { return y1() + l2 * Math.cos(theta2); } }