拼图游戏 java swing

编程入门 行业动态 更新时间:2024-10-07 13:16:14

<a href=https://www.elefans.com/category/jswz/34/1750108.html style=拼图游戏 java swing"/>

拼图游戏 java swing

一.界面介绍

1.基于Java swing的拼图游戏,源码见git:.git,初始效果图:

2.开始效果图:

3.提示效果图:

4.工程代码结构

二.代码介绍

1.程序启动类

public class App {

/**

* @param args

*/

public static void main(String[] args) {

MainFrame frame = new MainFrame();

frame.setVisible(true);

}

}

2.主界面

public class MainFrame extends JFrame{

private String[] items={"小女孩","女明星"};

private JRadioButton addNumInfo;

private PictureCanvsa canvsa;

private JRadioButton clearNum;

private JComboBox comboBox;

private PicturePreview preview;

private JTextField statu;

private JButton start;

public static JTextField step;

public MainFrame(){

initMainFrame();

addCompnent();

addPicture();

addListener();

}

private void addListener() {

addNumInfo.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

canvsa.reloadAddNumInfo();

}

});

clearNum.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

canvsa.reloadAddClearInfo();

}

});

comboBox.addItemListener(new ItemListener() {

@Override

public void itemStateChanged(ItemEvent e) {

int num = comboBox.getSelectedIndex();

canvsa.pictureId = num + 1;

canvsa.reloadAddClearInfo();

preview.repaint();

statu.setText("图片名称:"+comboBox.getSelectedItem());

canvsa.stepNum = 0;

clearNum.setSelected(true);

}

});

start.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

canvsa.stepNum = 0;

step.setText("步数:"+canvsa.stepNum);

clearNum.setSelected(true);

canvsa.start();

}

});

}

private void addPicture() {

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(1,2));

preview = new PicturePreview();

canvsa = new PictureCanvsa();

preview.setBorder(new TitledBorder("预览区"));

canvsa.setBorder(new TitledBorder("拼图区"));

panel.add(canvsa,BorderLayout.WEST);

panel.add(preview,BorderLayout.EAST);

this.add(panel,BorderLayout.CENTER);

}

private void addCompnent() {

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(1,2));

JPanel leftPanel = new JPanel();

leftPanel.setBorder(new TitledBorder("按钮区"));

leftPanel.setBackground(Color.pink);

addNumInfo = new JRadioButton("添加提示",false);

clearNum = new JRadioButton("清除提示",true);

addNumInfo.setBackground(Color.pink);

clearNum.setBackground(Color.pink);

ButtonGroup buttonGroup = new ButtonGroup();

buttonGroup.add(addNumInfo);

buttonGroup.add(clearNum);

JLabel label = new JLabel("                      选择图片:");

comboBox = new JComboBox(items);

start = new JButton("start");

leftPanel.add(addNumInfo);

leftPanel.add(clearNum);

leftPanel.add(label);

leftPanel.add(comboBox);

leftPanel.add(start);

panel.add(leftPanel,BorderLayout.WEST);

JPanel rightPanel = new JPanel();

rightPanel.setLayout(new GridLayout(1, 2));

rightPanel.setBorder(new TitledBorder("游戏状态区"));

rightPanel.setBackground(Color.pink);

statu = new JTextField();

statu.setText("图片名称:小女孩");

step = new JTextField();

step.setText("步数:0");

statu.setEditable(false);

step.setEditable(false);

rightPanel.add(statu,BorderLayout.WEST);

rightPanel.add(step,BorderLayout.EAST);

panel.add(rightPanel,BorderLayout.EAST);

this.add(panel,BorderLayout.NORTH);

}

