package PingTu_Jframe;
import test2.NewFrame;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
//先建立框架
//界面:标题,大小,位置,关闭模式,置顶,默认位置取消
//菜单:建立jmenubar, jmenu , jmenuitem
//背景图片,主体图片,数组建立,填入图片,用到jlabel , imageicon
//implements接口,在重写方法中键入事件
//作弊码,胜利
public class GameJframe extends JFrame implements KeyListener, ActionListener {
//创建一个数组存储打乱后的位置
int[][] arr1 = new int[4][4];
int[][] arr2 ={{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};
//定义空白图片的坐标
int x, y;
//计步器
int count = 0;
//绑定菜单选项
//创建选项下的条目对象
JMenuItem replayItem = new JMenuItem("重新开始");
JMenuItem reloginItem = new JMenuItem("重新登录");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("寻求合作");
//创建路径
String path = "C:\\Users\\13270\\Desktop\\day7\\image\\animal\\animal3\\";
public GameJframe() {
//初始化界面
initialJFram();
//初始化菜单
initialMenu();
//获取打乱后的数组
initialarr();
//初始化图片
initialpicture();
//整个界面接受键盘的事件
this.addKeyListener(this);
//设置界面的启动,设置在最后面
this.setVisible(true);
}
private void initialBackGroud2() {
JLabel j = new JLabel(new ImageIcon("C:\\Users\\13270\\Desktop\\day7\\src\\111.jpg"));
j.setBounds(0, 0, 703, 775);
this.getContentPane().add(j);
}
private void initialBackGroud() {
//将图片放入容器中
JLabel j = new JLabel(new ImageIcon("C:\\Users\\13270\\Desktop\\day7\\image\\background.png"));
//设置放的位置
j.setBounds(90, 100, 508, 560);
//放进界面中
this.getContentPane().add(j);
initialBackGroud2();
}
private void initialarr() {
//先对数组进行初始化
int arr2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
Random ran = new Random();
//打乱数组
for (int i = 0; i < arr2.length; i++) {
int index = ran.nextInt(arr2.length);
int temp = arr2[i];
arr2[i] = arr2[index];
arr2[index] = temp;
}
//将打乱后的数组的值放入到二维数组中
for (int i = 0; i < arr2.length; i++) {
arr1[i / 4][i % 4] = arr2[i];
if (arr2[i] == 0) {
//获取空白图片的坐标
x = i / 4;
y = i % 4;
// System.out.println(x);
// System.out.println(y);
}
}
}
private void initialMenu() {
//设置整个菜单对象
JMenuBar jMenuBar = new JMenuBar();
//创建菜单上的选项
JMenu functionMenu = new JMenu("功能");
JMenu aboutUs = new JMenu("关于我们");
//为菜单选项添加事件
replayItem.addActionListener(this);
reloginItem.addActionListener(this);
closeItem.addActionListener(this);
accountItem.addActionListener(this);
//将条目对象放入到菜单的选项中
functionMenu.add(replayItem);
functionMenu.add(reloginItem);
functionMenu.add(closeItem);
aboutUs.add(accountItem);
//将选项放入bar中
jMenuBar.add(functionMenu);
jMenuBar.add(aboutUs);
//给整个界面设置菜单
this.setJMenuBar(jMenuBar);
}
private void initialJFram() {
//设置界面的大小
this.setSize(703, 775);
//设置界面的标题
this.setTitle("拼图小游戏嘿嘿");
//设置界面置顶
this.setAlwaysOnTop(true);
//设置界面居中
this.setLocationRelativeTo(null);
//设置程序的关闭模式
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//取消默认居中位置
this.setLayout(null);
}
//定义一个放入图片的方法
public void initialpicture() {
this.getContentPane().removeAll();
//将计步器的数字放到背景中
JLabel j2 = new JLabel("步数:"+count);
j2.setBounds(50,50,50,50);
this.getContentPane().add(j2);
//表示胜利
if(victor())
{
JLabel j1 = new JLabel(new ImageIcon("C:\\Users\\13270\\Desktop\\day7\\image\\win.png"));
j1.setBounds(250,250,193,73);
this.getContentPane().add(j1);
this.getContentPane().repaint();
}
// System.out.println(arr1[x][y]);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
//创造一个图片对象
//创建一个管理容器对象
//将图片放到容器里面
int num = arr1[i][j];
JLabel j1 = new JLabel(new ImageIcon(path+ num + ".jpg"));
//设置图片在容器中的初始位置
j1.setBounds(j * 105 + 134, i * 105 + 194, 105, 105);
//给图片添加边框
j1.setBorder(new BevelBorder(BevelBorder.RAISED));
//将容器放入界面中
this.getContentPane().add(j1);
}
}
//初始化背景图片
initialBackGroud();
this.getContentPane().repaint();
}
//判断是否已经胜利
private boolean victor() {
for (int i = 0; i < 4; i++) {
for (int i1 = 0; i1 < 4; i1++) {
if( arr1[i][i1] !=arr2[i][i1])
{return false;}
}
}
return true;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int a = e.getKeyCode();
if(victor())
{
return;
}
if(a==66)
{
//显示完整图片
System.out.println(a);
this.getContentPane().removeAll();
JLabel j = new JLabel(new ImageIcon("C:\\Users\\13270\\Desktop\\day7\\image\\animal\\animal3\\all.jpg"));
j.setBounds(134,194,420,420);
this.getContentPane().add(j);
initialBackGroud();
this.getContentPane().repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
Object o = e.getSource();
int k = e.getKeyCode();
System.out.println(k);
//如果胜利了就直接返回
if(victor())
{
return;
}
if (k == 37) {
//当y等于三时时,他的右边已经没有图片了,直接返回
if (y == 3) {
return;
}
//让空白图片的数字和右边交换
arr1[x][y] = arr1[x][y + 1];
arr1[x][y + 1] = 0;
y++;
//计步器加一
count++;
//重新加载图片
initialpicture();
} else if (k == 38) {
if (x == 3) {
return;
}
//让空白图片的数字和下边交换
arr1[x][y] = arr1[x + 1][y];
arr1[x + 1][y] = 0;
x++;
//计步器加一
count++;
//重新加载图片
initialpicture();
} else if (k == 39) {
if (y == 0) {
return;
}
//让空白图片的数字和左边交换
arr1[x][y] = arr1[x][y - 1];
arr1[x][y - 1] = 0;
y--;
//计步器加一
count++;
//重新加载图片
initialpicture();
} else if (k == 40) {
if (x == 0) {
return;
}
//让空白图片的数字和右边交换
arr1[x][y] = arr1[x - 1][y];
arr1[x - 1][y] = 0;
x--;
//计步器加一
count++;
//重新加载图片
initialpicture();
} else if (k == 65) {
arr1 = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0},
};
initialpicture();
}else if(k==66)
{
//刷新图片
this.getContentPane().removeAll();
initialpicture();
this.getContentPane().repaint();
}
}
@Override
public void actionPerformed(ActionEvent e) {
Object s = e.getSource();
//点击重新开始时
if(s == replayItem)
{
//打乱数组
initialarr();
//计步器清零
count = 0;
//加载图片
initialpicture();
//刷新
this.getContentPane().repaint();
}
//重新登录界面
else if(s == reloginItem)
{
//先把界面关闭
this.setVisible(false);
//打开登录界面
new LoginJframe();
}
//关闭游戏
else if(s == closeItem)
{
System.exit(0);
}
//关于我们
else if (s == accountItem) {
//建立一个对话框
//初始化
//将图片放进去
JDialog jd = new JDialog();
jd.setSize(1280,960);
jd.setLocationRelativeTo(null);
//不关闭这个无法点击下面的界面
jd.setModal(true);
jd.setAlwaysOnTop(true);
jd.setLayout(null);
JLabel jb = new JLabel(new ImageIcon("C:\\Users\\13270\\Desktop\\day7\\src\\222.jpg"));
jb.setBounds(0,0,1280,960);
jd.getContentPane().add(jb);
jd.setVisible(true);
}
}
}
03-14
851