package text2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MyJframeSuper extends JFrame implements KeyListener , ActionListener{
public static void main(String[] args) {
new MyJframeSuper().painJframe();
}
//下面三个是方便进行行动监听进行换肤判断
JMenuItem jMenuItem1 = new JMenuItem(“经典”);
JMenuItem jMenuItem2 = new JMenuItem(“霓蓝”);
JMenuItem jMenuItem3 = new JMenuItem(“糖果”);
//下面是定义换肤的字符串
String str = “A-”;
int score = 0;
int data[][] = new int[4][4];
//判断上下左右整体移动效果,如果移动失败则游戏失败
public void check() {
if(checkLeft() == false && checkRight() == false && checkUp() == false && checkDown() == false) {
System.out.println("游戏失败");
}
}
//数组拷贝方法
public void copyArray(int src [][],int dest [][]) {
for (int i = 0; i < src.length; i++) {
for (int j = 0; j < src[i].length; j++) {
dest[i][j] = src[i][j];
}
}
}
//每次移动就产生新的图片
public void frushPainting() {
int frushI[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int frushj[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
int w = 0;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
if(data[i][j]==0) {
frushI[w] = i;
frushI[w] = j;
w++;
}
}
}
if(w != 0) {
Random r = new Random();
int index = r.nextInt(w);
int x=frushI[index];
int y=frushI[index];
data[x][y] = 2;
}
}
//产生两张随机图片
public void iniData() {
frushPainting();
frushPainting();
}
//检查左移动
public boolean checkLeft() {
//创建一个型数组
int newArray[][] = new int [4][4];
//定义一个方法用来拷贝数组
copyArray(data, newArray);
//调用左移方法
left();
//设置一个标号
boolean flag = false;
//再来判断一下
lo :
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
if(data[i][j] != newArray[i][j]) {
flag = true;
break lo;
}
}
}
//然后把数组转换回来,用字的数组拷贝方法
copyArray(newArray,data);
return flag;
}
//检查右移动
public boolean checkRight() {
//创建一个型数组
int newArray[][] = new int [4][4];
//定义一个方法用来拷贝数组
copyArray(data, newArray);
//调用右移方法
horizonReserve();
//设置一个标号
boolean flag = false;
//再来判断一下
lo :
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
if(data[i][j] != newArray[i][j]) {
flag = true;
break lo;
}
}
}
//然后把数组转换回来,用字的数组拷贝方法
copyArray(newArray,data);
return flag;
}
//检查上移动
public boolean checkUp() {
//创建一个型数组
int newArray[][] = new int [4][4];
//定义一个方法用来拷贝数组
copyArray(data, newArray);
//调用上移方法
//先逆时针翻转
anticlockwise();
//然后调用左移
left();
//最后顺时针翻转
clockwise();
//设置一个标号
boolean flag = false;
//再来判断一下
lo :
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
if(data[i][j] != newArray[i][j]) {
flag = true;
break lo;
}
}
}
//然后把数组转换回来,用字的数组拷贝方法
copyArray(newArray,data);
return flag;
}
//检查下移动
public boolean checkDown() {
//创建一个型数组
int newArray[][] = new int [4][4];
//定义一个方法用来拷贝数组
copyArray(data, newArray);
//调用下移方法
//先顺时针翻转
clockwise();
//然后调用右移
horizonReserve();
//最后逆时针翻转
anticlockwise();
//设置一个标号
boolean flag = false;
//再来判断一下
lo :
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
if(data[i][j] != newArray[i][j]) {
flag = true;
break lo;
}
}
}
//然后把数组转换回来,用字的数组拷贝方法
copyArray(newArray,data);
return flag;
}
//绘制换肤菜单栏
public void printinfjmenubar() {
JMenuBar jb = new JMenuBar();
JMenu jMenu1 = new JMenu("换肤");
JMenu jMenu2 = new JMenu("关于我们");
jb.add(jMenu1);
jb.add(jMenu2);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jMenu1.add(jMenuItem3);
this.setJMenuBar(jb);
jMenuItem1.addActionListener(this);
jMenuItem2.addActionListener(this);
jMenuItem3.addActionListener(this);
}
//绘制窗体
public void painJframe() {
//绘制窗体
this.setTitle("我的绘图");
printinfjmenubar();
this.setSize(514, 538);
this.addKeyListener(this);
this.setLocation(150, 100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);//这是让窗体显示出来
iniData();
painting( );
}
//绘制窗体背景
public void painting( ) {
getContentPane().removeAll();//消除所有界面
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
JLabel jb = new JLabel(new ImageIcon("E:\\阿鹏apeng\\Jimage\\A-"+data[j][i]+".png"));//从文件夹导入图片
jb.setBounds(50+100*i, 50+100*j, 100, 100);//设置图片的位置
this.getContentPane().add(jb);//把图片添加到窗体
}
}
//下面是设置窗体的背景
JLabel jb1 = new JLabel(new ImageIcon("E:\\阿鹏apeng\\Jimage_background\\"+str+"2.png"));
jb1.setBounds(40, 40, 420, 420);
JLabel jb2 = new JLabel("得分"+":"+score);//这是定义得分的字样
jb2.setBounds(40, 20, 100, 20);
this.getContentPane().add(jb2);
this.getContentPane().add(jb1);
getContentPane().repaint();//刷新界面
}
//图片下移
public void clockwise() {
//这个是顺时针旋转
int newArray[][] = new int [4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
newArray[j][3-i]=data[i][j];
}
}
data = newArray;
}
//图片上移
public void anticlockwise() {
//这个是逆时针旋转
int newArray[][] = new int [4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
newArray[3-j][i]=data[i][j];
}
}
data = newArray;
}
//图片左移
public void left() {
//下面是程序左移代码
for (int i = 0; i < data.length; i++) {
int b[] = new int[4];
int index = 0;
for (int x = 0; x < data[i].length; x++) {
if(data[i][x]!=0) {
b[index] = data[i][x];
index++;
}
}
data[i]=b;
//下面判断前面的数和后面的数是否相同,是相同则合并
for (int k = 0; k < 3; k++) {
if(data[i][k] == data[i][k+1]) {
data[i][k] *= 2;
score += data[i][k];
for (int j = k+1; j < 3; j++) {
data[i][j] = data[i][j+1];
}
data[i][3] = 0;
}
}
}
}
//图片右移
public void horizonReserve() {
//这个是水平翻转代码
for (int i = 0; i < data.length; i++) {
int array[] = new int [4];
for (int start = 0 , end = array.length -1; start