资源描述
// Cell.java
package cn.itcast.picture.ui;
import javax.swing.Icon;
import javax.swing.JButton;
/*
* 图片小方格类
*/
public class Cell extends JButton {
//带有图片的小方格
public Cell(Icon icon) {
super(icon);
//设置小方格大小
this.setSize(150, 150);
}
//带有图片和文字的小方格
public Cell(String text, Icon icon) {
super(text, icon);
//设置小方格大小
this.setSize(150, 150);
this.setHorizontalTextPosition(CENTER);//设置文字水平居中显示
this.setVerticalTextPosition(CENTER);//设置文字垂直居中显示
}
//
public void move(String direction) {
switch (direction) {
case "UP":
this.setLocation(this.getBounds().x,this.getBounds().y-150);
break;
case "DOWN":
this.setLocation(this.getBounds().x,this.getBounds().y+150);
break;
case "LEFT":
this.setLocation(this.getBounds().x-150,this.getBounds().y);
break;
case "RIGHT":
this.setLocation(this.getBounds().x+150,this.getBounds().y);
break;
default:
break;
}
}
}
//PictureCanvas.java
package cn.itcast.picture.ui;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/*
* 拼图类
*/
public class PictureCanvas extends JPanel implements MouseListener {
//静态变量
public static int pictureID = 1;//图片ID
public static int stepNum = 0;//步数
private Cell[] cell;
private boolean hasAddActionListener = false;//表示是否为小方格添加了点击监听,有为true
private Rectangle nullCell;
//构造方法
public PictureCanvas() {
initCanvas();
}
//初始化
public void initCanvas(){
//设置拼图区的位置
this.setLayout(null);//帧布局
//创建12个小方格,并添加到拼图区
cell = new Cell[12];
for(int i = 0;i < 4;i++){
for(int j = 0;j < 3;j++){
//加载图片
int num = i*3+j;
ImageIcon icon = new ImageIcon("picture/"+pictureID+"_"+(num+1)+".gif");
//创建图片小方格
cell[num] = new Cell(icon);
//指定显示位置
cell[num].setLocation(150*j+20, i*150+20);
//把图片小方格添加到拼图区
this.add(cell[num]);
}
}
//删除第12个小方格
this.remove(cell[11]);
nullCell = new Rectangle(320, 470, 150, 150);
hasAddActionListener = false;
}
//重新加载图片,并添加数字提示
public void reloadPictureAddNumber() {
for(int i = 0;i < 4;i++){
for(int j = 0;j < 3;j++){
int num = i*3+j;
ImageIcon icon = new ImageIcon("picture/"+pictureID+"_"+(num+1)+".gif");
cell[num].setIcon(icon);
cell[num].setText(num+1+"");
cell[num].setVerticalTextPosition(getY()/2);
cell[num].setHorizontalTextPosition(getX()/2);
}
}
}
//重新加载图片,并清除数字提示
public void reloadPictureClearNumber() {
for(int i = 0;i < 4;i++){
for(int j = 0;j < 3;j++){
int num = i*3+j;
ImageIcon icon = new ImageIcon("picture/"+pictureID+"_"+(num+1)+".gif");
cell[num].setIcon(icon);
cell[num].setText("");
}
}
}
//start 对小方格重新排序
public void start() {
//如果没有给小方格添加监听,则添加监听
if(!hasAddActionListener){
for(int i=0;i<11;i++){
cell[i].addMouseListener(this);
}
hasAddActionListener=true;
}
//当第一个小方格离左上角比较近的话,进行空方格与就近方格的随机交换
while(cell[0].getBounds().x<=170 && cell[0].getBounds().y<=170){
//获取空方格的位置
int nullX = nullCell.getBounds().x;
int nullY = nullCell.getBounds().y;
//随机产生一个方向,进行空方格的移动互换
//产生0-3之间的随机数,代表交换移动方向
int direction = (int)(Math.random()*4);
switch (direction){
case 0://空方格向左交换移动,原左侧方格向右移动
nullX -= 150;
cellMove(nullX,nullY,"RIGHT");
break;
case 1://空方格向右交换移动,原右侧方格向左移动
nullX += 150;
cellMove(nullX, nullY, "LEFT");
break;
case 2://空方格向上交换移动,原上方方格向下移动
nullY -= 150;
cellMove(nullX, nullY, "DOWN");
break;
case 3://空方格向下交换移动,原下方方格向上移动
nullY += 150;
cellMove(nullX, nullY, "UP");
break;
}
}
}
private void cellMove(int nullX, int nullY, String direction) {
for(int i=0;i<11;i++){
if(cell[i].getBounds().x == nullX && cell[i].getBounds().y == nullY){
//当前方格移动
cell[i].move(direction);
//空方格移动(nullX,nullY)
nullCell.setLocation(nullX,nullY);
break;
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
//获取当前点击的小方格
Cell button = (Cell)e.getSource();
//获取所点击方格的x,y
int clickX = button.getBounds().x;
int clickY = button.getBounds().y;
//获取空方格的x,y
int nullX = nullCell.getBounds().x;
int nullY = nullCell.getBounds().y;
//比较如果满足条件,则互换位置
if(clickX == nullX && clickY - nullY == 150){//点击为空方格下方方格
button.move("UP");
}else if (clickX == nullX && clickY - nullY == -150) {//点击为空方格上方方格
button.move("DOWN");
}else if (clickX - nullX == 150 && clickY == nullY) {//点击为空方格右方方格
button.move("LEFT");
}else if (clickX - nullX == -150 && clickY == nullY) {//点击为空方格左方方格
button.move("RIGHT");
}else {
return;
}
//更新空方格位置
nullCell.setLocation(clickX, clickY);
//拼图区重新绘制
this.repaint();
//更新游戏移动步数
stepNum++;
PictureMainFrame.step.setText("步数:" + stepNum);
//判断若完成,则提示完成
if(isFinish()){
JOptionPane.showMessageDialog(this, "恭喜你完成了,共用"+stepNum+"步。");
//撤销小方格的监听
for(int i=0;i<11;i++){
cell[i].removeMouseListener(this);
}
hasAddActionListener=false;
}
}
private boolean isFinish(){
for (int i = 0; i < 11; i++) {
int x = cell[i].getBounds().x;
int y = cell[i].getBounds().y;
if (((y-20)/150*3+(x-20)/150) != i) {
return false;
}
}
return true;
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
//PictureMainFrame.java
package cn.itcast.picture.ui;
import javax.swing.JFrame;
import java.awt.Rectangle;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/*
* 主界面类
*/
public class PictureMainFrame extends JFrame {
private String[] items = {"小女孩","女明星"};
private JRadioButton addNumberInfo;
private JRadioButton clearNumberInfo;
private PictureCanvas canvas;
private PicturePreview preview;
private JComboBox<String> comboBox;
private JTextField name;
public static JTextField step;
private JButton start;
// 空参数构造方法
public PictureMainFrame() {
//super();
init();//界面初始化
addComponent();//添加组建
addPreviewImage();//添加预览图片与拼图图片
addActionListener();//为组建添加事件监听
}
private void addActionListener() {
//数值提示
addNumberInfo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//完成数字提示的显示
canvas.reloadPictureAddNumber();
}
});
clearNumberInfo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//完成数值提示的清除
canvas.reloadPictureClearNumber();
}
});
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
//获取选择图片的序号
int num = comboBox.getSelectedIndex();
// 更新预览区
PictureCanvas.pictureID = num+1;
preview.repaint();//重新绘制预览区界面
//更新拼图区
canvas.removeAll();
canvas.initCanvas();
canvas.repaint();
// canvas.reloadPictureClearNumber();
//更新游戏状态区
name.setText("图片名称:"+comboBox.getSelectedItem());
PictureCanvas.stepNum = 0;
step.setText("步数:"+0);
//更新按钮区
clearNumberInfo.setSelected(true);
}
});
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 更新游戏状态区 步数:0
PictureCanvas.stepNum = 0;
step.setText("步数:0");
//将小方格位置随机打乱
canvas.start();
}
});
}
private void addPreviewImage() {
//创建面板,包含拼图区和预览区
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,2));//设置成1X2的表格布局
canvas = new PictureCanvas();
canvas.setBorder(new TitledBorder("拼图区"));
preview = new PicturePreview();
preview.setBorder(new TitledBorder("预览区"));
panel.add(canvas,BorderLayout.WEST);
panel.add(preview,BorderLayout.EAST);
//------------------
//把面板显示在主界面中
this.add(panel,BorderLayout.CENTER);
}
private void addComponent() {
JPanel panel = new JPanel();
panel.setBackground(Color.PINK);
panel.setLayout(new GridLayout(1, 2));
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.PINK);
leftPanel.setBorder(new TitledBorder("按钮区"));
addNumberInfo = new JRadioButton("数字提示",false);
addNumberInfo.setBackground(Color.PINK);
clearNumberInfo = new JRadioButton("清楚提示",true);
clearNumberInfo.setBackground(Color.PINK);
comboBox = new JComboBox(items);
comboBox.setPreferredSize(new Dimension(100, 24));
start = new JButton("开始");
start.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
start.setBackground(Color.PINK);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(addNumberInfo);
buttonGroup.add(clearNumberInfo);
leftPanel.add(addNumberInfo);
leftPanel.add(clearNumberInfo);
leftPanel.add(new JLabel(" 选择图片:"));
leftPanel.add(comboBox);
leftPanel.add(start);
JPanel rightPanel = new JPanel();
rightPanel.setBackground(Color.PINK);
rightPanel.setBorder(new TitledBorder("游戏状态"));
rightPanel.setLayout(new GridLayout(1,2));
name = new JTextField("图片名称:小女孩");
step = new JTextField("步数:0");
name.setEditable(false);
step.setEditable(false);
rightPanel.add(name, BorderLayout.WEST);
rightPanel.add(step, BorderLayout.EAST);
panel.add(leftPanel, BorderLayout.WEST);
panel.add(rightPanel, BorderLayout.EAST);
this.add(panel, BorderLayout.NORTH);
}
private void init() {
this.setBounds(new Rectangle(150, 25, 1000, 720));
this.setTitle("拼图游戏");
this.setResizable(false);
//设置窗口默认关闭操作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//PicturePreview.java
package cn.itcast.picture.ui;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
/*
* 图片预览类
*/
public class PicturePreview extends JPanel {
//重写绘制当前组件方法
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
//制定图片文件路径
String filename = "picture/"+PictureCanvas.pictureID+".jpg";
//获取图片文件中的图像
ImageIcon icon = new ImageIcon(filename);
Image image = icon.getImage();
//把图像绘制在预览区中
g.drawImage(image, 20, 20, 450, 600, this);
}
}
// MainApp.java
package cn.itcast.picture.app;
import cn.itcast.picture.ui.PictureMainFrame;
/*
* 启动器
*/
public class MainApp {
public static void main(String[] args) {
// 创建主界面
PictureMainFrame frame=new PictureMainFrame();
//显示界面
frame.setVisible(true);
}
}
展开阅读全文