拼图java 源代码,Java语言实现拼图游戏源代码

/*

* JAVA小游戏-拼图 我做的第一个小游戏

* Cell类是继承的按钮类,并加上相应图形,形成方格

*MyCanvas是一个面板,加载Cell类的对象(方格),是这三个类中的核心

*/

import java.awt.Rectangle;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Choice;

import java.awt.Color;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.Icon;

import javax.swing.JButton;

public class MyMainFrame extends JFrame implements ActionListener {

MyCanvas myCanvas;

JPanel panelNorth,panelPreview;//定义上方的面板,及预览所需的面板

Button start,preview,set;//定义开始,预览,设定按钮

Container container;//容器,得到内容面板

public MyMainFrame() {//初使化

container=this.getContentPane();

start=new Button("开始");

start.addActionListener(this);

preview=new Button("预览");

preview.addActionListener(this);

set = new Button("设置");

set.addActionListener(this);

panelPreview=new JPanel();

panelPreview.setLayout(null);

Icon icon=new ImageIcon("pic_"+MyCanvas.pictureID+".jpg");

JLabel label=new JLabel(icon);

label.setBounds(0,0,300,300);

panelPreview.add(label);

panelNorth=new JPanel();

panelNorth.setBackground(Color.red);

panelNorth.add(start);

panelNorth.add(preview);

panelNorth.add(set);

myCanvas=new MyCanvas();

container.add(myCanvas,BorderLayout.CENTER);

container.add(panelNorth,BorderLayout.NORTH);

this.setTitle("拼图小游戏-明");

this.setLocation(300,200);

this.setSize(308,365);

this.setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(3);

}

public static void main(String[] args) {

// TODO 自动生成方法存根

new MyMainFrame();

}

public void actionPerformed(ActionEvent arg0) {//对三个按钮事件的处理

// TODO 自动生成方法存根

Button button=(Button)arg0.getSource();

if(button==start){

myCanvas.Start();

}else if(button==preview){

if(button.getLabel()=="预览"){

container.remove(myCanvas);

container.add(panelPreview);

panelPreview.updateUI();

container.repaint();

button.setLabel("返回");

}else{

container.remove(panelPreview);

container.add(myCanvas);

container.repaint();

button.setLabel("预览");

}

}else if(button==set){//修改所选图片

Choice pic = new Choice();

pic.add("小猫");

pic.add("小猪");

pic.add("云");

pic.add("QQ");

pic.add("卡通");

pic.add("花");

int i=JOptionPane.showConfirmDialog(this, pic, "选择图片", JOptionPane.OK_CANCEL_OPTION);

if(i==JOptionPane.YES_OPTION){

MyCanvas.pictureID=pic.getSelectedIndex()+1;

myCanvas.reLoadPictrue();

Icon icon=new ImageIcon("pic_"+MyCanvas.pictureID+".jpg");

JLabel label=new JLabel(icon);

label.setBounds(0,0,300,300);

panelPreview.removeAll();

panelPreview.add(label);

panelPreview.repaint();

}

}

}

}

class MyCanvas extends JPanel implements MouseListener {

boolean hasAddActionListener=false;//设置方格的动作监听器的标志位,TRUE为已经添加上动作事件,FALSE是尚未添加动作事件

Cell cell[];//定义方格

Rectangle cellNull;//定义空方格区域

public static int pictureID=1;//当前选择的图片代号

public MyCanvas() {

this.setLayout(null);

this.setSize(400,400);

cellNull=new Rectangle(200,200,100,100);//空方格区域在第三行每三列

cell=new Cell[9];

Icon icon;

for (int i = 0; i < 3; i++) {//为9个方格加载图片,并初使化坐标,形成三行三列

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

icon=new ImageIcon("pic_"+pictureID+"_"+(i*3+j+1)+".jpg");

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

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

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

}

}

this.remove(cell[8]);//移除最后一个多余的方格

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值