网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

44的拼图代码java的简单介绍

找高手帮我翻译这个JAVA拼图游戏代码(希望能帮我解析每句中用到的JAVA技术)

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;

//以上均引用不同的package内的类

public class MyMainFrame extends JFrame implements ActionListener {//MyMainFrame类extends “JFrame”类实现 ActionListener的方法

MyCanvas myCanvas; //初始化对象MyCanvas类

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

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

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

public MyMainFrame() {//初使化

container=this.getContentPane(); //得到当前对象的ContentPane,并且把它赋给container

start=new Button("开始");//创建并初始新的Button(按钮)对象,赋给start

start.addActionListener(this); //在这个按钮对象中添加监听器,范围是当前对象

preview=new Button("预览");//同上,创建新的Button对象。。。。。

preview.addActionListener(this);//同上。。。。。

set = new Button("设置"); //同上。。。。。(感觉代码都差不多吧?呵呵)

set.addActionListener(this);//同上

panelPreview=new JPanel(); 创建新的JPanel(面板)对象

panelPreview.setLayout(null); //设置面板对象的布局为空

Icon icon=new ImageIcon("pic/pic_"+MyCanvas.pictureID+".jpg"); //创建并初始新的图标对象。图标的图片路径是pic目录下的pic与通过MyCanvas.pictureId取得字符串再与.jpg合并后的名称。例如(pic/pic_1234.jsp)

JLabel label=new JLabel(icon); //定义新的JLable(java标签),并初始

label.setBounds(0,0,300,300); //设置标签的范围(长x轴,宽y轴,长多,宽多少)

panelPreview.add(label); //面板对象中添加label这个对象

panelNorth=new JPanel(); //定义新的JPanel

panelNorth.setBackground(Color.red); //设置JPanel的背景色

panelNorth.add(start); //Jpanel加入按钮

panelNorth.add(preview); //同上

panelNorth.add(set); //同上

myCanvas=new MyCanvas(); //实例化MyCanvas

container.add(myCanvas,BorderLayout.CENTER);//在容器(前边定义好了这个对象)中添加myCanvas,设置它的布局为居中

container.add(panelNorth,BorderLayout.NORTH);//添加Jpanel,布局为北(也就是上)

this.setTitle("拼图小游戏-"); //设置这个对象的题目叫。。。。。

this.setLocation(300,200); //设置它的初始位置

this.setSize(308,365); //设置大小

this.setResizable(false); //设置是否可以改变窗口的大小(否)

this.setVisible(true); //是否可以显示(是)

this.setDefaultCloseOperation(3); //(swt和swing本人用的少)这个好像是按钮的初始样式是哪种吧。自己改下

}

public static void main(String[] args) {//类进口,也就是主程序的进口

// TODO 自动生成方法存根

new MyMainFrame(); //实例化主框架

}

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

// TODO 自动生成方法存根

Button button=(Button)arg0.getSource(); //取得通过监听得到的动作时间对象的源(getSource具体得到的是啥,我也不太清楚)

if(button==start){ //判断。如果监听到的是按start按钮

myCanvas.Start(); //启动myCanvas

}else if(button==preview){ //如果是preview按钮

if(button.getLabel()=="预览"){ //如果按钮的标签是"预览"

container.remove(myCanvas); //容器销毁myCanvas

container.add(panelPreview); //然后容器添加panelPreview对象

panelPreview.updateUI(); //panelPreview对象的upDateUI方法

container.repaint(); //调用容器重新画图的命令

button.setLabel("返回"); //标签设置成"返回"

}else{ //如果以上两个if都不是,执行下边的语句

container.remove(panelPreview); //cantainer销毁p....这个对象

container.add(myCanvas); //添加m...这个对象

container.repaint(); //容器重新画图

button.setLabel("预览"); //设置标签名称为"预览"

}

}else if(button==set){//修改所选图片 //如果间听到的是按set这个键的时候

Choice pic = new Choice(); //Choice实例化对象(这个是啥类,我也不知道,名字上看是选择?)

pic.add("小猫"); //添加小猫

pic.add("小猪"); //添加小猪

pic.add("云"); //添加云

pic.add("QQ"); //添加qq

pic.add("卡通"); //添加卡通

pic.add("花"); //花

int i=JOptionPane.showConfirmDialog(this, pic, "选择图片", JOptionPane.OK_CANCEL_OPTION);//定义一个int类型的对象i,赋值成后边的那些

if(i==JOptionPane.YES_OPTION){ //如果对象i与JOptionPane对象的YES...相等,则执行下列代码

MyCanvas.pictureID=pic.getSelectedIndex()+1; //MyC....这个类的pic...这个属性等于pic.get......这个方法的结果+1

myCanvas.reLoadPictrue(); //myCanvas对象重新读取图片

Icon icon=new ImageIcon("pic/pic_"+MyCanvas.pictureID+".jpg"); //定义新的图标对象

JLabel label=new JLabel(icon); //初始新的标签(标签中加入图标)

label.setBounds(0,0,300,300); //标签设置范围

panelPreview.removeAll(); //调用pane....对象的remo...方法

panelPreview.add(label); //对象pan...调用add方法

panelPreview.repaint(); //调用。。。。对象重新画的方法

}

}

}

}

