15.1(1) 只随机刷新一次扑克牌
package hahaha;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
public class demo14_3 extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Pane pane = new HBox(10);
// pane.setPadding(new Insets(5,5,5,5));
int[]a=new int [60];
for(int i=0;i<3;i++){
int x;
do {
x=(int)(Math.random()*54+1);
}
while(a[x]!=0);
a[x]++;
Image image = new Image("image/card/"+x+".png");
pane.getChildren().add(new ImageView(image));
}
Scene scene=new Scene(pane);
primaryStage.setTitle("card");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
15.1 (2)添加了下方中间的按钮()
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
//import javafx.scene.layout.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Pane pane = new HBox(10);
pane.setPadding(new Insets(5,5,5,5));
int[]a=new int [55];
for(int i=1;i<=3;i++) {
int x;
do {
x = (int) (Math.random() * 54 + 1);
}
while (a[x] != 0);
a[x]++;
Image image = new Image("image/card/" + x + ".png");
pane.getChildren().add(new ImageView(image));
}
Pane pane2 = new VBox(10);
pane2.getChildren().add(pane);
pane2.getChildren().add(new StackPane(new Button("Refresh")));
Scene scene=new Scene(pane2);
primaryStage.setTitle("card");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
15.1(3)可以随机生成,点按钮后并随机刷新
把抽三张牌写进了函数里
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
//import javafx.scene.layout.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Pane pane = new HBox(10);
pane.setPadding(new Insets(5,5,5,5));
addcard(pane);
Pane pane2 = new VBox(10);
pane2.getChildren().add(pane);
Button btRefresh = new Button("Refresh");
pane2.getChildren().add(new StackPane(btRefresh));
btRefresh.setOnAction(e -> {
pane.getChildren().clear();
addcard(pane);
});
Scene scene=new Scene(pane2);
primaryStage.setTitle("card");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
public void addcard(Pane pane){
int[]a=new int [55];
for(int i=1;i<=3;i++) {
int x;
do {
x = (int) (Math.random() * 54 + 1);
}
while (a[x] != 0);
a[x]++;
Image image = new Image("image/card/" + x + ".png");
pane.getChildren().add(new ImageView(image));
}
}
}
15.2
package hahaha;
import javafx.application.Application;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.control.Button;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
public class demo2 extends Application
{
private double angle = 0;
@Override
public void start(Stage primaryStage)
{
Pane pane = new VBox(15);
pane.setPadding(new Insets(30, 80, 30, 80));
Rectangle re = new Rectangle(25, 50, 25, 50);
re.setFill(Color.WHITE);
re.setStroke(Color.BLACK);
Button btRotate = new Button("Rotate");
Pane t1 = new StackPane();
Pane t2 = new StackPane();
t1.getChildren().add(re);
t2.getChildren().add(btRotate);
pane.getChildren().add(t1);
pane.getChildren().add(t2);
// pane.setAlignment(Pos.CENTER);
btRotate.setOnAction(e -> {
jiaodu(re);
});
primaryStage.setTitle("RotateARectangle");
primaryStage.setScene(new Scene(pane));
primaryStage.show();
}
public void jiaodu(Rectangle r)
{
angle += 15;
r.setRotate(angle);
}
}
15.3
package hahaha;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.geometry.Pos;
public class MoveBall extends Application
{
@Override
public void start(Stage primaryStage)
{
Pane pane = new Pane();
Circle circle = new Circle(100, 100, 30);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
Button up = new Button("Up");
Button right = new Button("Right");
Button down = new Button("Down");
Button left = new Button("Left");
HBox hbox = new HBox(5);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(left);
hbox.getChildren().add(right);
hbox.getChildren().add(up);
hbox.getChildren().add(down);
pane.getChildren().addAll(hbox,circle);
up.setOnAction(e -> {
if (circle.getCenterY() - 10 - 5 < 0)
{
circle.setCenterY(circle.getCenterY());
}
else
circle.setCenterY(circle.getCenterY() - 5);
circle.setCenterX(circle.getCenterX());
});
right.setOnAction(e -> {
if (circle.getCenterX() + circle.getRadius() + 5 > pane.getWidth())
{
circle.setCenterX(circle.getCenterX());
}
else
circle.setCenterX(circle.getCenterX() + 5);
circle.setCenterY(circle.getCenterY());
});
down.setOnAction(e -> {
if (circle.getCenterY() + circle.getRadius() + 5 > pane.getHeight())
{
circle.setCenterY(circle.getCenterY());
}
else
circle.setCenterY(circle.getCenterY() + 5);
circle.setCenterX(circle.getCenterX());
});
left.setOnAction(e -> {
if (circle.getCenterX() - circle.getRadius() - 5 < 0)
{
circle.setCenterX(circle.getCenterX());
}
else
circle.setCenterX(circle.getCenterX() - 5);
circle.setCenterY(circle.getCenterY());
});
Scene scene = new Scene(pane, 500, 300);
primaryStage.setTitle("Move");
primaryStage.setScene(scene);
primaryStage.show();
}
}