📄
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.lang.Throwable;
class Person extends JButton implements FocusListener //按纽类的定义与实现
{
int number;
ImageIcon image;
Person(int number,ImageIcon image)
{
this.number = number;
this.image=image;
addFocusListener(this);
}
public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
}
}
class pintu extends JFrame implements ActionListener, MouseListener, KeyListener
{
Person person[] = new Person[8];//声明拼图游戏的元素
JButton left, right, blelow, above;
long time1,time2,shijiancha,min,sec,mic;
int casecount;
String str=String.valueOf(time1);
long count=0,record=0; boolean Flag=false;
JButton restart = new JButton("AGAIN");
JButton EXIT=new JButton("EXIT");
JButton btshow = new JButton();
JButton tianchong=new JButton();
JTextField text1 = new JTextField(),text3=new JTextField();
TextArea text2=new TextArea("在游戏进行中可以键盘和鼠标配合使用,其中在文本框显示的数值是评价玩家游戏水平的标准!");
JLabel label=new JLabel("说明");
public pintu()
{
init();
setBounds(200, 200, 200, 200);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void init()//初始化窗体的个组件以及相关容器
{
setLayout(null);
add(restart);
add(btshow);
add(text1);
add(EXIT);
add(label);
add(text2);
add(text3);
add(tianchong);
label.setVisible(true);
label.setBounds(100,50,50,50);
EXIT.addActionListener(this);
EXIT.setBounds(400,0,110,100);
btshow.setBounds(700,0,600,700);
btshow.setIcon(new ImageIcon("E:/2/10.gif"));
text1.setBounds(100, 0, 150, 50);
text3.setBounds(250,0,150,50);
text1.setVisible(true);
text2.setBounds(150,50,250,50);
restart.setBounds(512,0,112,100);
restart.setBackground(Color.blue);
restart.addActionListener(this);
for (int i = 0; i < 8; i++)//确定各按钮的初始位置与图片
{
person[i] = new Person(i,new ImageIcon());
person[i].addMouseListener(this);
person[i].addKeyListener(this);
add(person[i]);
}
java.util.Random r=new java.util.Random(); //产生随机器数来控制按钮的位置
casecount=(int)(Math.random()*4)+1;
System.out.println(casecount);
person[0].setIcon(new ImageIcon("E:/2/"+1+".gif")); //图片与按钮形成一对一,便于控制后可能出现的情形
person[1].setIcon(new ImageIcon("E:/2/"+2+".gif"));
person[2].setIcon(new ImageIcon("E:/2/"+3+".gif"));
person[3].setIcon(new ImageIcon("E:/2/"+4+".gif"));
person[4].setIcon(new ImageIcon("E:/2/"+5+".gif"));
person[5].setIcon(new ImageIcon("E:/2/"+6+".gif"));
person[6].setIcon(new ImageIcon("E:/2/"+7+".gif"));
person[7].setIcon(new ImageIcon("E:/2/"+8+".gif"));
switch(casecount) //随机生成各图片的位置,从而达到控制游戏难度(由于情形比较多,因此只随机列出几种图片位置)
{
case 1:
person[0].setBounds(100, 100, 170, 190);
person[1].setBounds(275, 100,170, 190);
person[2].setBounds(450, 100, 170, 190);
person[3].setBounds(100, 290, 170,190);
person[4].setBounds(275, 290, 170, 190);
person[5].setBounds(450, 290, 170, 190);
person[6].setBounds(100, 480, 170, 190);
person[7].setBounds(450, 480, 170, 190);
person[7].requestFocus();
break;
case 2:
person[0].setBounds(100, 480, 170, 190);
person[1].setBounds(100, 100, 170, 190);
person[3].setBounds(275, 100,170, 190);
person[2].setBounds(450, 100, 170, 190);
person[4].setBounds(100, 290, 170,190);
person[6].setBounds(275, 290, 170, 190);
person[5].setBounds(450, 290, 170, 190);
person[7].setBounds(275, 480, 170, 190);
person[7].requestFocus();
break;
case 3:
person[4].setBounds(100, 100, 170, 190);
person[5].setBounds(275, 100,170, 190);
person[3].setBounds(450, 100, 170, 190);
person[7].setBounds(100, 290, 170,190);
person[2].setBounds(275, 290, 170, 190);
person[1].setBounds(450, 290, 170, 190);
person[0].setBounds(100, 480, 170, 190);
person[6].setBounds(450, 480, 170, 190);
person[6].requestFocus();
break;
case 4:
person[7].setBounds(100, 100, 170, 190);
person[6].setBounds(275, 100,170, 190);
person[5].setBounds(450, 100, 170, 190);
person[4].setBounds(100, 290, 170,190);
person[3].setBounds(275, 290, 170, 190);
person[2].setBounds(450, 290, 170, 190);
person[1].setBounds(100, 480, 170, 190);
person[0].setBounds(450, 480, 170, 190);
person[7].requestFocus();
break;
default:
person[0].setBounds(100, 100, 170, 190);
person[1].setBounds(275, 100,170, 190);
person[3].setBounds(100, 290, 170,190);
person[4].setBounds(275, 290, 170, 190);
person[5].setBounds(450, 290, 170, 190);
person[6].setBounds(100, 480, 170, 190);
person[2].setBounds(450, 480, 170, 190);
person[7].setBounds(450,100,170,190);
person[2].requestFocus();
}
left = new JButton();
right = new JButton();
above = new JButton();
blelow = new JButton();
tianchong.setVisible(false);
time1= System.currentTimeMillis();
text3.setText(str);
validate();//让所有组件都正常显示出来
}
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
public void keyPressed(KeyEvent e)//四个方向键的功能实现
{
Person man = (Person)e.getSource();
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
go(man, blelow);
}
if (e.getKeyCode() == KeyEvent.VK_UP)
{ go(man, above); }
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{ go(man, left); }
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{ go(man, right); }
}
public void mousePressed(MouseEvent e)//点击鼠标后的处理
{
Person man = (Person)e.getSource();
int x = -1, y = -1;
x = e.getX();
y = e.getY();
System.out.println("x=" + x + "y=" + y);
int w = man.getBounds().width;
int h = man.getBounds().height;
boolean flag = false;
if ((y > h / 2))
{
flag = go(man, blelow);
if(check())
getimage();
tianchong.setVisible(true);
}
if ((!flag) && (y < h / 2))
{
flag = go(man, above);
if(check())
getimage();
tianchong.setVisible(true);
}
if ((!flag) && x < w / 2)
{
flag = go(man, left);
if(check())
getimage();
tianchong.setVisible(true);
}
if ((!flag) && x > w / 2)
{
flag = go(man, right);
if(check())
getimage();
tianchong.setVisible(true);
}
count=count+1;
time2 = System.currentTimeMillis(); //计算时间差
shijiancha=time2-time1;
sec=shijiancha/1000;
mic=shijiancha%1000;
min=0;
if(sec>=60)
{
min=min+1;
sec=shijiancha/1000-60;
mic=shijiancha%1000;
}
System.out.println(min+":"+"minutes" +":"+sec+" seconds "+ ":"+mic +" micr");
text3.setText(min+":" + sec+ ":"+mic); //显示时间
System.out.println(count); //测试
text1.setText("你已经点击了"+String.valueOf( count)+"次"); //显示点击图片次数,做为衡量游戏者的水平
}
public void mouseReleased(MouseEvent e) { }//鼠标的各种事件
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public boolean go(Person man, JButton direction)//控制按钮的移动
{
boolean move = true;
Rectangle manRect = man.getBounds();
int x = man.getLocation().x;
int y = man.getLocation().y;
if( (direction == blelow)&&(y<=290))
{
y = y + 190;
x = man.getLocation().x;
}
else if((direction == above)&&(y>=290) )
{
y = y - 190;
x = man.getLocation().x;
}
else if ((direction == left)&&(x>=275))
{
x = x - 175;
y = man.getLocation().y;
}
else if ((direction == right)&&(x<=275) )
{
x = x + 175;
y = man.getLocation().y;
}
manRect.setLocation(x, y);
for (int k = 0; k <= 7; k++)
{
Rectangle personRect = person[k].getBounds();
if (manRect.intersects(personRect) && (man != person[k]))
{
move = false;
}
}
if (move == true)
{
man.setLocation(x, y);
return true;
}
return false;
}
public void actionPerformed(ActionEvent e)//监听事件的实现
{ if(e.getSource()==restart)
{
dispose();
new pintu();
}
else if(e.getSource()==EXIT)
{
System.exit(0);
}
}
public boolean check() //判断图片是否在正确位置 ////使用递归算法实现值的传递
{
boolean flag=true;
boolean flag1[]=new boolean[8];
for(int i=0;i<flag1.length;i++)
{
flag1[i]=false;
}
int x1,x2,y1,y2;
x1=person[5].getBounds().x;
y1=person[5].getBounds().y;
if((x1==100)&&(y1==100))
{
flag1[0]=true;
}
flag=flag&&flag1[0];
x1=person[2].getBounds().x;y1=person[2].getBounds().y;
if((x1==275)&&(y1==100))
{
flag1[1]=true;
}
flag=flag&&flag1[1];
x1=person[6].getBounds().x;y1=person[6].getBounds().y;
if((x1==450)&&(y1==100))
{
flag1[2]=true;
}
flag=flag&&flag1[2];
x1=person[4].getBounds().x;y1=person[4].getBounds().y;
if((y1==290)&&(x1==100))
{
flag1[3]=true;
}
flag=flag&&flag1[3];
x1=person[1].getBounds().x;y1=person[1].getBounds().y;
if((x1==275)&&(y1==290))
{
flag1[4]=true;
}
flag=flag&&flag1[4];
x1=person[3].getBounds().x;y1=person[3].getBounds().y;
if((x1==100)&&(y1==480))
{
flag1[5]=true;
}
flag=flag&&flag1[5];
x1=person[0].getBounds().x;y1=person[0].getBounds().y;
if((x1==275)&&(y1==480))
{
flag1[6]=true;
}
flag=flag&&flag1[6];
x1=person[7].getBounds().x;y1=person[7].getBounds().y;
if((x1==450)&&(y1==480))
{
flag1[7]=true;
}
flag=flag&&flag1[7];
Flag=flag;
if(count<10&&Flag)
{
text1.setText("非常不错!");System.out.println("aaaa");
}
else if(count>200&&Flag)
text1.setText("水平有待提高!");
else
text1.setText("不赖,加油!");
return Flag;
}
public void getimage()//显示缺省的图片
{
tianchong.setBounds(450,290,170,190);
tianchong.setIcon(new ImageIcon("E:/2/9.gif"));
tianchong.setVisible(true);
text1.setText("游戏不可过度,请休息下吧!");
}
}
class TEST
{
public static void main(String[] args)
{
pintu game=new pintu();
game.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -