ITI0011RUS:timelinedemo

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

Обратно на страницу предмета.

TimelineDemo.java

<source lang="java"> import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Shape; import javafx.stage.Stage; import javafx.util.Duration;

/**

* 
*/

/**

* @author aleksandr.lenin
*
*/

public class TimelineDemo extends Application {

Group shapes = null;

/* (non-Javadoc) * @see javafx.application.Application#start(javafx.stage.Stage) */ @Override public void start(Stage primaryStage) throws Exception { shapes = new Group(); Scene scene = new Scene(shapes,800,600); primaryStage.setTitle("Timeline Demo"); primaryStage.setScene(scene); primaryStage.show();

/* Animation Timeline t = new Timeline(); KeyValue kv1 = new KeyValue(c.translateXProperty(),600); KeyValue kv2 = new KeyValue(c.translateYProperty(),300); KeyFrame kf1 = new KeyFrame(Duration.seconds(1),kv1,kv2); KeyFrame kf2 = new KeyFrame(Duration.seconds(5), new KeyValue(c.scaleXProperty(),2), new KeyValue(c.scaleYProperty(),4), new KeyValue(c.fillProperty(), Paint.valueOf("green")) );

t.getKeyFrames().addAll(kf1,kf2); t.setCycleCount(3); t.play();

  • /

Timeline tl1 = new Timeline(); tl1.setCycleCount(Timeline.INDEFINITE); KeyFrame kf = new KeyFrame(Duration.millis(1000), e -> createRandomCircle()); tl1.getKeyFrames().add(kf); tl1.play();

Timeline tl2 = new Timeline(); tl2.setCycleCount(Timeline.INDEFINITE); KeyFrame kf2 = new KeyFrame(Duration.millis(650), e -> createRandomRectangle()); tl2.getKeyFrames().add(kf2); tl2.play(); }

private double randRange(double from, double to) { if(from > to) return 0; return Math.random() * (to-from+1) + from; }

private boolean intersects(Shape a, Group shapes) { for(Node n : shapes.getChildren()) { Shape cur = (Shape) n; Shape intersection = Shape.intersect(cur, a); if(intersection.getBoundsInLocal().getWidth() > 0) return true; } return false; }

private void createRandomCircle() { Scene scene = shapes.getScene(); MyCircle c = null;

do { double radius = randRange(25,50); double x = randRange(radius, scene.getWidth() - radius); double y = randRange(radius, scene.getHeight() - radius); Color color = Color.rgb((int)randRange(0,255), (int)randRange(0,255), (int)randRange(0,255)); c = new MyCircle(x,y,radius,color); } while(intersects(c,shapes));

shapes.getChildren().add(c); }

private void createRandomRectangle() { Scene scene = shapes.getScene(); MyRectangle r = null;

do { double w = randRange(25,50); double h = randRange(25,50); double x = randRange(0, scene.getWidth() - w); double y = randRange(0, scene.getHeight() - h); Color color = Color.rgb((int)randRange(0,255), (int)randRange(0,255), (int)randRange(0,255)); r = new MyRectangle(x,y,w,h,color); } while(intersects(r,shapes));

shapes.getChildren().add(r); }

/** * @param args */ public static void main(String[] args) { launch(args); }

}

</source>

MyCircle.java

<source lang="java"> import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.shape.Circle;


public class MyCircle extends Circle {

public MyCircle(double x, double y, double radius) { super(x,y,radius); setOnMouseClicked(new EventHandler<MouseEvent>() {

@Override public void handle(MouseEvent arg0) { removeSelf(); }

}); }

public MyCircle(double x, double y, double radius, Color color) { super(x,y,radius,color); setOnMouseClicked(new EventHandler<MouseEvent>() {

@Override public void handle(MouseEvent arg0) { removeSelf(); }

}); }

private void removeSelf() { Group shapes = (Group) getParent(); if(shapes != null) { shapes.getChildren().remove(this); } } }

</source>

MyRectangle.java

<source lang="java"> import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle;


public class MyRectangle extends Rectangle {

public MyRectangle(double x, double y, double width, double height) { super(x,y,width,height); addHandler(); }

public MyRectangle(double x, double y, double width, double height, Color color) { super(x,y,width,height); setFill(color); addHandler(); }

private void removeSelf() { Group shapes = (Group) getParent(); if(shapes != null) { shapes.getChildren().remove(this); } }

private void addHandler() { setOnMouseClicked(new EventHandler<MouseEvent>() {

@Override public void handle(MouseEvent arg0) { removeSelf(); }

}); } }

</source>