package propintu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Font;
import java.awt.Rectangle;
public class Pintu extends JFrame implements Runnable {
ImageIcon imgAll[] = new ImageIcon[10]; //装载大图片
String img_num[] = new String[10]; //大图片序号
Image img_All;
Image m_Img[] = new Image[9]; //装载小图片
int no_Img = -1; //定义没有图片的值
int m_ImgNo[][] = new int[3][3]; //小图片序号
final int img_Width = 120; //小图片的宽度
final int img_Height = 120; //小图片的高度
boolean beShowImgAll = false; //是否显示全图
final int DELTAX = 180; //图片区域与窗体X方向距离
final int DELTAY = 50; //图片区域与窗体Y方向距离
int nStep = 0; //步数
int nTime = 0; //时间
int nScore = 0; //分数
Thread timer;
boolean beCloseGame = false; //图是否拼全
final int DIRECTION_UP = 1; //判断方向
final int DIRECTION_DOWN = 2;
final int DIRECTION_LEFT = 3;
final int DIRECTION_RIGHT = 4;
final int DIRECTION_NONE = -1;
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
JButton jButton2 = new JButton();
JLabel jLabel2 = new JLabel();
JTextField nstep = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField nscore = new JTextField();
JTextArea jTextArea1 = new JTextArea();
JPanel img_buffer = new JPanel();
JComboBox jComboBox1;
public Pintu() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
for (int i = 0; i < 10; i++) {
img_num[i] = String.valueOf(i);
imgAll[i] = new ImageIcon("img/pintu" + i + ".jpg");
}
img_All = imgAll[0].getImage();
jComboBox1 = new JComboBox(img_num);
getContentPane().setLayout(null);
jButton1.setBounds(new Rectangle(36, 21, 76, 29));
jButton1.setText("开始");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
nscore.setBackground(Color.cyan);
nscore.setFont(new java.awt.Font("RowdyHeavy", Font.BOLD, 16));
nscore.setForeground(Color.red);
nscore.setBorder(BorderFactory.createLoweredBevelBorder());
nscore.setCaretColor(Color.red);
nscore.setEditable(false);
nscore.setHorizontalAlignment(SwingConstants.CENTER);
nscore.setBounds(new Rectangle(36, 215, 74, 26));
jLabel3.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 16));
jLabel3.setForeground(Color.magenta);
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setText("分数");
jLabel3.setBounds(new Rectangle(49, 194, 46, 22));
nstep.setBackground(Color.cyan);
nstep.setFont(new java.awt.Font("Splash", Font.BOLD, 18));
nstep.setForeground(Color.red);
nstep.setBorder(BorderFactory.createLoweredBevelBorder());
nstep.setCaretColor(Color.white);
nstep.setEditable(false);
nstep.setHorizontalAlignment(SwingConstants.CENTER);
nstep.setBounds(new Rectangle(36, 158, 74, 23));
jLabel2.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 16));
jLabel2.setForeground(Color.blue);
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("步数");
jLabel2.setBounds(new Rectangle(35, 135, 74, 25));
jButton2.setBounds(new Rectangle(36, 269, 75, 29));
jButton2.setText("预览");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jLabel1.setFont(new java.awt.Font("新宋体", Font.BOLD, 14));
jLabel1.setText("图片");
jLabel1.setBounds(new Rectangle(36, 89, 42, 15));
jTextArea1.setBackground(Color.pink);
jTextArea1.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
jTextArea1.setBorder(BorderFactory.createLoweredBevelBorder());
jTextArea1.setEditable(false);
jTextArea1.setBounds(new Rectangle(17, 328, 125, 74));
img_buffer.setBounds(new Rectangle(134, 23, 254, 275));
img_buffer.addKeyListener(new Pintu_img_buffer_keyAdapter(this));
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
this_mouseClicked(e);
}
});
this.getContentPane().add(jTextArea1);
this.getContentPane().add(jButton2);
this.getContentPane().add(nscore);
this.getContentPane().add(jLabel3);
this.getContentPane().add(nstep);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jButton1);
this.getContentPane().add(jComboBox1);
this.getContentPane().add(img_buffer);
jComboBox1.setBounds(new Rectangle(65, 82, 48, 27));
jComboBox1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int b = Integer.parseInt((String) e.getItem());
int a = Integer.parseInt(img_num[b]);
img_All = imgAll[a].getImage();
}
});
}
/*************************
* 判断是否能移动(鼠标)
*/
public int directioncanMove(int nCol, int nRow) {
if (nCol - 1 >= 0) {
if (m_ImgNo[nRow][nCol - 1] == no_Img) {
return DIRECTION_UP;
}
}
if (nCol + 1 <= 2) {
if (m_ImgNo[nRow][nCol + 1] == no_Img) {
return DIRECTION_DOWN;
}
}
if (nRow - 1 >= 0) {
if (m_ImgNo[nRow - 1][nCol] == no_Img) {
return DIRECTION_LEFT;
}
}
if (nRow + 1 <= 2) {
if (m_ImgNo[nRow + 1][nCol] == no_Img) {
return DIRECTION_RIGHT;
}
}
return DIRECTION_NONE;
}
/***********************
* 移动(鼠标)
*/
public void move(int nCol, int nRow, int nDirection) {
switch (nDirection) {
case DIRECTION_UP:
m_ImgNo[nRow][nCol - 1] = m_ImgNo[nRow][nCol];
m_ImgNo[nRow][nCol] = no_Img;
break;
case DIRECTION_DOWN:
m_ImgNo[nRow][nCol + 1] = m_ImgNo[nRow][nCol];
m_ImgNo[nRow][nCol] = no_Img;
break;
case DIRECTION_LEFT:
m_ImgNo[nRow - 1][nCol] = m_ImgNo[nRow][nCol];
m_ImgNo[nRow][nCol] = no_Img;
break;
case DIRECTION_RIGHT:
m_ImgNo[nRow + 1][nCol] = m_ImgNo[nRow][nCol];
m_ImgNo[nRow][nCol] = no_Img;
break;
}
}
/**********************
* 移动(键盘)
*/
public void move(int nDirection) {
int nNoImageCol = -1;
int nNoImageRow = -1;
int i = 0;
int j = 0;
while (i < 3 && nNoImageRow == -1) {
while (j < 3 && nNoImageCol == -1) {
if (m_ImgNo[i][j] == no_Img) {
nNoImageRow = i;
nNoImageCol = j;
}
j++;
}
j = 0;
i++;
}
switch (nDirection) {
case DIRECTION_UP:
if (nNoImageCol == 2) {
return;
}
m_ImgNo[nNoImageRow][nNoImageCol] = m_ImgNo[nNoImageRow][
nNoImageCol + 1];
m_ImgNo[nNoImageRow][nNoImageCol + 1] = no_Img;
bre
用JAVA语言写的一个拼图游戏
4星 · 超过85%的资源 需积分: 3 178 浏览量
2008-08-31
16:49:52
上传
评论 2
收藏 883KB RAR 举报
lulin76827
- 粉丝: 0
- 资源: 7
最新资源
- 基于Qt+opencv+C++实现图像旋转+自动&&手动+直线检测,角度计算+界面操作+源码(期末大作业&课程设计&项目开发)
- 基于servlet的简单游戏管理系统
- matlab基于混沌系统的图像加密.zip
- Fortran语言教程,详细地介绍了Fortran语言
- (函数)图论中最短路径计算D算法MATLAB源代码,修改网络上D算法的错误,并编写通用的MATLAB函数.rar
- 基于matlab 2Dijkstra最短路径算法的matlab程序,希望对大家有所帮助.zip
- python入门学习,基础语法,用法等.zip
- Swift代码转换指南(Swift Swift Code Convension Guide .)
- Python入门到精通.zip
- 基于QT+C++开发的炫酷九宫格主界面+源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