Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| restricted:mco-2:correction:tp3 [2016/03/02 10:29] – fbrucker | restricted:mco-2:correction:tp3 [2016/03/05 17:46] (Version actuelle) – fbrucker | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== Correction TP3 ====== | ||
| + | |||
| + | ===== Le Texte ===== | ||
| + | |||
| + | <code java> | ||
| + | package com.mco; | ||
| + | |||
| + | import javafx.application.Application; | ||
| + | import javafx.scene.Group; | ||
| + | import javafx.scene.Scene; | ||
| + | import javafx.scene.paint.Color; | ||
| + | import javafx.scene.text.*; | ||
| + | import javafx.stage.Stage; | ||
| + | |||
| + | public class Main extends Application { | ||
| + | public static void main(String[] args) { | ||
| + | System.out.println(" | ||
| + | for (String fontName: Font.getFamilies()) { | ||
| + | System.out.println(fontName); | ||
| + | } | ||
| + | |||
| + | launch(args); | ||
| + | } | ||
| + | |||
| + | |||
| + | public void start(Stage theStage) { | ||
| + | theStage.setTitle(" | ||
| + | |||
| + | //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); | ||
| + | theStage.setScene(theScene); | ||
| + | |||
| + | //Création d'un text | ||
| + | Text text = new Text(); | ||
| + | | ||
| + | Font comic = new Font(" | ||
| + | text.setFont(comic); | ||
| + | text.setFill(Color.color(.5, | ||
| + | |||
| + | text.setText(" | ||
| + | text.setX(0); | ||
| + | text.setY(200); | ||
| + | |||
| + | //on l' | ||
| + | root.getChildren().add(text); | ||
| + | | ||
| + | theStage.show(); | ||
| + | |||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Des ronds ===== | ||
| + | <code java> | ||
| + | gc.setFill(Color.rgb(200, | ||
| + | gc.fillOval(100, | ||
| + | |||
| + | gc.setFill(Color.rgb(0, | ||
| + | gc.fillOval(100, | ||
| + | |||
| + | gc.setFill(Color.rgb(0, | ||
| + | gc.fillOval(200, | ||
| + | |||
| + | gc.setFill(Color.rgb(200, | ||
| + | gc.fillOval(200, | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Afficher le Delta ===== | ||
| + | |||
| + | == Main.java == | ||
| + | |||
| + | <code java> | ||
| + | package com.mco; | ||
| + | |||
| + | import javafx.application.Application; | ||
| + | import javafx.scene.Group; | ||
| + | import javafx.scene.Scene; | ||
| + | import javafx.scene.text.Text; | ||
| + | import javafx.stage.Stage; | ||
| + | |||
| + | public class Main extends Application { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | launch(args); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void start(Stage primaryStage) { | ||
| + | primaryStage.setTitle(" | ||
| + | |||
| + | Group root = new Group(); | ||
| + | Scene theScene = new Scene(root, 100, 200); //taille initiale. | ||
| + | primaryStage.setScene(theScene); | ||
| + | |||
| + | Text text = new Text(); | ||
| + | root.getChildren().add(text); | ||
| + | text.setX(0); | ||
| + | text.setY(100); | ||
| + | |||
| + | Loop loop = new Loop(text); | ||
| + | |||
| + | loop.start(); | ||
| + | primaryStage.show(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Loop.java == | ||
| + | |||
| + | <code java> | ||
| + | package com.mco; | ||
| + | |||
| + | import javafx.animation.AnimationTimer; | ||
| + | import javafx.scene.text.Text; | ||
| + | |||
| + | public class Loop extends AnimationTimer { | ||
| + | |||
| + | private Text text; | ||
| + | |||
| + | private long previousTime; | ||
| + | |||
| + | public Loop(Text text) { | ||
| + | this.text = text; | ||
| + | this.previousTime = System.nanoTime(); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void handle(long now) { | ||
| + | long delta = now - previousTime; | ||
| + | previousTime = now; | ||
| + | |||
| + | text.setText(String.valueOf(delta)); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | ===== La petite aiguille ===== | ||
| + | |||
| + | <code 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; | ||
| + | |||
| + | final double ROTATION | ||
| + | final Color BACKGROUND_COLOR = Color.ALICEBLUE; | ||
| + | |||
| + | final Color WATCH_HAND_COLOR | ||
| + | final int WATCH_HAND_LINE_WIDTH = 10; | ||
| + | final double WATCH_HAND_LENGTH = 200; | ||
| + | |||
| + | private double angleBig; | ||
| + | private double angleSmall; | ||
| + | private long currentTime; | ||
| + | |||
| + | public Loop(Canvas canvas) { | ||
| + | currentTime = System.nanoTime(); | ||
| + | this.canvas = canvas; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void handle(long now) { | ||
| + | handleTime(now); | ||
| + | drawStuff(); | ||
| + | } | ||
| + | |||
| + | private void handleTime(long now) { | ||
| + | changeAngle(now - currentTime); | ||
| + | currentTime = now; | ||
| + | } | ||
| + | |||
| + | private void changeAngle(long delta) { | ||
| + | angleBig += ROTATION * delta; | ||
| + | angleBig %= 2 * Math.PI; | ||
| + | |||
| + | angleSmall += (ROTATION / 12) * delta; | ||
| + | angleSmall %= 2 * Math.PI; | ||
| + | } | ||
| + | |||
| + | private void drawStuff() { | ||
| + | clearCanvas(); | ||
| + | drawWatchHand(); | ||
| + | } | ||
| + | |||
| + | private void clearCanvas() { | ||
| + | GraphicsContext gc = canvas.getGraphicsContext2D(); | ||
| + | |||
| + | gc.setFill(BACKGROUND_COLOR); | ||
| + | gc.fillRect(0, | ||
| + | } | ||
| + | |||
| + | 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(WATCH_HAND_COLOR); | ||
| + | |||
| + | gc.moveTo(middleX, | ||
| + | gc.lineTo(middleX + WATCH_HAND_LENGTH * Math.cos(angleBig), | ||
| + | middleY + WATCH_HAND_LENGTH * Math.sin(angleBig)); | ||
| + | |||
| + | gc.moveTo(middleX, | ||
| + | gc.lineTo(middleX + (WATCH_HAND_LENGTH / 4) * Math.cos(angleSmall), | ||
| + | middleY + (WATCH_HAND_LENGTH / 4) * Math.sin(angleSmall)); | ||
| + | |||
| + | gc.stroke(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||