Java語言程式設計 第十五章(15.29、15.30、15.31、15.32)
阿新 • • 發佈:2018-12-30
程式小白,希望和大家多交流,共同學習
15.29
//進行了擴充
//CarPane,使小車可以按照指定的x,y繪製,並且可以加減速,開始暫停,還有定時器
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.collections.ObservableList;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.util.Duration;
import javafx.scene.layout.Pane;
public class CarPane extends Pane
{
private double x;
private double y;
private double w;
private double time;
private PathTransition aRoof;
private PathTransition aBody;
private PathTransition aWheel1;
private PathTransition aWheel2;
private Polygon roof;
private Rectangle body;
private Circle wheel1;
private Circle wheel2;
private Duration duration;
public CarPane()
{
this(0, 100, 400, 5000);
}
public CarPane(double w)
{
this(0, 100, w, 5000);
}
public CarPane(double x, double y)
{
this(x, y, 400, 5000);
}
public CarPane(double x, double y, double w, double time)
{
this.x = x;
this.y = y;
this.time = time;
this.w = w;
aRoof = new PathTransition();
aBody = new PathTransition();
aWheel1 = new PathTransition();
aWheel2 = new PathTransition();
duration = new Duration(time);
roof = new Polygon();
body = new Rectangle();
wheel1 = new Circle();
wheel2 = new Circle();
paintCar();
}
//加速
public void increaseRate()
{
System.out.println("increase");
aRoof.setRate(aRoof.getRate() + 0.1);
aBody.setRate(aBody.getRate() + 0.1);
aWheel1.setRate(aWheel1.getRate() + 0.1);
aWheel2.setRate(aWheel2.getRate() + 0.1);
}
//減速
public void decreaseRate()
{
System.out.println("decrease");
aRoof.setRate(aRoof.getRate() - 0.1 < 0.0 ? 0.0 : aRoof.getRate() - 0.1);
aBody.setRate(aBody.getRate() - 0.1 < 0.0 ? 0.0 : aBody.getRate() - 0.1);
aWheel1.setRate(aWheel1.getRate() - 0.1 < 0.0 ? 0.0 : aWheel1.getRate() - 0.1);
aWheel2.setRate(aWheel2.getRate() - 0.1 < 0.0 ? 0.0 : aWheel2.getRate() - 0.1);
}
//動畫開始
public void play()
{
aRoof.play();
aBody.play();
aWheel1.play();
aWheel2.play();
}
//動畫暫停
public void pause()
{
aRoof.pause();
aBody.pause();
aWheel1.pause();
aWheel2.pause();
}
//畫小車
public void paintCar()
{
roof.setFill(Color.RED);
roof.setStroke(Color.BLACK);
ObservableList<Double> list = roof.getPoints();
list.add(x + 10);
list.add(y - 20);
list.add(x + 20);
list.add(y - 30);
list.add(x + 30);
list.add(y - 30);
list.add(x + 40);
list.add(y - 20);
Line lR = new Line(x + 25, y - 25, w - 25, y - 25);
body.setX(x);
body.setY(y - 20);
body.setWidth(50);
body.setHeight(10);
body.setFill(Color.RED);
body.setStroke(Color.BLACK);
Line lB = new Line(x + 25, y - 15, w - 25, y - 15);
wheel1.setCenterX(x + 15);
wheel1.setCenterY(y - 5);
wheel1.setRadius(5);
wheel1.setFill(Color.BLACK);
Line lW1 = new Line(x + 15, y - 5, w - 35, y - 5);
wheel2.setCenterX(x + 35);
wheel2.setCenterY(y - 5);
wheel2.setRadius(5);
wheel2.setFill(Color.BLACK);
Line lW2 = new Line(x + 35, y - 5, w - 15, y - 5);
super.getChildren().addAll(roof, body, wheel1, wheel2);
aRoof.setDuration(duration);
aRoof.setPath(lR);
aRoof.setNode(roof);
aRoof.setCycleCount(Timeline.INDEFINITE);
aRoof.play();
aBody.setDuration(duration);
aBody.setPath(lB);
aBody.setNode(body);
aBody.setCycleCount(Timeline.INDEFINITE);
aBody.play();
aWheel1.setDuration(duration);
aWheel1.setPath(lW1);
aWheel1.setNode(wheel1);
aWheel1.setCycleCount(Timeline.INDEFINITE);
aWheel1.play();
aWheel2.setDuration(duration);
aWheel2.setPath(lW2);
aWheel2.setNode(wheel2);
aWheel2.setCycleCount(Timeline.INDEFINITE);
aWheel2.play();
}
}
//使用CarPane產生兩個小車,使用W,S加減上面的車的速度
//UP DOWN控制下面車的速度
//滑鼠按鈕按下和鬆開,開始和暫停
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.input.KeyCode;
public class DriveCars extends Application
{
@Override
public void start(Stage primaryStage)
{
double w = 1000;
VBox pane = new VBox(5);
CarPane car1 = new CarPane(w);
pane.getChildren().add(car1);
CarPane car2 = new CarPane(w);
pane.getChildren().add(car2);
car1.setOnMousePressed(e -> {
car1.pause();
car2.pause();
});
pane.setOnMouseReleased(e -> {
car1.play();
car2.play();
});
pane.setOnKeyPressed(e -> {
switch (e.getCode())
{
case UP:
car2.increaseRate(); break;
case DOWN:
car2.decreaseRate(); break;
case W:
car1.increaseRate(); break;
case S:
car1.decreaseRate(); break;
}
});
Scene scene = new Scene(pane, w, 250);
primaryStage.setTitle("Drive Cars");
primaryStage.setScene(scene);
primaryStage.show();
pane.requestFocus();
}
}
15.30
//使用一個按鈕改變一個每兩秒鐘播放的幻燈片的狀態
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.animation.Timeline;
import javafx.util.Duration;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.animation.KeyFrame;
import javafx.animation.Animation;
public class ShowPictures extends Application
{
private int num = 0;
@Override
public void start(Stage primaryStage)
{
//初始化
VBox pane = new VBox(10);
pane.setAlignment(Pos.CENTER);
ImageView picture = new ImageView();
picture.setImage(getImage());
pane.getChildren().add(picture);
//動畫
EventHandler<ActionEvent> eventHandler = e -> {
picture.setImage(getImage());
};
Timeline animation = new Timeline(new KeyFrame(Duration.millis(2000), eventHandler));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
//控制按鈕
Button change = new Button("Change");
pane.getChildren().add(change);
change.setOnAction(e -> {
if (animation.getStatus() == Animation.Status.RUNNING)
{
animation.pause();
}
else if (animation.getStatus() == Animation.Status.PAUSED)
{
animation.play();
}
});
Scene scene = new Scene(pane, 100, 150);
primaryStage.setTitle("ShowPicture");
primaryStage.setScene(scene);
primaryStage.show();
}
//返回影象
public Image getImage()
{
num = (num + 1) % 55 == 0 ? 1 : (num + 1) % 55;
return new Image("image/card/" + num + ".png");
}
}
15.31
//使用S、P控制鐘擺的停止和開始,使用UP、DOWN控制小球速度
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
public class MoveSwing extends Application
{
@Override
public void start(Stage primaryStage)
{
SwingPane pane = new SwingPane();
pane.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.UP)
{
pane.increaseSpeed();
}
else if (e.getCode() == KeyCode.DOWN)
{
pane.decreaseSpeed();
}
else if (e.getCode() == KeyCode.S)
{
pane.stop();
}
else if (e.getCode() == KeyCode.P)
{
pane.play();
}
});
primaryStage.setResizable(false);
Scene scene = new Scene(pane, pane.getW() + 100, pane.getH());
primaryStage.setTitle("MoveSwing");
primaryStage.setScene(scene);
primaryStage.show();
pane.requestFocus();
}
}
15.32
//讓時鐘動起來
//時鐘大小可以隨桌面改變大小
import javafx.animation.Animation;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.util.Duration;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import javafx.geometry.Pos;
import javafx.beans.Observable;
import javafx.beans.InvalidationListener;
public class ClockAnimation extends Application
{
@Override
public void start(Stage primaryStage)
{
MoreClockPane clock = new MoreClockPane();
BorderPane pane = new BorderPane();
pane.setCenter(clock);
EventHandler<ActionEvent> eventHandler = new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent e)
{
clock.setCurrentTime();
//從內部類引用的變數必須是最終變數或實際上的最終變數
}};
Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), eventHandler));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
//新增兩個控制按鈕
Button stop = new Button("Stop");
Button start = new Button("Start");
HBox hbox = new HBox(5);
hbox.getChildren().addAll(stop, start);
hbox.setAlignment(Pos.CENTER);
pane.setBottom(hbox);
stop.setOnAction(e -> {
animation.stop();
});
start.setOnAction(e -> {
animation.play();
});
pane.widthProperty().addListener(new InvalidationListener()
{
@Override
public void invalidated(Observable o)
{
clock.setW(pane.getWidth());
}
});
pane.heightProperty().addListener(new InvalidationListener()
{
@Override
public void invalidated(Observable o)
{
clock.setH(pane.getHeight());
}
});
Scene scene = new Scene(pane);
primaryStage.setTitle("ClockAnimation");
primaryStage.setScene(scene);
primaryStage.show();
}
}