程序小白,希望和大家多交流,共同学习 因为没有第十版的汉语电子书,(有的汉语版是第八版,使用的还是Swing)这部分内容只能使用英语版截屏。
14.7
//显示10 x 10 的表格,每个格点随机生成1 或 0
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class Array_0_1 extends Application
{
@Override
public void start(Stage primaryStage)
{
GridPane pane = new GridPane();
for (int column = 0; column < 10; column++)
{
for (int row = 0; row < 10; row++)
{
pane.add(getTextField(), column, row);
}
}
Scene scene = new Scene(pane);
primaryStage.setTitle(" Array_0_1 ");
primaryStage.setScene(scene);
primaryStage.show();
}
public TextField getTextField()
{
TextField f = new TextField();
f.setPrefColumnCount(1);
int in = (int)(Math.random() * 2);
f.setText(in + "");
return f;
}
}
14.8
//随机显示一副扑克牌每行九张
//采取的随机方式:数组转列表,列表调用Collections.shuffle()方法
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.geometry.Insets;
import javafx.scene.image.ImageView;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
public class ShowAllCard extends Application
{
@Override
public void start(Stage primaryStage)
{
VBox pane = new VBox(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, 53, 54};
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(array));
Collections.shuffle(list);
for (int column = 0; column < 6; column++)
{
pane.getChildren().add(getHBox(column, list));
}
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowCard");
primaryStage.setScene(scene);
primaryStage.show();
}
//每行九张
public HBox getHBox(int column, ArrayList<Integer> list)
{
HBox pane = new HBox(5);
for (int i = 0; i < 9; i++)
{
ImageView image = new ImageView("image/card/" +
Integer.valueOf(list.get(column * 9 + i)) + "" + ".png");
pane.getChildren().add(image);
}
return pane;
}
}
14.9
//画风扇,在一个空心圆中画四个扇叶
//将圆和扇叶都加入到一个面板中返回后在加入 Gridpane 中
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.layout.Pane;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.scene.paint.Color;
public class FourFan extends Application
{
@Override
public void start(Stage primaryStage)
{
GridPane pane = new GridPane();
pane.setPadding(new Insets(10, 10, 10, 10));
for (int column = 0; column < 2; column++)
{
for (int row = 0; row < 2; row++)
{
pane.add(getCircle(), column, row);
pane.add(getArc(), column, row);
}
}
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowFan");
primaryStage.setScene(scene);
primaryStage.show();
}
//生成空心圆
public Pane getCircle()
{
Pane pane = new Pane();
pane.setPadding(new Insets(10, 10, 10, 10));
Circle c = new Circle(100, 100, 80);
// c.setFill(Color.WHITE);
// c.setStroke(Color.BLACK);
c.setStyle("-fx-fill : white; -fx-stroke : black");
pane.getChildren().add(c);
return pane;
}
//生成扇叶
public Pane getArc()
{
Pane pane = new Pane();
pane.setPadding(new Insets(10, 10, 10, 10));
for (int i = 0; i < 4; i++)
{
Arc a = new Arc(100, 100, 60, 60, (i * 90 + 45), 30);
a.setType(ArcType.ROUND);
a.setFill(Color.BLACK);
a.setStroke(Color.BLACK);
pane.getChildren().add(a);
}
return pane;
}
}
14.10
//画一个圆柱的三维图,挡住的部分使用虚线表示
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.scene.layout.Pane;
import javafx.geometry.Insets;
public class PaintCylinder extends Application
{
@Override
public void start(Stage primaryStage)
{
Pane pane = new Pane();
pane.setPadding(new Insets(20, 0, 20, 20));
Ellipse e = new Ellipse(100, 60, 80, 20);
e.setFill(Color.WHITE);
e.setStroke(Color.BLACK);
pane.getChildren().add(e);
Line line1 = new Line(20, 60, 20, 160);
Line line2 = new Line(180, 60, 180, 160);
line1.setStroke(Color.BLACK);
line2.setStroke(Color.BLACK);
pane.getChildren().addAll(line1, line2);
Arc a = new Arc(100, 160, 80, 20, 0, -180);
a.setStroke(Color.BLACK);
a.setFill(Color.WHITE);
pane.getChildren().add(a);
Arc a2 = new Arc(100, 160, 80, 20, 0, 180);
a2.setStroke(Color.BLACK);
a2.setFill(Color.WHITE);
a2.getStrokeDashArray().addAll(6.0, 21.0);
pane.getChildren().add(a2);
Line line3 = new Line(0, 200,300, 200);
line3.setStroke(Color.BLACK);
//加入虚线的方法,getStrokeDashArray().add(double...);
//double数值的第奇数位为实线长度,第偶数位为空白长度
line3.getStrokeDashArray().addAll(10.0, 5.0, 5.0, 10.0);
pane.getChildren().add(line3);
Scene scene = new Scene(pane);
primaryStage.setTitle("PaintCylinder");
primaryStage.setScene(scene);
primaryStage.show();
}
}
14.11
// 画脸
//使用 (x, x)先确定脸,即大圆圆心的位置,然后分别找到其他器官相对于圆心的位置坐标
//使用move改变每个图形的坐标,就可以整体移动脸了。以免贴在窗体边界上
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.geometry.Insets;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Polygon;
import javafx.collections.ObservableList;
public class SmileFace extends Application
{
@Override
public void start(Stage primaryStage)
{
Pane pane = new Pane();
double x = 200.0;
double move = 40;
pane.setPadding(new Insets(0, 0, move, move));
Circle face = new Circle(x + move, x + move, x);
face.setFill(Color.WHITE);
face.setStroke(Color.BLACK);
Ellipse eye1 = new Ellipse(x / 2 + move, x / 2 + move, x / 4, x / 4 * 0.6);
Ellipse eye2 = new Ellipse(3 * x / 2 + move, x / 2 + move, x / 4, x / 4 * 0.6);
eye1.setFill(Color.WHITE);
eye2.setFill(Color.WHITE);
eye1.setStroke(Color.BLACK);
eye2.setStroke(Color.BLACK);
Circle e1 = new Circle(x / 2 + move, x / 2 + move, x / 4 * 0.4);
Circle e2 = new Circle(3 * x / 2 + move, x / 2 + move, x / 4 * 0.4);
e1.setFill(Color.RED);
e1.setStroke(Color.BLACK);
e2.setFill(Color.RED);
e2.setStroke(Color.BLACK);
Polygon nose = new Polygon();
nose.setFill(Color.WHITE);
nose.setStroke(Color.BLACK);
ObservableList<Double> list = nose.getPoints();
list.add(x + move);
list.add(x / 2 + x / 4 * 0.4 + move);
list.add(x - x / 2 * Math.sin(10) + move);
list.add(x + x / 4 + move);
list.add(x + x / 2 * Math.sin(10) + move);
list.add(x + x / 4 + move);
Arc mouth = new Arc(x + move, 3 * x / 2 + move, x / 2, x / 4 , 0, -180);
mouth.setType(ArcType.OPEN);
mouth.setStroke(Color.BLACK);
mouth.setFill(Color.WHITE);
pane.getChildren().addAll(face, nose, eye1, eye2, e1, e2, mouth);
Scene scene = new Scene(pane);
primaryStage.setTitle("SmileFace");
primaryStage.setScene(scene);
primaryStage.show();
}
}
14.12
//按照已给的数据显示数据的柱状图,不同的百分比显示的颜色不同
//将每个柱状图在面板中返回,加入HBox中
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.paint.Color;
import javafx.geometry.Insets;;
public class BarGraph extends Application
{
@Override
public void start(Stage primaryStage)
{
HBox pane = new HBox(10);
pane.setPadding(new Insets(10, 10, 10, 10));
double[] re = {20, 10, 30, 40};
String[] name = {"项目 - 20%", "测试 - 10%", "期中考试 - 30%", "期末考试 - 40%"};
int length = re.length;
double max = re[0];
for (int i = 1; i < length; i++)
{
if (re[i] > max)
{
max = re[i];
}
}
for (int i = 0; i < length; i++)
{
pane.getChildren().add(getRectangle(max, re[i], name[i]));
}
Scene scene = new Scene(pane);
primaryStage.setTitle("BarGraph");
primaryStage.setScene(scene);
primaryStage.show();
}
//返回一个柱状图
public Pane getRectangle(double max, double length, String name)
{
final double LENGTH = 3;
final double WIDTH = 100;
Pane pane = new Pane();
//保证text在柱状图的上方,text 向下移动比 柱状图向下移动少 2 * LENGTH
Text per = new Text(0, (max - length + 8) * LENGTH, name);
per.setStroke(Color.BLACK);
Rectangle re = new Rectangle(0, (max - length + 10) * LENGTH, WIDTH, length * LENGTH);
switch ((int)length / 10)
{
case 1:
re.setFill(Color.BLUE);
break;
case 2:
re.setFill(Color.RED);
break;
case 3:
re.setFill(Color.GREEN);
break;
case 4:
re.setFill(Color.ORANGE);
break;
}
re.setStroke(Color.BLACK);
pane.getChildren().addAll(re, per);
return pane;
}
}
14.13
//输出饼状图
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.geometry.Insets;
public class PieChart extends Application
{
final double X = 100;
final double R = X * 0.8;
@Override
public void start(Stage primaryStage)
{
Pane pane = new Pane();
pane.setPadding(new Insets(10, 10, 10, 10));
double[] data = {20, 10, 30, 40};
String[] text = {"项目 - 20%", "测试 - 10%", "期中考试 - 30%", "期末考试 - 40%"};
int length = data.length;
double divide = Math.PI / length ;
double start = 0;
for (int i = 0; i < length; i++)
{
pane.getChildren().add(getArc(data[i], start));
start += data[i];
}
start = 0;
for (int i = 0; i < length; i++)
{
pane.getChildren().add(getText(data[i], text[i], start));
start += data[i];
}
Scene scene = new Scene(pane);
primaryStage.setTitle("PieChart");
primaryStage.setScene(scene);
primaryStage.show();
}
//根据提供的数据和起始角度画饼状图
public Arc getArc(double data, double start)
{
double startAngle = start / 100 * 360;
double length = data / 100 * 360;
Arc arc = new Arc(X, X, R, R, startAngle, length);
arc.setType(ArcType.ROUND);
switch ((int)data / 10)
{
case 1:
arc.setFill(Color.BLUE);
break;
case 2:
arc.setFill(Color.RED);
break;
case 3:
arc.setFill(Color.GREEN);
break;
case 4:
arc.setFill(Color.ORANGE);
break;
}
arc.setStroke(Color.BLACK);
return arc;
}
//为每块饼状图显示文档
//文档输出起始点是每份饼状图弧线的中点
//start + date / 2 在100 中占的比例正好是中点在整个圆(2 * Math.PI)所占角度的比例
public Text getText(double data, String name, double start)
{
double proportion = (data / 2 + start) / 100;
Text text = new Text(X + R * Math.cos(2 * Math.PI * proportion),
X - R * Math.sin(2 * Math.PI * proportion), name);
return text;
}
}
14.14
//画一个立方体使它的大小随着窗体改变
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polyline;
import javafx.collections.ObservableList;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
public class Cube extends Application
{
@Override
public void start(Stage primaryStage)
{
Pane pane = new Pane();
DoubleProperty width = new SimpleDoubleProperty();
DoubleProperty height = new SimpleDoubleProperty();
//pane.widthProperty()是ReadOnlyProperty()无法转换成DubleProperty
width.bind(pane.widthProperty());
height.bind(pane.heightProperty());
//按照选择好的比例设置8个点相对于窗体的位置
//使用斜线都是45°角设定
DoubleProperty x1 = new SimpleDoubleProperty();
x1.bind(width.divide(3));
DoubleProperty y1 = new SimpleDoubleProperty();
y1.bind(height.divide(6));
DoubleProperty x2 = new SimpleDoubleProperty();
x2.bind(width.multiply(5).divide(6));
DoubleProperty y2 = new SimpleDoubleProperty();
y2.bind(height.divide(6));
DoubleProperty x3 = new SimpleDoubleProperty();
x3.bind(width.multiply(5).divide(6));
DoubleProperty y3 = new SimpleDoubleProperty();
y3.bind(height.multiply(2).divide(3));
DoubleProperty x4 = new SimpleDoubleProperty();
x4.bind(width.divide(3));
DoubleProperty y4 = new SimpleDoubleProperty();
y4.bind(height.multiply(2).divide(3));
DoubleProperty x5 = new SimpleDoubleProperty();
x5.bind(width.divide(6));
DoubleProperty y5 = new SimpleDoubleProperty();
y5.bind(height.multiply(5).divide(6));
DoubleProperty x6 = new SimpleDoubleProperty();
x6.bind(width.divide(6));
DoubleProperty y6 = new SimpleDoubleProperty();
y6.bind(height.divide(3));
DoubleProperty x7 = new SimpleDoubleProperty();
x7.bind(width.multiply(2).divide(3));
DoubleProperty y7 = new SimpleDoubleProperty();
y7.bind(height.divide(3));
DoubleProperty x8 = new SimpleDoubleProperty();
x8.bind(width.multiply(2).divide(3));
DoubleProperty y8 = new SimpleDoubleProperty();
y8.bind(height.multiply(5).divide(6));
DoubleProperty[] x = {x1, x2, x3, x4, x5, x6, x7, x8};
DoubleProperty[] y = {y1, y2, y3, y4, y5, y6, y7, y8};
//先使用循环一次画出最多的线条
//之后在做补充
for (int i = 0; i < 7; i++)
{
Line line = new Line();
line.startXProperty().bind(x[i]);
line.startYProperty().bind(y[i]);
line.endXProperty().bind(x[i + 1]);
line.endYProperty().bind(y[i + 1]);
pane.getChildren().add(line);
}
Line line1 = new Line();
line1.startXProperty().bind(x1);
line1.startYProperty().bind(y1);
line1.endXProperty().bind(x4);
line1.endYProperty().bind(y4);
Line line2 = new Line();
line2.startXProperty().bind(x1);
line2.startYProperty().bind(y1);
line2.endXProperty().bind(x6);
line2.endYProperty().bind(y6);
Line line3 = new Line();
line3.startXProperty().bind(x2);
line3.startYProperty().bind(y2);
line3.endXProperty().bind(x7);
line3.endYProperty().bind(y7);
Line line4 = new Line();
line4.startXProperty().bind(x3);
line4.startYProperty().bind(y3);
line4.endXProperty().bind(x8);
line4.endYProperty().bind(y8);
Line line5 = new Line();
line5.startXProperty().bind(x5);
line5.startYProperty().bind(y5);
line5.endXProperty().bind(x8);
line5.endYProperty().bind(y8);
pane.getChildren().addAll(line1, line2, line3, line4, line5);
Scene scene = new Scene(pane);
primaryStage.setTitle("Cube");
primaryStage.setScene(scene);
primaryStage.show();
}
}
14.15
//一个STOP警示牌
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.collections.ObservableList;
import javafx.scene.layout.Pane;
import javafx.geometry.Insets;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.FontPosture;
public class Warning extends Application
{
@Override
public void start(Stage primaryStage)
{
Pane pane = new Pane();
pane.setPadding(new Insets(20, 20, 20, 20));
final double X = 300;
final double R = 200;
Polygon line = new Polygon();
line.setStroke(Color.RED);
line.setFill(Color.RED);
ObservableList<Double> list = line.getPoints();
for (int i = 0; i < 6; i++)
{
//一定注意三角函数使用的是弧度制
list.add(X + R * Math.cos(i * Math.PI / 3));
list.add(X - R * Math.sin(i * Math.PI / 3));
}
pane.getChildren().add(line);
double startX = X - R / 3 * 2;
double startY = X;
double size = 100;
//size * 0.4 保证字体在中间
Text stop = new Text(startX, startY + size * 0.4, "STOP");
//Text中的字体设置当然是setFont()
stop.setFont(Font.font("MyFont", FontWeight.BOLD, FontPosture.ITALIC, size));
stop.setFill(Color.WHITE);
pane.getChildren().add(stop);
Scene scene = new Scene(pane);
primaryStage.setTitle("Warning");
primaryStage.setScene(scene);
primaryStage.show();
}
}