JAVA语言程序设计与数据结构(基础篇)第十四章练习题14.3-14.6

 14.3随机生成三张扑克牌

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.util.Random;

public class Exercise14_03 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new HBox(10);
        pane.setPadding(new Insets(5,5,5,5));
//生成随机三张牌
        int images[] = new int[52];
        for(int i = 0;i < 52;i++){
            images[i] = i+1;
        }
        Random random = new Random();
        int index1 = random.nextInt(images.length);
        int index2 = random.nextInt(images.length);
        int index3 = random.nextInt(images.length);

        Image image1 = new Image("image/card/"+images[index1]+".png");
        Image image2 = new Image("image/card/"+images[index2]+".png");
        Image image3 = new Image("image/card/"+images[index3]+".png");

        pane.getChildren().add(new ImageView(image1));
        pane.getChildren().add(new ImageView(image2));
        pane.getChildren().add(new ImageView(image3));

        Scene scene = new Scene(pane);
        primaryStage.setTitle("Exercise14_03");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

 14.4颜色和字体·

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.stage.Stage;

public class Exercise14_04 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new HBox(10);
        pane.setPadding(new Insets(15,15,15,15));
        for(int i = 0;i <5;i++){
            Text text = new Text("JAVA");
            HBox.setMargin(text,new Insets(15,0,15,0));
            text.setFont(Font.font("Times New Roman ", FontWeight.BLACK, FontPosture.ITALIC, 22));
            text.setRotate(90);
            pane.getChildren().add(text);
            text.setFill(Color.color(Math.random(), Math.random(), Math.random()));
        }

        Scene scene = new Scene(pane);
        primaryStage.setTitle("Exercise14_04");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

14.5围绕成圆的字符

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.stage.Stage;

public class Exercise14_05 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();
        String str = "WELCOME TO JAVA";
        int length = str.length();
        final double centerX = 100, centerY = 100, radius = 50;
        double divide = 2 * Math.PI / length;
        /*PI即为圆周率π,是弧度制的π,即为180°
        Math.PI/5表示角度平分为36°
        顶点到与中心线的夹角α=(2π)/n=Math.PI/n*2
        相间的两个顶点到与中心连线之间的夹角β=2α=4π/n=Math.PI/n*4
         */


        for (int i = 0; i < length; i++)
        {
            double newX = centerX + radius * Math.cos(i * divide);
            double newY = centerY + radius * Math.sin(i * divide);
            Text t = new Text(newX, newY, str.charAt(i) + "");
            t.setFill(Color.BLACK);
            //Text中的setFont(Font)
            t.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 22));
            //旋转字体
            int r = (int)((Math.PI / 2 + i * divide) / (2 * Math.PI) * 360);
            t.setRotate(r);
            pane.getChildren().add(t);
        }

        Scene scene = new Scene(pane, 200, 200);
        primaryStage.setTitle("ShowString");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

14.6生成象棋棋盘


public class Exercise14_06 extends Application {
//Rectangle VBox嵌套HBox
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        //把HBox套入VBox里

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(0,0,0,0));
        vBox.setAlignment(Pos.CENTER);
        for(int n = 0;n < 8;n++) {
            if(n%2==0)
                vBox.getChildren().add(getHBox1());
            else
                vBox.getChildren().add(getHBox2());
        }

        Scene scene = new Scene(vBox);
        primaryStage.setTitle("Exercise14_06");
        primaryStage.show();
        primaryStage.setScene(scene);
    }
    //创建第一种类型的横排格子,白黑白黑白黑白黑
    private HBox getHBox1(){
        HBox hBox1 = new HBox();
        hBox1.setPadding(new Insets(0,0,0,0));
        hBox1.setAlignment(Pos.CENTER);

        for(int i = 17;i < 25;i++) {
            Rectangle rectangle = new Rectangle(i, i, 34, 34);
            rectangle.setStroke(Color.BLACK);
            if(i%2==0)
                rectangle.setFill(Color.BLACK);
            else
                rectangle.setFill(Color.WHITE);

            hBox1.getChildren().add(rectangle);
        }
        return hBox1;
    }
    //创建第二种类型的横排格子,黑白黑白黑白黑白
    private HBox getHBox2(){
        HBox hBox2 = new HBox();
        hBox2.setPadding(new Insets(0,0,0,0));
        hBox2.setAlignment(Pos.CENTER);
        for(int i = 17;i < 25;i++) {
            Rectangle rectangle = new Rectangle(i, i, 34, 34);
            rectangle.setStroke(Color.BLACK);
            if(i%2==0)
                rectangle.setFill(Color.WHITE);
            else
                rectangle.setFill(Color.BLACK);

            hBox2.getChildren().add(rectangle);
        }
        return hBox2;
    }

}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值