java拼图源代码(拼图小程序源码)
admin 发布:2022-12-19 22:49 194
今天给各位分享java拼图源代码的知识,其中也会对拼图小程序源码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、找高手帮我翻译这个JAVA拼图游戏代码(希望能帮我解析每句中用到的JAVA技术)
- 2、1200分跪求JAVA数字拼图游戏源代码!
- 3、java做的拼图游戏怎么实现更换图片哦
- 4、请问java怎样实现拼图游戏图片的载入,就是图片怎么分割成一小张一小张的,请详讲。谢谢,最好有代码
- 5、如何用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做的拼图游戏怎么实现更换图片哦
编写拼图按钮的监听器类,该类为主类的内部类。
在actionPerformed()方法中,首先获得空白按钮和被单击按钮的所在行和列,
然后判断这两个按钮是否相邻,如果相邻则将被单击按钮显示的图片移动到空白按钮上,并令被单击按钮显示空白图片,以及将在类中声明的空白按钮对象设置为被单击的按钮对象
思路就这样,我给你个示例代码,你参考参考哦:
class ImgButtonAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String emptyName = emptyButton.getName();
char emptyRow = emptyName.charAt(0);
char emptyCol = emptyName.charAt(1);
JButton clickButton = (JButton) e.getSource();
String clickName =clickButton.getName();
char clickRow = clickName.charAt(0);
char clickCol = clickName.charAt(1);
if(Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) {
emptyButton.setIcon(clickButton.getIcon()) ;
clickButton.setIcon(new ImageIcon("img/00.jpg"));
emptyButton = clickButton;
}
}
}
大概就这样了
希望对你有帮助哈
请问java怎样实现拼图游戏图片的载入,就是图片怎么分割成一小张一小张的,请详讲。谢谢,最好有代码
public static void cut(String srcImageFile, String descDir, int destWidth,
int destHeight) {
try {
Image img;
ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getHeight(); // 源图宽度
int srcHeight = bi.getWidth(); // 源图高度
if (srcWidth destWidth srcHeight destHeight) {
Image image = bi.getScaledInstance(srcWidth, srcHeight,
Image.SCALE_DEFAULT);
destWidth = 200; // 切片宽度
destHeight = 150; // 切片高度
int cols = 0; // 切片横向数量
int rows = 0; // 切片纵向数量
// 计算切片的横向和纵向数量
if (srcWidth % destWidth == 0) {
cols = srcWidth / destWidth;
} else {
cols = (int) Math.floor(srcWidth / destWidth) + 1;
}
if (srcHeight % destHeight == 0) {
rows = srcHeight / destHeight;
} else {
rows = (int) Math.floor(srcHeight / destHeight) + 1;
}
// 循环建立切片
// 改进的想法:是否可用多线程加快切割速度
for (int i = 0; i rows; i++) {
for (int j = 0; j cols; j++) {
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(j * 200, i * 150,
destWidth, destHeight);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(),
cropFilter));
BufferedImage tag = new BufferedImage(destWidth,
destHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "JPEG", new File(descDir
+ "pre_map_" + i + "_" + j + ".jpg"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
如何用JAVA编写一个图片的拼图游戏
说说思路:
1 本身他不是一个完整的图片,应该被切分成数组,该数组为拼图成功的标准
2 将该数组复制,然后打散元素的顺序
3 初始化一个二维数组方格矩阵
4 允许用户通过鼠标事件将 备选图案中的某个元素放置到二维数组方格矩阵中,并记载其顺序
5 直到所有的方格被填满,与答案数组的元素顺序比较,给出结果
以上是实现拼图的关键思路,其他情况还要考虑,如允许从方格中撤出图像或者与其他图像调整位置,仅代表个人不成熟的想法,希望对你能有所帮助,祝你好运!
java拼图源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于拼图小程序源码、java拼图源代码的信息别忘了在本站进行查找喔。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-07pb超级报表源代码(pb报表工具)[20240507更新]
- 05-07简历源代码可以上传照片的简单介绍[20240507更新]
- 05-07广告切换源代码免费下载(广告切换源代码免费下载安装)[20240507更新]
- 05-06阁楼网源代码(阁楼是什么网站)[20240506更新]
- 05-06源代码管理资源管理器(资源管理器运行代码)[20240506更新]
- 05-06人脸识别源代码pdf的简单介绍[20240506更新]
- 05-06包含超市管理系统java源代码的词条[20240506更新]
- 05-06商城app源代码免费(商城App源码)[20240506更新]
- 05-06包含游戏源代码不同的模式的词条[20240506更新]
- 05-06matlab写的图象灰度切分源代码(matlab灰度图像分段线性变换)[20240506更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接