📄 picturecombine.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.math.*;
import javax.swing.*;
import javax.swing.text.*;
class PictureScramble extends JFrame implements ActionListener{
private JFrame f=new JFrame("拼图游戏");
private JButton b1=new JButton("开始");
private JButton b2=new JButton("退出");
private JButton b3=new JButton("重新开始");
private JButton b4=new JButton("游戏说明");
private String state;
private int hiddenX=2,hiddenY=2,steps=0;
private JDialog dia1,dia2;
private JPanel p1=new JPanel();
private JPanel p2=new JPanel();
private JLabel label[]=new JLabel[9];
private int[][] current=new int[3][3];
//程序各种组件的添加
public void create(){
f.setFocusable(true);
f.requestFocus();
f.getContentPane().setLayout(new BorderLayout());
p1.setSize(240,240);
p1.setLayout(new GridLayout(3,3));
for(int m=0;m<9;m++){
label[m]=new JLabel("");
label[m].setHorizontalAlignment(JLabel.CENTER);
label[m].setForeground(Color.blue);
p1.add(label[m]);
}
p1.setFocusable(true);
p2.setLayout(new GridLayout(1,5));
f.addKeyListener(new MyKeyListener());
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
f.addWindowListener(new WindowClosing());
f.getContentPane().add("Center",p1);
f.getContentPane().add("South",p2);
f.setSize(800,400);
f.setVisible(true);
}
//各种按钮功能的实现
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1)
{
make();
}
if(e.getSource()==b2)
{
System.exit(0);
}
if(e.getSource()==b3)
{
restart();
}
if(e.getSource()==b4)
{
intro();
}
f.requestFocus();
}
//游戏说明的初始化
public void intro(){
if(dia1==null){
dia1=new JDialog(f,"游戏说明",true);
dia1.setSize(400,300);
dia1.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dia1.removeNotify();
}
});
JPanel pa=new JPanel();
pa.setLayout(new BorderLayout());
{
String str1="( 程序版本:河海大学计信学院)";
JLabel label=new JLabel(str1);
label.setHorizontalAlignment(JLabel.CENTER);
label.setFont(new Font("黑体",Font.PLAIN,15));
label.setForeground(Color.green);
pa.add(label,BorderLayout.NORTH);
}
{
JTextArea area=new JTextArea(50,30);
String str2= "\n\n" +
" 各按钮的功能如下:\n\n "+
" “开始” 在选择了图片后进行游戏。\n\n" +
" “选择图片” 即选择一幅新的图片,以供游戏。\n\n" +
" “查看原图” 查看原图,并在拼图与原图点切换。\n\n" +
" “退出” 离开并关闭游戏。\n\n" +
" 附注: 本游戏采用数字键控制图形的移动,即小键盘\n\n"+
" 的1—9数字键对应于拼图的9个方格顺序一致 ,\n\n"+
" 与空白方格相邻的才能与之交换,移动方格使 \n\n"+
" 之从左到右,从上到下依次为8-1才算成功。 \n\n"+
" Made by 计算机三班第六组 \n"+
" 2008-01-06";
area.setText(str2);
//设置文本为不可编辑状态
area.setEditable(false);
area.setBackground(dia1.getBackground());
pa.add(area, BorderLayout.CENTER);
JScrollPane scrollpanel = new JScrollPane(area);
scrollpanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
scrollpanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pa.add(scrollpanel, BorderLayout.CENTER);
}
dia1.getContentPane().add(pa);
}
//设置dialog位于最接近窗口中心的一侧
dia1.setLocationRelativeTo(f);
//设置dialog不被用户调整大小
dia1.setResizable(false);
dia1.show();
//至于前端
dia1.toFront();
}
//游戏状态的设置
public void isFinish(){
if(label[8].getText()==""){
if(isComplete())
this.state="FINISH";
}
else
this.state="PLAY";
}
public boolean isComplete(){
int j=8;
for(int i=0;i<8;i++,j--){
if(label[i].getText()==String.valueOf(j))
continue;
else
return false;
}
return true;
}
//游戏结束的方法
public void restart(){
this.steps=0;
make();
}
//打乱图像的顺序
public void make(){
if(hiddenX<0||hiddenX>2)
throw new IllegalArgumentException("hiddenX must be 0,1,2.");
if(hiddenY<0||hiddenY>2)
throw new IllegalArgumentException("hiddenY must be 0,1,2.");
int array[][]={
{0,7,5,1,8,6,4,3,2},
{1,6,7,5,2,3,4,8,0},
{1,6,8,3,2,7,5,0,4},
{2,5,8,6,4,0,3,1,7},
{2,4,7,3,1,6,8,5,0},
{3,7,8,0,2,5,1,6,4},
{4,2,0,6,3,5,8,7,1},
{4,1,2,5,8,7,6,3,0},
{5,0,1,8,3,7,4,6,2},
{5,8,0,3,1,2,4,7,6},
{5,1,2,4,8,6,3,7,0},
{4,1,2,5,8,7,6,0,3},
{5,8,6,3,1,2,4,7,0},
{1,8,6,3,7,2,5,0,4},
{5,8,0,3,1,2,4,7,6},
{5,8,6,1,3,4,2,7,0},
{5,7,0,3,4,2,1,8,6},
{5,8,6,3,1,2,4,7,0},
{5,1,2,8,4,6,3,0,7},
{5,8,6,3,1,2,4,7,0},
{1,8,6,3,7,2,5,0,4},
{5,8,3,6,7,2,4,1,0},
{4,5,2,1,8,7,6,0,3},
{5,6,8,3,2,1,7,4,0},
{8,5,6,1,3,0,4,7,2},
{4,8,6,7,1,2,5,3,0},
{5,7,0,3,8,2,4,1,6},
{5,1,7,3,2,8,4,6,0},
{5,4,6,8,1,2,3,7,0},
{5,0,6,7,1,2,4,3,8},
};
//产生随机数来获得拼图方格的数字
int n=(int)(Math.random()*30);
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
current[i][j]=array[n][3*i+j];
}
}
//将数字0调到最右下角的位置
int temp=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(current[i][j]==0){
temp=current[2][2];
current[2][2]=current[i][j];
current[i][j]=temp;
}
}
}
/*初始化各方格的数字,并把
0对应的方格设置为隐藏形式*/
int t=7;
for(int i=0; i<3; i++){
for(int j=0;j<3;j++){
if(current[i][j]==0)
label[8].setText("");
else
label[t].setText(String.valueOf(current[i][j]));
t--;
}
}
}
//拼图完成后的显示设置
public void pass(){
if(dia2==null){
dia2=new JDialog(f,"过关",true);
dia2.setSize(400,300);
dia2.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
JPanel pa=new JPanel();
JLabel la1,la2;
pa.setLayout(new BorderLayout());
String str1="河海大学计信学院";
String str2="恭喜你已完成,"+
"你总共移动 "+this.steps+" 步!";
la1=new JLabel(str1);
la2=new JLabel(str2);
la1.setHorizontalAlignment(JLabel.CENTER);
la2.setHorizontalAlignment(JLabel.CENTER);
la1.setFont(new Font("黑体",Font.PLAIN,20));
la2.setFont(new Font("SIMSUM",Font.PLAIN,20));
pa.add(la1,BorderLayout.NORTH);
pa.add(la2,BorderLayout.CENTER);
dia2.getContentPane().add(pa);
}
//设置dialog位于最接近窗口中心的一侧
dia2.setLocationRelativeTo(f);
//设置dialog不被用户调整大小
dia2.setResizable(false);
dia2.show();
//至于前端
dia2.toFront();
}
//移动方格的实现
public void move(int x,int y){
boolean moved=false;
if(this.state=="FINISH")
return;
//试图移动隐藏方格,直接返回
if(hiddenX==x&&hiddenY==y)
return;
/*如果方格位于(hiddenX,hiddenY)的相邻位置,
则交换(x,y)和(hiddenX,hiddenY)的相关数据*/
if(((x-1)==hiddenX)&&(y==hiddenY)){
sweep(x,y);
moved=true;
}
if(((x+1)==hiddenX)&&(y==hiddenY)){
sweep(x,y);
moved=true;
}
if((x==hiddenX)&&((y-1)==hiddenY)){
sweep(x,y);
moved=true;
}
if((x==hiddenX)&&((y+1)==hiddenY)){
sweep(x,y);
moved=true;
}
if(moved){
steps++;
}
}
public void sweep(int x,int y){
label[3*hiddenX+hiddenY].setText(String.valueOf(label[3*x+y].getText()));
label[3*x+y].setText("");
this.hiddenX=x;
this.hiddenY=y;
//调用方法判断拼图是否完成
isFinish();
if(this.state=="FINISH")
pass();
}
//内部类实现用小键盘数字键移动方格
class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()-96){
case 1:
move(2,0);
break;
case 2:
move(2,1);
break;
case 3:
move(2,2);
break;
case 4:
move(1,0);
break;
case 5:
move(1,1);
break;
case 6:
move(1,2);
break;
case 7:
move(0,0);
break;
case 8:
move(0,1);
break;
case 9:
move(0,2);
break;
default:
break;
}
}
}
}
//外部类实现窗口监听器
class WindowClosing extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public class PictureCombine{
public static void main(String args[]){
PictureScramble psc=new PictureScramble();
psc.create();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -