Yux1’s Jigsaw
基于Java与Swing制作的拼图小游戏。
本项目综合运用了Java基础课程中学习的封装、继承、多态、抽象类、接口、内部类、以及字符串、集合、数组、循环判断的知识,综合性较强。
参考自黑马程序员Java零基础视频教程_ 上部 _ 阶段项目-01-项目介绍和界面搭建_ 哔哩哔哩_bilibili
功能
-———————
制作过程
搭建界面
初步搭建注册、登录、游戏界面与程序入口
APP.java:
1 2 3 4 5 6 7 8
|
public class APP { public static void main(String[] args){ new SignInJFrame(); } }
|
SignInJFrame.java:
1 2 3 4 5 6 7 8 9 10 11
|
public class SignInJFrame extends JFrame {
public SignInJFrame() { this.setSize(488, 430); this.setVisible(true); }
}
|
GameJFrame.java与SignUpJFrame.java此处省略
界面设置
进行界面相关的设置
GameJFrame.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public GameJFrame(){ initJFrame();
this.setVisible(true); }
private void initJFrame(){ this.setSize(603, 680); this.setTitle("Yux1's Jigsaw V1.0"); this.setAlwaysOnTop(true); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); }
|
菜单搭建
搭建游戏上方的选项菜单,用到JMenuBar、JMenu、JMenuItem。
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
| public GameJFrame(){ initJFrame();
initJMenu();
this.setVisible(true); }
private void initJMenu(){ JMenuBar jMenuBar = new JMenuBar(); JMenu functionMenu = new JMenu("功能"); JMenu aboutMenu = new JMenu("关于我"); JMenuItem replayMenuItem = new JMenuItem("重新游戏"); JMenuItem reLoginMenuItem = new JMenuItem("重新登录"); JMenuItem closeMenuItem = new JMenuItem("关闭游戏"); JMenuItem githubMenuItem = new JMenuItem("github");
functionMenu.add(replayMenuItem); functionMenu.add(reLoginMenuItem); functionMenu.add(closeMenuItem); aboutMenu.add(githubMenuItem); jMenuBar.add(functionMenu); jMenuBar.add(aboutMenu); this.setJMenuBar(jMenuBar); }
|
添加图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| private void initImage(){ int number = 1; for (int i = 0; i < 4; i++){ for (int j = 0; j < 4; j++){ if (number == 16){ break; } String numberString = ""; if (number < 10){ numberString = "0" + number; } else { numberString = String.valueOf(number); } Image image = new ImageIcon("image/image_1/image_1_" + numberString + ".jpg").getImage().getScaledInstance(105, 105, Image.SCALE_SMOOTH); ImageIcon imageIcon = new ImageIcon(image); JLabel jLabel = new JLabel(imageIcon); jLabel.setBounds(105 * j, 105 * i, 105, 105); this.getContentPane().add(jLabel); number++; } } }
|
打乱图片
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 55 56 57 58 59
| private final int[][] data;
public GameJFrame(){
data = new int[4][4];
initJFrame();
initJMenu();
initData();
initImage();
this.setVisible(true); }
private void initData(){ int[] primitiveData = new int[16]; for (int i = 0; i < 16; i++){ primitiveData[i] = i; } for (int i = 0; i < 16; i++){ int randomNumber = new Random().nextInt(16); int temp = primitiveData[i]; primitiveData[i] = primitiveData[randomNumber]; primitiveData[randomNumber] = temp; } for (int i = 0; i < 16; i++){ data[i / 4][i % 4] = primitiveData[i]; } } private void initImage(){ for (int i = 0; i < 4; i++){ for (int j = 0; j < 4; j++){ int number= data[i][j]; String numberString = ""; if (number < 10){ numberString = "0" + number; } else { numberString = String.valueOf(number); } Image image = new ImageIcon("image/image_1/image_1_" + numberString + ".jpg").getImage().getScaledInstance(105, 105, Image.SCALE_SMOOTH); ImageIcon imageIcon = new ImageIcon(image); JLabel jLabel = new JLabel(imageIcon); jLabel.setBounds(105 * j, 105 * i, 105, 105); this.getContentPane().add(jLabel); number++; } } }
|
移动图片
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
| @Override public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == 37){ if (y == 3){ return; } data[x][y] = data[x][y + 1]; data[x][y + 1] = 0; y++; initImage(); } else if (key == 38){ if (x == 3){ return; } data[x][y] = data[x + 1][y]; data[x + 1][y] = 0; x++; initImage(); } else if (key == 39){ if (y == 0){ return; } data[x][y] = data[x][y - 1]; data[x][y - 1] = 0; y--; initImage(); } else if (key == 40){ if (x == 0){ return; } data[x][y] = data[x - 1][y]; data[x - 1][y] = 0; x--; initImage(); }
}
|
查看完整图片
1 2 3 4 5 6 7 8 9 10
| private void initWholeImage(){ this.getContentPane().removeAll(); Image image = new ImageIcon("image/image_1/image_1_all.jpg").getImage().getScaledInstance(600, 600, Image.SCALE_SMOOTH); ImageIcon imageIcon = new ImageIcon(image); JLabel jLabel = new JLabel(imageIcon); jLabel.setBounds(0, 0, 600, 600); this.getContentPane().add(jLabel); this.getContentPane().repaint(); }
|
作弊码
1 2 3 4 5 6 7 8 9 10 11 12 13
| private void cheat(){ int[] primitiveData = new int[16]; for (int i = 0; i < 16; i++){ primitiveData[i] = i; } for (int i = 0; i < 16; i++){ data[i / 4][i % 4] = primitiveData[i] + 1; } x = 3; y = 3; data[3][3] = 0; }
|
菜单功能
未完成