Java语言程序设计与数据结构(基础篇)课后练习题 第十四章

提示一下:图像文件可以从liveexample.pearsoncmg.com/resource/image.zip获得!

14.1

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class dishisizhang extends Application {

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

@Override
public void start(Stage primaryStage) {
	GridPane pane = new GridPane();
	pane.setPadding(new Insets(5, 5, 5, 5));
	pane.setHgap(10);
	pane.setVgap(10);
	ImageView imageView1 = new ImageView("image/flag1.gif");
	ImageView imageView2 = new ImageView("image/flag5.gif");
	ImageView imageView3 = new ImageView("image/flag6.gif");

	pane.add(imageView1, 0, 0);
	pane.add(imageView2, 0, 1);
	pane.add(imageView3, 1, 1);

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

}

14.2

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class dishisizhang extends Application {

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

@Override
public void start(Stage primaryStage) {
    GridPane pane = new GridPane();
    pane.setPadding(new Insets(8, 8, 8, 8));
    pane.setHgap(8);
    pane.setVgap(8);

    for (int column = 0; column < 3; column++)
        for (int row = 0; row < 3; row++) {
            int i = (int)(Math.random() * 3);
            if (i != 2)
                pane.add(getNode(i), column, row);
        }
    Scene scene = new Scene(pane);
    primaryStage.setTitle("Exercise14_02");
    primaryStage.setScene(scene);
    primaryStage.show();
}

public ImageView getNode(int i) {
    if (i == 0)
        return new ImageView("image/o.gif");
    else
        return new ImageView("image/x.gif");
}

}

14.3

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class dishisizhang extends Application {

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

@Override
public void start(Stage primaryStage) {
    HBox pane = new HBox(5);
    pane.setPadding(new Insets(5, 5, 5, 5));
    Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
            21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
            31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
            41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
            51, 52};
    ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array));
    Collections.shuffle(list);

    ImageView p1 = new ImageView("image/card/" + Integer.valueOf(list.get(0)) + ".png");
    ImageView p2 = new ImageView("image/card/" + Integer.valueOf(list.get(1)) + ".png");
    ImageView p3 = new ImageView("image/card/" + Integer.valueOf(list.get(2)) + ".png");
    pane.getChildren().addAll(p1, p2, p3);

    Scene scene = new Scene(pane);
    primaryStage.setTitle("Exercose14_02");
    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.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class dishisizhang extends Application {

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

@Override
public void start(Stage primaryStage) {
    HBox pane = new HBox(15);
    pane.setPadding(new Insets(15, 15, 15, 15));
    for (int i = 0; i < 5; i++)
        pane.getChildren().add(getVBox());

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

public VBox getVBox() {
    VBox pane = new VBox();
    Color color = new  Color(Math.random(), Math.random(), Math.random(), Math.random());
    Text lb = new Text("Java");
    lb.setFont(Font.font("TimesRomes", FontWeight.BOLD, FontPosture.ITALIC, 22));
    lb.setFill(color);
    lb.setRotate(90);
    pane.getChildren().add(lb);
    return pane;
}

}

14.5

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class dishisizhang extends Application {

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

@Override
public void start(Stage primaryStage) {
    Pane pane = new Pane();
    String str = "WELCOM TO JAVA";
    int length = str.length();
    final double centerX = 200, centerY = 200, radius = 100;
    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.RED);
        t.setFont(Font.font("MyFont", FontWeight.BOLD, FontPosture.ITALIC, 15));
        int r = (int)((Math.PI / 2 + i * divide) / (2 * Math.PI) * 360);
        t.setRotate(r);
        pane.getChildren().add(t);
    }

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

}

14.6

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class dishisizhang extends Application {

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

@Override
public void start(Stage primaryStage) {
    GridPane pane = new GridPane();
    for (int column = 0; column < 8; column = column + 2)
        for (int row = 0; row < 8; row = row + 2)
            pane.add(getSquare(column, row, pane, 0), column, row);

    for (int column = 1; column < 8; column = column + 2)
        for (int row = 0; row < 8; row = row + 2)
            pane.add(getSquare(column, row, pane, 1), column, row);

    for (int column = 0; column < 8; column = column + 2)
        for (int row = 1; row < 8; row = row + 2)
            pane.add(getSquare(column, row, pane, 1), column, row);

    for (int column = 1; column < 8; column = column + 2)
        for (int row = 1; row < 8; row = row + 2)
            pane.add(getSquare(column, row, pane, 0), column, row);

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

public Rectangle getSquare(int column, int row, GridPane pane, int judge) {
    Rectangle r = new Rectangle();
    r.xProperty().bind(pane.widthProperty().divide(8).multiply(row));
    r.yProperty().bind(pane.heightProperty().divide(8).multiply(column));
    r.widthProperty().bind(pane.widthProperty().divide(8));
    r.heightProperty().bind(pane.heightProperty().divide(8));

    if (judge == 0){
        r.setFill(Color.WHITE);
        r.setStroke(Color.WHITE);
    } else if (judge == 1) {
        r.setFill(Color.BLACK);
        r.setStroke(Color.BLACK);
    }

    return r;
}

}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字50 设计师:CSDN官方博客 返回首页