------------------------

不太熟悉java的swt和swing,自己改下吧

1200分跪求JAVA数字拼图游戏源代码!

1:

import java.io.IOException;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.UnsupportedAudioFileException;

// 华容道原理的拼图游戏。 利用轻组建的套用。

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;

public class MyMainFrame extends JFrame implements ActionListener {

/**

*

*/

private static final long serialVersionUID = 1L;

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 ("images/pic_"+MyCanvas.pictureID+".jpg");

JLabel label=new JLabel(icon);

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

panelPreview.add(label);

panelNorth=new JPanel();

panelNorth.setBackground(Color.yellow);

panelNorth.add(start);

panelNorth.add(preview);

panelNorth.add(set);

myCanvas=new MyCanvas();

container.add(myCanvas,BorderLayout.CENTER);

container.add(panelNorth,BorderLayout.NORTH);

this.setTitle("成型拼图小游戏-1212");

this.setLocation(300,200);

this.setSize(408,465);

this.setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(3);

} //end of 初始化 构造函数

public void actionPerformed(ActionEvent e) {

Button button=(Button)e.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("QQ");

pic.add("美女");

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

//使用选择对话框来进行选择图片。

if(i==JOptionPane.YES_OPTION){

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

myCanvas.reLoadPictrue();

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

JLabel label=new JLabel(icon);

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

panelPreview.removeAll();

panelPreview.add(label);

panelPreview.repaint();

}

}

}

public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException

{

new MyMainFrame();

} //end of main

}

2:

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;

public class MyCanvas extends JPanel implements MouseListener

{

/**

*

*/

private static final long serialVersionUID = 1L;

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

Cell cell[];//定义方格

Rectangle cellNull;//定义空方格区域 是一个矩形类

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

public MyCanvas() {

this.setLayout(null);

this.setSize(400,400);

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

cell=new Cell[16];

Icon icon;

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

for(int j=0;j4;j++){

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

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

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

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

}

}

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

} //放置9张小图片并且移调最后一张

public void reLoadPictrue(){//当选择其它图形进行拼图时,需重新加载新图片

Icon icon;

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

for(int j=0;j4;j++){

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

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

}

}

}

public boolean isFinish(){//判断是否拼合成功

for(int i=0;i15;i++)

{ int x=cell[i].getBounds().x;

int y=cell[i].getBounds().y;

if(y/100*4+x/100!=i)

return false;

} //end of for

return true;

}

public void Start(){//对方格进行重新排列,打乱顺序

while(cell[0].getBounds().x=100cell[0].getBounds().y=100){//当第一个方格距左上角较近时

int x=cellNull.getBounds().x;

int y=cellNull.getBounds().y;

int direction=(int)(Math.random()*4);//产生0-4,对应空方格的上下左右移动

if(direction==0){//空方格左移动,与左侧方格互换位置,左侧方格右移动

x-=100;

if(test(x,y)){

for(int j=0;j15;j++){

if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){//依次寻找左侧的按钮

cell[j].move("RIGHT",100);

cellNull.setLocation(x,y);

break;//找到后跳出for循环

}

}

}

}else if(direction==1){//RIGHT

x+=100;

if(test(x,y)){

for(int j=0;j15;j++){

if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){

cell[j].move("LEFT",100);

cellNull.setLocation(x,y);

break;

}

}

}

}else if(direction==2){//UP

y-=100;

if(test(x,y)){

for(int j=0;j15;j++){

if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){

cell[j].move("DOWN",100);

cellNull.setLocation(x,y);

break;

}

}

}

}else{//DOWN

y+=100;

if(test(x,y)){

for(int j=0;j15;j++){

if((cell[j].getBounds().x==x)(cell[j].getBounds().y==y)){

cell[j].move("UP",100);

cellNull.setLocation(x,y);

break;

}

}

}

}

}

