JAVA大作业

代码

package thegreatwork;

import javafx.application.*;
import javafx.scene.control.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.event.*;
import javafx.scene.input.*;
import javafx.scene.text.*;
import javafx.geometry.*; import java.util.*;
import java.io.*; /*Gui2048
*目的:显示分数和2048游戏,
*颜色伴随着分数的改变而改变*/ public class Gui2048 extends Application { private String outputBoard;
private Board board; private static final int TILE_WIDTH = 106;
// 这是为了未来不同位数的数字提供不同的字体以便看起来美观?
private static final int TEXT_SIZE_LOW = 55;
private static final int TEXT_SIZE_MID = 45; private static final int TEXT_SIZE_HIGH = 35; // 不同的数字对应不同的颜色(改变的是方块的颜色的填充色)
private static final Color COLOR_EMPTY = Color.rgb(238, 228, 218, 0.35);
private static final Color COLOR_2 = Color.rgb(238, 228, 218);
private static final Color COLOR_4 = Color.rgb(237, 224, 200);
private static final Color COLOR_8 = Color.rgb(242, 177, 121);
private static final Color COLOR_16 = Color.rgb(245, 149, 99);
private static final Color COLOR_32 = Color.rgb(246, 124, 95);
private static final Color COLOR_64 = Color.rgb(246, 94, 59);
private static final Color COLOR_128 = Color.rgb(237, 207, 114);
private static final Color COLOR_256 = Color.rgb(237, 204, 97);
private static final Color COLOR_512 = Color.rgb(237, 200, 80);
private static final Color COLOR_1024 = Color.rgb(237, 197, 63);
private static final Color COLOR_2048 = Color.rgb(237, 194, 46);
private static final Color COLOR_OTHER = Color.BLACK;
private static final Color COLOR_GAME_OVER = Color.rgb(238, 228, 218, 0.5);
// 数字大小的差异可以用数字颜色的填充色来使其明显
private static final Color COLOR_VALUE_LIGHT = Color.rgb(249, 246, 242); private static final Color COLOR_VALUE_DARK = Color.rgb(119, 110, 101); private GridPane pane; private int tile;
private Rectangle[][] rectangle;
private Text[][] text;
private Text txt0;
private Text txtScore;
private int[][] grid;
private StackPane pane0;
private double title; /*start
*初始化页面框
*/
@Override
public void start(Stage primaryStage) {
// 界面的初始化
processArgs(getParameters().getRaw().toArray(new String[0])); pane = new GridPane();
pane.setAlignment(Pos.CENTER);//全部居中
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));//设置页边距
pane.setStyle("-fx-background-color: rgb(187, 173, 160)");
//间距的设定
pane.setHgap(15);
pane.setVgap(15); int size = board.GRID_SIZE;
grid = new int[size][size];
grid = board.getGrid();//把数字粘过来 //2048
txt0 = new Text();
txt0.setText("2048");
txt0.setFont(Font.font("Times New Roman", FontWeight.BOLD, 50));
txt0.setFill(Color.BLACK);
//分数
txtScore = new Text();
txtScore.setText("score: " + this.board.getScore());
txtScore.setFont(Font.font("Times New Roman", FontWeight.BOLD, 30));
txtScore.setFill(Color.BLACK); //把“2048”和分数框放到相应的位置上
pane.add(txt0, 0, 0,2, 1);
pane.add(txtScore, 2, 0, size - 2, 1);
//居中
GridPane.setHalignment(txt0, HPos.CENTER);
GridPane.setHalignment(txtScore, HPos.CENTER); //初始化网格的各种属性
rectangle = new Rectangle[size][size];
text = new Text[size][size]; //把数字所在的框和数字进行初始化
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
tile = grid[row][col];//把grid里面的数字取出来 //每一个数字所在的框
rectangle[row][col] = new Rectangle();
rectangle[row][col].setWidth(TILE_WIDTH);
rectangle[row][col].setHeight(TILE_WIDTH);
rectangle[row][col].setFill(getColor(tile));
//把画好的方格放到页面框上
pane.add(rectangle[row][col], col, row + 1); //初始化数字的各种
text[row][col] = new Text();
if (tile == 0) {
text[row][col].setText("");//0为空
} else {
text[row][col].setText(Integer.toString(tile));
}
int txt = getSize(tile);//根据数字大小来决定字体大小,在最下面
text[row][col].setFont(Font.font("Times New Roman", FontWeight.BOLD, txt));//bold:粗体
text[row][col].setFill(getTextColor(tile));
pane.add(text[row][col], col, row+1 );
GridPane.setHalignment(text[row][col], HPos.CENTER); }
}
//有了下面这一段才能把界面显示出来,基本上少一行界面就不能显示。然而每句话是什么含义我并不是非常的清楚
pane0 = new StackPane();//堆栈面板
pane0.getChildren().add(pane);
Scene scene = new Scene(pane0); scene.setOnKeyPressed(new myKeyHandler());//接收山下左右的按键 primaryStage.setTitle("Gui2048");//界面的名称
primaryStage.setScene(scene);
primaryStage.show(); } /*EventHandler.java
*目的:根据玩家输入不同的指令进行不同的操作
*键盘输入*/
private class myKeyHandler implements EventHandler<KeyEvent> {
public void handle(KeyEvent e) {
if (e.getCode().equals(KeyCode.UP)) {
if (board.canMove(Direction.UP)) {
board.move(Direction.UP);
System.out.println("Moving Up");
board.addRandomTile();
}
if (board.isGameOver()) {
gameOver();
}
} else if (e.getCode().equals(KeyCode.DOWN)) {
if (board.canMove(Direction.DOWN)) {
board.move(Direction.DOWN);
System.out.println("Moving Down");
board.addRandomTile();
}
if (board.isGameOver()) {
gameOver();
}
} else if (e.getCode().equals(KeyCode.LEFT)) {
if (board.canMove(Direction.LEFT)) {
board.move(Direction.LEFT);
System.out.println("Moving Left");
board.addRandomTile();
}
if (board.isGameOver()) {
gameOver();
}
} else if (e.getCode().equals(KeyCode.RIGHT)) {
if (board.canMove(Direction.RIGHT)) {
board.move(Direction.RIGHT);
System.out.println("Moving Right");
board.addRandomTile();
}
if (board.isGameOver()) {
gameOver();
}
//按R的话页面顺时针方向旋转90度
} else if (e.getCode().equals(KeyCode.R)) {
board.rotate(true);
System.out.println("Rotate 90 degrees clockwise");
//按S的话这个界面就会被保存下来
} else if (e.getCode().equals(KeyCode.S)) {
System.out.println("Saving Board to " + outputBoard);
try {
board.saveBoard(outputBoard);
} catch (IOException yingjun) {
System.out.println("saveBoard throw an Exception");
}
} paint();//更新画面 }
} /*gameOver
*用来表示这个游戏已经结束了
*/
private void gameOver() {
//创建一个矩形,和之前那个游戏界面一样大小
Rectangle rec = new Rectangle();
rec.setFill(COLOR_GAME_OVER);
rec.setWidth(pane.getWidth());
rec.setHeight(pane.getHeight()); //打印出来"gameover"
Text over = new Text();
over.setText("Game Over!");
over.setFont(Font.font("Impact", FontWeight.BOLD, 50));
over.setFill(Color.BLACK); //把这两个元素添加到界面上
pane0.getChildren().add(rec);
pane0.getChildren().add(over); } /*paint
* 移动之后重新画这个页面
*/
private void paint() {
int size = board.GRID_SIZE;
grid = board.getGrid(); txtScore.setText("score: " + this.board.getScore()); //在移动以后重新打印16个数字框
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
tile = grid[row][col];
rectangle[row][col].setFill(getColor(tile));
if (tile == 0) {
text[row][col].setText("");
} else {
text[row][col].setText(Integer.toString(tile));//把数字变成字符串 }
int txt = getSize(tile);
text[row][col].setFont(Font.font("Times New Roman", FontWeight.BOLD, txt));
text[row][col].setFill(getTextColor(tile)); }
} }
} /*getColor
*让矩形框有颜色
*每一个数字对应一个背景颜色
*/
private Color getColor(int num) {
if (num == 0)
return COLOR_EMPTY;
if (num == 2)
return COLOR_2;
if (num == 4)
return COLOR_4;
if (num == 8)
return COLOR_8;
if (num == 16)
return COLOR_16;
if (num == 32)
return COLOR_32;
if (num == 64)
return COLOR_64;
if (num == 128)
return COLOR_128;
if (num == 256)
return COLOR_256;
if (num == 512)
return COLOR_512;
if (num == 1024)
return COLOR_1024;
if (num == 2048)
return COLOR_2048; return COLOR_OTHER;
} /*getTextColor
*每一串文字对应一个颜色
*数字大小决定颜色
*/
private Color getTextColor(int num) {
if (num < 8) {
return COLOR_VALUE_DARK;
} else {
return COLOR_VALUE_LIGHT;
}
} /*getSize
*决定一串文字的大小
*数字大小决定字体大小
*/
private int getSize(int num) {
if (num < 128) {
return TEXT_SIZE_LOW;
} else if (num < 1024) {
return TEXT_SIZE_MID;
} else {
return TEXT_SIZE_HIGH;
}
} // 用于高级用户,命令行输入
private void processArgs(String[] args) {
String inputBoard = null;
int boardSize = 0; //参数必须成对出现
if ((args.length % 2) != 0) {
printUsage();
System.exit(-1);
} //加工参数
for (int i = 0; i < args.length; i += 2) {
if (args[i].equals("-i")) {
//设置初始的界面,也可以载入其他的存档
inputBoard = args[i + 1];
} else if (args[i].equals("-o")) {
//确定保存的时候输出文件的名字
outputBoard = args[i + 1];
} else if (args[i].equals("-s")) {
//设置界面大小
boardSize = Integer.parseInt(args[i + 1]);
} else { //错误的内容
printUsage();
System.exit(-1);
}
} //对应-o
if (outputBoard == null)
outputBoard = "2048.board";
//对应-s
if (boardSize < 2)
boardSize = 4; //对应-i
try {
if (inputBoard != null)
board = new Board(inputBoard, new Random());
else
board = new Board(boardSize, new Random());
} catch (Exception e) {
System.out.println(e.getClass().getName() +
" was thrown while creating a " +
"Board from file " + inputBoard);
System.out.println("Either your Board(String, Random) " +
"Constructor is broken or the file isn't " +
"formated correctly");
System.exit(-1);
}
} //帮助文档
private static void printUsage() {
System.out.println("Gui2048");
System.out.println("Usage: Gui2048 [-i|o file ...]");
System.out.println();
System.out.println(" Command line arguments come in pairs of the " +
"form: <command> <argument>");
System.out.println();
System.out.println(" -i [file] -> Specifies a 2048 board that " +
"should be loaded");
System.out.println();
System.out.println(" -o [file] -> Specifies a file that should be " +
"used to save the 2048 board");
System.out.println(" If none specified then the " +
"default \"2048.board\" file will be used");
System.out.println(" -s [size] -> Specifies the size of the 2048" +
"board if an input file hasn't been");
System.out.println(" specified. If both -s and -i" +
"are used, then the size of the board");
System.out.println(" will be determined by the input" +
" file. The default size is 4.");
}
}