private void initMainFrame() {

this.setTitle("拼图游戏");

this.setBounds(100, 10, 1000, 720);

this.setResizable(false);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

3.左部游戏操作界面

public class PictureCanvsa extends JPanel implements MouseListener{

private Cell[] cell;

public static int pictureId = 1;

public static int stepNum = 0;

private Rectangle nullCell;

private boolean isAddMouserListener = false;

public PictureCanvsa(){

this.setLayout(null);

cell = new Cell[12];

for(int i = 0;i < 4;i++){

for(int j = 0;j < 3;j++){

ImageIcon icon = new ImageIcon("picture\\"+pictureId+"_"+(i*3+j+1)+".gif");

cell[i*3+j] = new Cell(icon);

cell[i*3+j].setLocation(j*150+20, i*150+20);

this.add(cell[i*3+j]);

}

}

this.remove(cell[11]);

nullCell = new Rectangle();

nullCell.setBounds(320, 470, 150, 150);

}

public void reloadAddNumInfo() {

for(int i = 0;i < 4;i++){

for(int j = 0;j < 3;j++){

ImageIcon icon = new ImageIcon("picture\\"+pictureId+"_"+(i*3+j+1)+".gif");

cell[i*3+j].setIcon(icon);

cell[i*3+j].setText(""+(i*3+j+1));

cell[i*3+j].setHorizontalTextPosition(this.getX()/2);

cell[i*3+j].setVerticalTextPosition(this.getY()/2);

}

}

}

public void reloadAddClearInfo() {

for(int i = 0;i < 4;i++){

for(int j = 0;j < 3;j++){

ImageIcon icon = new ImageIcon("picture\\"+pictureId+"_"+(i*3+j+1)+".gif");

cell[i*3+j].setIcon(icon);

cell[i*3+j].setText("");

}

}

}

public void start() {

if(!isAddMouserListener){

for(int i = 0;i < 4;i++){

for(int j = 0;j < 3;j++){

cell[i*3+j].addMouseListener(this);

}

}

}

while(cell[0].getBounds().x <= 170 && cell[0].getBounds().y <= 170){

int nullX = nullCell.getBounds().x;

int nullY = nullCell.getBounds().y;

int direction = (int) (Math.random()*4);

switch (direction) {

case 0:

nullY -= 150;

move(nullX,nullY,1);

break;

case 1:

nullY += 150;

move(nullX,nullY,0);

break;

case 2:

nullX -= 150;

move(nullX,nullY,3);

break;

case 3:

nullX += 150;

move(nullX,nullY,2);

break;

default:

break;

}

}

}

private void move(int nullX, int nullY, int direction) {

for(int i = 0;i < 4;i++){

for(int j = 0;j < 3;j++){

if(cell[i*3+j].getBounds().x == nullX &&cell[i*3+j].getBounds().y == nullY){

nullCell.setLocation(nullX, nullY);

cell[i*3+j].moveCell(direction);

break;

}

}

}

}

@Override

public void mouseClicked(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseEntered(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mousePressed(MouseEvent e) {

stepNum++;

MainFrame.step.setText("步数:"+stepNum);

int nullX = nullCell.getBounds().x;

int nullY = nullCell.getBounds().y;

Cell button = (Cell) e.getSource();

int clickX = button.getBounds().x;

int clickY = button.getBounds().y;

if(clickX - nullX ==150 &&clickY == nullY){

nullCell.setLocation(nullX+150, nullY);

button.moveCell(2);

}else if(clickX - nullX == -150 &&clickY == nullY){

nullCell.setLocation(nullX-150, nullY);

button.moveCell(3);

}else if(clickX == nullX  &&clickY - nullY == 150){

nullCell.setLocation(nullX, nullY+150);

button.moveCell(0);

}else if(clickX == nullX &&clickY - nullY == -150){

nullCell.setLocation(nullX, nullY-150);

button.moveCell(1);

}else{

}

if(isFinal()){

JOptionPane.showConfirmDialog(this,"您通关了!!!\n步数:"+stepNum);

for(int i = 0;i < 4;i++){

for(int j = 0;j < 3;j++){

cell[i*3+j].removeMouseListener(this);

}

}

isAddMouserListener = false;

}

}

private boolean isFinal() {

for(int i = 0;i < 4;i++){

for(int j = 0;j < 3;j++){

int x = cell[i*3+j].getBounds().x;

int y = cell[i*3+j].getBounds().y;

if((x-20)/150+(((y-20)/150)*3) != (i*3+j)){

return false;

}

}

}

return true;

}

@Override

public void mouseReleased(MouseEvent e) {

// TODO Auto-generated method stub

}

}

4.右部预览界面

public class PicturePreview extends JPanel{

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

String filePath = "picture\\"+PictureCanvsa.pictureId+".jpg";

ImageIcon icon = new ImageIcon(filePath);

Image image = icon.getImage();

g.drawImage(image, 20, 20, 450, 600, this);

}

}

5.小方格图片

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 moveCell(int direction) {

switch (direction) {

case 0:

this.setLocation(this.getBounds().x, this.getBounds().y-150);

break;

case 1:

this.setLocation(this.getBounds().x, this.getBounds().y+150);

break;

case 2:

this.setLocation(this.getBounds().x-150, this.getBounds().y);

break;

case 3:

this.setLocation(this.getBounds().x+150, this.getBounds().y);

break;

default:

break;

}

}

}

更多推荐

拼图游戏 java swing

本文发布于:2024-03-13 18:33:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1734567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拼图游戏   java   swing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!