栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
        


        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生成象棋棋盘

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.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

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

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();
        pane.getChildren().add(getVBox());

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

        for(int i = 9;i < 17;i++) {
            Rectangle rectangle = new Rectangle(i, i, 18, 18);
            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));

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

            hBox2.getChildren().add(rectangle);
        }
        return hBox2;
    }
    //把HBox套入VBox里
    private VBox getVBox(){
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(0,0,0,0));
        for(int n = 0;n < 8;n++) {
           if(n%2==0)
               vBox.getChildren().add(getHBox1());
           else
               vBox.getChildren().add(getHBox2());
        }
        return vBox;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/760448.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号