急急急课综要求java用做一个拼图游戏,去网上弄个一个代码,但是运行后拼图被切割的太多了,求大神解救!
求大神指教如何改成3×34×46×6的拼图游戏,这是我所有的财富了,真的很急,明天就要交了!!这个是已经做好的云盘下载地址:http://yunpan.cn/QDhWH3...
求大神指教如何改成3×3 4×4 6×6的拼图游戏,这是我所有的财富了,真的很急,明天就要交了!!
这个是已经做好的云盘下载地址:http://yunpan.cn/QDhWH39fx5v3h
这个是网址:http://wenku.baidu.com/link?url=x1YKZkth4-T7JfTRr5tQ3887hoempC6nQiq4Z2OLWkAJOzrMZy8uZZmrfQhid3Hd9ZFpyawt0pvCt2A2w2EvnWqFZfrDElmN1ALQtsAQnX_ 展开
这个是已经做好的云盘下载地址:http://yunpan.cn/QDhWH39fx5v3h
这个是网址:http://wenku.baidu.com/link?url=x1YKZkth4-T7JfTRr5tQ3887hoempC6nQiq4Z2OLWkAJOzrMZy8uZZmrfQhid3Hd9ZFpyawt0pvCt2A2w2EvnWqFZfrDElmN1ALQtsAQnX_ 展开
2个回答
展开全部
改好了,你把固定切割像素的规则改为根据数量切割就是了。
代码如下:
核心改动算法:
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
//Singleton class
public class Split
{
public static final int HARD = 0;
public static final int NORMAL = 1;
public static final int EASY = 2;
public static final String pvo = "png";
public static int [] level = { 6 ,4 ,3}; // { 60,80 ,100};
public static int w ; //增加了当前难度的切割后的图片的像素的宽的存放
public static int h ; //增加了当前难度的切割后的图片的像素的高的存放
private static Split Ob;
private String filename;
private String path;
private Split() {}
public static Split get()
{
if (Ob == null)
Ob = new Split();
return Ob;
}
public boolean set(String fn)
{
filename = fn;
path = Arg.path + "/" + filename;
File file = new File(path);
return file.exists();
}
public BufferedImage[][] divid(int type)
{
try
{
if (filename == null)
return null;
BufferedImage image = ImageIO.read(new File(path));
int len = level[type]; //获取当前难度
// int cal = image.getWidth() / len; //原先为根据固定长度 60 80 100 切割。现在改为固定数量 6,4,3切割
// int row = image.getHeight() / len;
w = image.getWidth() / len ; //计算当前难度下的切割后的图片宽和高
h = image.getHeight() / len;
// BufferedImage [][] subimage = new BufferedImage[row][cal];
BufferedImage [][] subimage = new BufferedImage[len][len];
// for (int i = 0; i < row; i++)
// for (int j = 0; j < cal; j++)
// subimage[i][j] = image.getSubimage(j*len, i*len, len, len);
// return subimage;
for (int i = 0; i < len; i++)
for (int j = 0; j < len; j++)
subimage[i][j] = image.getSubimage(j*w, i*h, w, h);
return subimage;
}
catch (Exception e)
{ return null; }
}
}
//动态调整窗体大小的算法也要改变
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
public class Puzzle extends CardFrame {
private boolean start;
private int fWidth = this.getWidth();
private int fHeight = this.getHeight();
private GameOver gOver;
private boolean index = true;
public Puzzle() {
start = false;
ImageIcon icon = new ImageIcon("icon/OK.png");
this.setSize(200 + fWidth, 200 + fHeight);
this.setIconImage(icon.getImage());
this.setVisible(true);
}
private void startGame() {
if (start)
return;
start = true;
updateMenuBegin();
}
public void endGame() {
updateMenuBegin();
initMenuBackground();
start = false;
JOptionPane.showMessageDialog(null, "时间:" + gOver.getTime() + "s\n"
+ "步数:" + gOver.getStep());
Grades g = new Grades(this);
g.set((int) gOver.getTime(), gOver.getStep());
}
protected void FrameLostFocus() {
if (start && index) {
nextCard();
if (gOver != null)
gOver.pause();
index = false;
}
}
protected void FrameGetFocus() {
nextCard();
if (gOver != null)
gOver.pause();
index = true;
}
public void menuNewClick() {
Split sp = Split.get();
BufferedImage[][] image;
if (!sp.set(getFilename()) || (image = sp.divid(gettype())) == null) {
JOptionPane.showMessageDialog(null, "图片不存在!\n请重新选择~");
return;
}
startGame();
this.setSize(fWidth, fHeight);
this.setVisible(true);
// int len = Split.level[gettype()]; //由于这里放的不再是切割后的图片宽和高(老版本是正方形图片,宽高一样,现在的根据具体的背景图片来切割的。)
int row = image.length;
int cal = image[0].length;
gOver = new GameOver(this);
JButton[][] button = new JButton[row][cal];
Matrix matrix = new Matrix(button, panel[0], Split.w,Split.h, gOver); //改动构造函数,传入新的宽和高(原来的宽和高一样所以只需要传一个,现在不一样所以要传入两个)
matrix.init(image);
//同样修改调整窗体大小算法。
this.setSize(cal * Split.w + fWidth, row * Split.h + fHeight);
//设置窗体显示位置
int x = new Double((this.getToolkit().getScreenSize().width-this.getWidth())*0.5).intValue();
int y = new Double((this.getToolkit().getScreenSize().height-this.getHeight())*0.5).intValue();
this.setLocation(x,y);
this.setVisible(true);
}
public void menuGradesClick() {
Grades g = new Grades(this);
g.show();
}
public void menuShowClick() {
new ShowImage(getFilename());
}
public void menuExitClick() {
System.exit(0);
}
public void menuHelpClick() {
String help0 = "通过移动每一个小图片,最终拼为原本的完整图片.\n\n";
String help1 = "您可以单击空白区周围的小图片,可以使它移动到空白区。";
JOptionPane.showMessageDialog(null, help0 + help1);
}
public void menuAboutClick() {
String version = "版本: 0.0.2 beta 1\n";
String author = "作者: hjs,wj,df,ljy,ply\n";
String email = "E-mail: hjs1120@vip.qq.com";
JOptionPane.showMessageDialog(null, version + author + email);
}
public static void main(String[] argv) {
new Puzzle();
}
}
//MenuFrame 类的 构造函数里
public MenuFrame()
{
super("拼图游戏");
getContentPane().setBackground(Color.ORANGE);
addMenu();
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(widthTitle, heightTitle+heightMenu);
//窗体的起始显示位置这个最好调整下, 这个算法有问题的,最好根据屏幕的像素与当前窗体的大小来计算,放到menuNewClick()方法里。
//this.setLocation(
this.getToolkit().getScreenSize().width/3 - this.getWidth()/3,this.getToolkit().getScreenSize().height/3 - this.getHeight()/3 );
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询