if(!hasAddActionListener)//如果尚未添加动作事件,则添加

for(int i=0;i15;i++)//为第个方格添加动作事件,这样单击按钮就能移动了

cell[i].addMouseListener(this);

hasAddActionListener=true;

}

private boolean test(int x,int y){

if((x=0x=200)||(y=0y=200))

return true;

else

return false;

}

public void mouseClicked(MouseEvent e) { }

public void mouseEntered(MouseEvent e) { }

public void mouseExited(MouseEvent e) { }

public void mouseReleased(MouseEvent e) { }

public void mousePressed(MouseEvent e) {

//方格的鼠标事件,因为用到了MyCanvas中的一些方法,因此没有在Cell类中处理鼠标事件

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

int x1=button.getBounds().x;//得到所单击方格的坐标

int y1=button.getBounds().y;

int x2=cellNull.getBounds().x;//得到空方格的坐标

int y2=cellNull.getBounds().y;

if(x1==x2y1-y2==100)//进行比较,如果满足条件则进行交换

button.move("UP",100);

else if(x1==x2y1-y2==-100)

button.move("DOWN",100);

else if(x1-x2==100y1==y2)

button.move("LEFT",100);

else if(x1-x2==-100y1==y2)

button.move("RIGHT",100);

else

return;//不满足就不进行任何处理

cellNull.setLocation(x1,y1);

this.repaint();

if(this.isFinish()){//进行是否完成的判断

JOptionPane.showMessageDialog(this,"景锋恭喜你完成拼图,加油!想继续下一关么?");

for(int i=0;i15;i++)

cell[i].removeMouseListener(this);//如果已完成,撤消鼠标事件,鼠标单击方格不在起作用

hasAddActionListener=false;

}

}

}

3:

import javax.swing.Icon;

import javax.swing.JButton;

public class Cell extends JButton {

/**

*

*/

private static final long serialVersionUID = 1L;

Cell(Icon icon){//实际为ICON

super(icon);

this.setSize(100,100);

}

public void move(String direction,int sleep){//方格的移动

if(direction=="UP"){

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

}else if(direction=="DOWN"){

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

}else if(direction=="LEFT"){

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

}else{

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

}

}

}

急求用JAVA编写的图形化界面拼图小游戏代码!

个人见解,总体需要两个二维数组(一个存储正确图片排列 Array1 String[][],一个随机生成图片排列Array2 String[][]),一个一维数组来存储图片的名称Array3 String[],。

(1)如何实现图片移动

使用带图片的按钮(button =new button(getImage(Array[2][4]))),然后通过单击事件来更改按钮的图片来源。 把被点击的按钮的图片路径更新到空白按钮,并且把被点击的按钮图片更新的成空白。其实就是变换两个的二维数组成员的值。更新Array2中的值,然后重绘按钮

如 Array[2][3]=“3.image”

Array[2][4]=“”

图片3.image右移

Array[2][3]=“”

Array[2][4]=“3.image”

(2)如何判断被单击的网格与空白的网格是否相邻

后台使用一个二维数组Array2来做映射。通过二维数组的下标来判断,如Array[2][3]可以知道Array[2][4]是它右边的那个。

(3)如何实现图片的随机摆放

比如有9个图片,你可以命名1-9,然后初始化一个长度为9的一维String 数组Array3来存储图片的名称,

使用随机函数给二维数组Array2赋值,如Array2[2][3]=Array3[random()],这里要判断这个图片是否已被使用过,可以通过遍历Array2来确定当前Array3这个值是否已经在Array2中了

最后通过Array1 和Array2来比较,用户的拼图是否正确。

语言组织能力有限。讲不太清楚。


当前题目:44的拼图代码java的简单介绍
网站网址:http://bjjierui.cn/article/ddsogod.html

其他资讯