JAVA大作业汇总1的更多相关文章

  1. JAVA大作业汇总2

    JAVA大作业2 代码 package thegreatwork; //Enum一般用来表示一组相同类型的常量,这里用于表示运动方向的枚举型常量,每个方向对象包括方向向量. public enum D ...

  2. JAVA大作业汇总3

    JAVA大作业3 代码 ``` package thegreatwork; import java.util.; import java.io.; /Board.java 目的:里面有一些关于如何移动 ...

  3. < JAVA - 大作业(2)仿qq即时通讯软件 >

    < JAVA - 大作业(2)仿qq即时通讯软件 > 背景 JAVA上机大作业:设计一个仿qq即时通讯软件 任务简要叙述:设计一款仿QQ的个人用户即时通讯软件,能够实现注册,登陆,与好友聊 ...

  4. java大作业博客--购物车

    Java 大作业----使用MySQL的购物车 一.团队介绍 姓名 任务 李天明.康友煌 GUI设计及代码编写 谢晓淞 业务代码编写.MySQL服务器平台部署.git代码库 严威 类和包的结构关系设计 ...

  5. <JAVA - 大作业(1)文本编辑器 >

    <JAVA - 大作业(1)文本编辑器 > 背景 JAVA上机大作业:qq / 代码评价系统 第一次上机主题是练习JAVA自带的GUI图形化编程 目的:实现一个跟window10记事本界面 ...

  6. java大作业 KShinglingAlgorithm

    wiki上关于KShingling Algorithm(w-shingling)的说明: http://en.wikipedia.org/wiki/W-shingling 摘要: In natural ...

  7. 期末Java Web大作业----简易的学生管理系统

    学生信息管理系统(大作业) 2018-12-21:此文章已在我的网站更新,添加视图介绍等信息,源码请移步下载https://www.jeson.xin/javaweb-sims.html PS:首先不 ...

  8. Java Web大作业——编程导航系统

    title: Java Web大作业--编程导航系统 categories: - - 计算机科学 - Java abbrlink: 40bc48a1 date: 2021-12-29 00:37:35 ...

  9. 史上最全的 Java 新手问题汇总

    史上最全的 Java 新手问题汇总   Java是目前最流行的编程语言之一——它可以用来编写Windows程序或者是Web应用,移动应用,网络程序,消费电子产品,机顶盒设备,它无处不在. 有超过30亿 ...

随机推荐

  1. 二、Python安装扩展库

    第一步:推荐easy_install工具 下载地址:https://pypi.python.org/pypi/setuptools 下载"ez_setup.py"文件; 通过运行c ...

  2. Template Pattern & Strategy Pattern

    详细见<C++设计模式 23种设计模式.pdf 55页> 在面向对象系统的分析与设计过程中经常会遇到这样一种情况:对于某一个业务逻辑(算法实现)在不同的对象中有不同的细节实现,但是逻辑(算 ...

  3. 关于Java中截取字符串

    获取系统时间:to_char(sysdate,'yyyy-mm-dd')截取CREATETIME常量的前10位字符串:CREATETIME.substring(0,10)截取DESCRIPT常量的前2 ...

  4. DeepQA websocket 并发测试

    var client = new Array(); var W3CWebSocket = new Array(); var concurrent = 2; for (var i = 0; i < ...

  5. BestCoder Round #81 (div.2)

    HDU:5670~5764 A题: 是一个3进制计数: #include <bits/stdc++.h> using namespace std; ]; int calc(long lon ...

  6. Netbackup客户端安装网络放通端口需求,及测试网络放通方法

      192.168.1.101网络放通需求如下:   客户端主机 192.168.1.101 到 服务端主机 192.168.1.100 的1556.13720.13724.1378.13782这5个 ...

  7. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  8. 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  9. where are you going ? 反序为:going you are where

    一个反序小算法,就是首尾替换,生成新的反序后的数组

  10. VirtualBox改变虚拟硬盘位置

    原本放虚拟硬盘的位置容量不足,因此将原来的虚拟硬盘放到了一个相对空闲的分区.设置虚拟硬盘位置时出现一点小问题,解决过程记录如下. 1. 将虚拟硬盘复制到目标位置后,假设为“F:\Ubuntu 16.0 ...