Rompecabezas en java

yop2
15 de Julio del 2009
?

yop2
15 de Julio del 2009
Buenas. Estoy haciendo un programa en java para crear un rompecabezas con imagenes. He logrado cargar las imagenes y dividirla en las partes que el ususario quiera pero...estoy perdido al "barajar" las subimagenes (public void shuffle() lo he llamado) Aqui teneis el codigo por si podeis echarme una mano.

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

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;


public class ImageLoader
{
public static void main(String[] args)
{
JFrame frame = new ImageLoaderFrame();
frame.show();
}
}

class ImageLoaderFrame extends JFrame implements ActionListener
{

public ImageLoaderFrame()
{
setTitle("ImageLoader");
setSize(500, 500);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );

Container contentPane = getContentPane();
panel = new ImageLoaderPanel(this);
contentPane.add(panel, "Center");

JMenu fileMenu = new JMenu("Select Image");

openItem = new JMenuItem("Open");
openItem.addActionListener(this);
fileMenu.add(openItem);

exitItem = new JMenuItem("Exit");
exitItem.addActionListener(this);
fileMenu.add(exitItem);

JMenu fileMenu2 = new JMenu("Play Game");

InputDialog1 = new JMenuItem("Input");
InputDialog1.addActionListener(this);
fileMenu2.add(InputDialog1);

Shuffle = new JMenuItem("Shuffle");
Shuffle.addActionListener(this);
fileMenu2.add(Shuffle);

Start = new JMenuItem("Start");
Start.addActionListener(this);
fileMenu2.add(Start);

JMenu fileMenu3 = new JMenu("Help");

Credits = new JMenuItem("About ImageToy");
Credits.addActionListener(this);
fileMenu3.add(Credits);

Howtoplay = new JMenuItem("Game instructions");
Howtoplay.addActionListener(this);
fileMenu3.add(Howtoplay);

JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(fileMenu2);
menuBar.add(fileMenu3);
setJMenuBar(menuBar);
}

public void actionPerformed(ActionEvent evt)
{
Object source = evt.getSource();
if (source == openItem)
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));

chooser.setFileFilter(new
javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
String name = f.getName().toLowerCase();
return name.endsWith(".gif")
|| name.endsWith(".jpg")
|| name.endsWith(".jpeg")
|| f.isDirectory();
}

public String getDescription()
{
return "Image files";
}
});

int r = chooser.showOpenDialog(this);
if(r == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getAbsolutePath();
panel.loadImage(name);
}
}
else if (source == exitItem)
System.exit(0);
else if (source == InputDialog1)
{
System.out.println("Heres the input dialog");

String s = (String)JOptionPane.showInputDialog(frame, "Enter number of whatever:");
String t = (String)JOptionPane.showInputDialog(frame, "Enter number of whatever:");


if((s == null)&&(t == null))
{
s="3";
t="3";
JOptionPane.showMessageDialog(frame, "Set to 3,3");
}

i = Integer.valueOf(s).intValue();
j = Integer.valueOf(t).intValue();

panel.divide_image(i,j);

}

else if (source == Shuffle)
{
System.out.println("Shuffle the image!!!!");
panel.shuffle(i,j);
}

else if (source == Start)
System.out.println("Start playing!!");
else if (source == Credits)
{
JOptionPane.showMessageDialog(frame, "ImageToy 1.0 Created by Antonio Medina. 2005.");
System.out.println("By Antonio Medina");
}
else if (source == Howtoplay)
{
JOptionPane.showMessageDialog(frame, "1. Select an imagen 2. Select 'Play Game -> Input'n 3. Select 'Play Game -> Shuffle'n 4. Select 'Play Game -> Start' 5. Try and re-order the image by clicking on a sub-image adjacent to the gap to move it into the gap");

System.out.println("Play man, its easy.");
}
}

public int getSubImSizeX()
{
return i;
}

public int getSubImSizeY()
{
return j;
}

private int i;
private int j;
private ImageLoaderPanel panel;
private JMenuItem openItem;
private JMenuItem exitItem;
private JMenuItem InputDialog1;
private JMenuItem Shuffle;
private JMenuItem Start;
private JMenuItem Credits;
private JMenuItem Howtoplay;
private JFrame frame;
}


class ImageLoaderPanel extends JPanel
{
public ImageLoaderPanel(ImageLoaderFrame frame)
{
parent=frame;
int noSubImX=parent.getSubImSizeX();
int noSubImY=parent.getSubImSizeY();
}


public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (image != null)
{
g.drawImage(image, 0, 0, null);
}
}


public void loadImage(String name)
{
Image loadedImage = Toolkit.getDefaultToolkit().getImage(name);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(loadedImage, 0);
try { tracker.waitForID(0); }
catch (InterruptedException e) {}
image = new BufferedImage(loadedImage.getWidth(null),
loadedImage.getHeight(null), BufferedImage.TYPE_INT_RGB);

System.out.println(image.getWidth() + "x" + image.getHeight() + " pixels");

Graphics2D g2=image.createGraphics();
g2.drawImage(loadedImage,0,0,null);

repaint();

}

public void divide_image(int x, int y)
{
int widthsq= image.getWidth()/x;
int heightsq= image.getHeight()/y;

gridx = new int[x][y];
gridy = new int[x][y];
this.counter = 0;


for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
System.out.println("ok " + i + " " + j+" "+i*widthsq+" "+j*heightsq);
image2 = image.getSubimage(i*widthsq,j*heightsq,widthsq,heightsq);
gridx[i][j]=i*widthsq;
gridy[i][j]=j*heightsq;
counter++;

if((i==(x-1))&&(j==(y-1)))
{
Graphics g3= image.createGraphics();
//Color test= g3.getBackground();
g3.setColor(new Color(204,204,204));
g3.drawRect(i*widthsq,j*heightsq,widthsq,heightsq);
g3.fillRect(i*widthsq,j*heightsq,widthsq,heightsq);

}

}
}

repaint();


System.out.println("3 OK");

}
public void shuffle(int x, int y)
{
int counterx=0;
int countery=0;

int random_x=(int)(Math.random()*x)+1;
int random_y=(int)(Math.random()*y)+1;
// if(
System.out.println(random_x + " " + random_y);

for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{

if((random_x==gridx[i][j])&&(random_y==gridy[i][j]))
{
image2 = image2.getSubimage(gridx[i][j],gridy[i][j],widthsq,heightsq);

counterx++;
countery++;
}
}
}

repaint();
}


private ImageLoaderFrame parent;
private int widthsq, heightsq;
private BufferedImage image;
private BufferedImage image2;
private int[][] gridx;
private int[][] gridy;
private int counter;

}