- #1
- 110
- 3
Homework Statement
My class is required to write the Java source code for a 15 Puzzle program as an end of the year assignment. http://en.wikipedia.org/wiki/15_puzzle" [Broken] is a link to what the 15 Puzzle is, for those who aren't familiar with its name.
The assignment is split into two parts; setting up the visual part of the puzzle and setting up the actions/events of the puzzle.
Currently, I'm in the first half of the puzzle. The second half will be assigned at a later date, and I will most likely end up having to post again for help :P
Homework Equations
This project requires "for" loops, and I was also told that "if" statements aren't allowed.
How much these restrictions will inhibit anyone's ability to find a helpful solution, I don't know. I myself am not familiar with "if" statements since my professor has yet to cover that part of the course.
The Attempt at a Solution
All current code:
NOTE: The first two sections of code was code that was given to us already to start us off; these aren't to be altered. The last section of code is what I have been writing.
Code:
package code;
public class Driver {
public static void main(String[] args) {
new FifteenPuzzle("images/butterflySmall.png");
}
}
Code:
package support;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class HelpfulImageMethods {
/**
* @param filePath
* @return The BufferedImage object created from the data in the file
* located at filePath; if no image data can be loaded, null
* is returned.
*/
public static BufferedImage loadImage(String filePath) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(filePath));
}
catch (IOException e) {
System.err.println("I could not load the file \'"+filePath+"'. Sorry.");
}
return img;
}
/**
* @param img
* @param sx
* @param sy
* @param imageWidth
* @param imageHeight
* @return a BufferedImage object which is cut out from the BufferedImage
* object 'img'. The returned image is the sub-image of 'img' whose
* upper-left corner is at (sx,sy) and whose width is imageWidth,
* and whose height is imageHeight.
*/
public static BufferedImage createSubImage(BufferedImage img, int sx, int sy, int imageWidth,
int imageHeight) {
BufferedImage subImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = subImage.getGraphics();
int dx = 0;
int dy = 0;
g.drawImage(img,dx, dy, dx+imageWidth, dy+imageHeight,
sx, sy, sx+imageWidth, sy+imageHeight,
null);
g.dispose();
return subImage;
}
}
This last section of code is the code that the student is supposed to write himself/herself, ie. the problem lies in this section.
Code:
package code;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import support.HelpfulImageMethods;
public class FifteenPuzzle {
private JFrame _window;
private BufferedImage _wholeImage;
public FifteenPuzzle(String filePath) {
_wholeImage = HelpfulImageMethods.loadImage(filePath);
_window = new JFrame("15 Puzzle");
Container p = _window.getContentPane();
p.setLayout(null);
p.setPreferredSize(new Dimension(300,300));
this.subImageAdder();
_window.pack();
_window.setVisible(true);
_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public List<BufferedImage> subImageCreator(){
List<BufferedImage> list = new LinkedList<BufferedImage>();
int subImageWidth = _wholeImage.getWidth()/4;
int subImageHeight = _wholeImage.getHeight()/4;
for (int col = 0; col < 4; col = col+1){
BufferedImage rowImages = HelpfulImageMethods.createSubImage(_wholeImage, 0, col*subImageHeight, subImageWidth, subImageHeight);
list.add(rowImages);
}
for (int row = 0; row < 4; row = row+1){
BufferedImage columnImages = HelpfulImageMethods.createSubImage(_wholeImage, row*subImageWidth, 0, subImageWidth, subImageHeight);
list.add(columnImages);
}
return list;
}
public void subImageAdder(){
Iterator<BufferedImage> i = this.subImageCreator().iterator();
int subImageWidth = _wholeImage.getWidth()/4;
int subImageHeight = _wholeImage.getHeight()/4;
for (int col = 0; col < 4; col = col+1){
ImageIcon icon = new ImageIcon(i.next());
JLabel label = new JLabel(icon);
label.setBounds(2, col*(2+subImageHeight), subImageWidth, subImageHeight);
_window.add(label);
}
for (int row = 0; row < 4; row = row+1){
ImageIcon icon = new ImageIcon(i.next());
JLabel label = new JLabel(icon);
label.setBounds(row*(2+subImageWidth), 2, subImageWidth, subImageHeight);
_window.add(label);
}
}
}
Here is a snapshot of what my current code results in:
So as you can see, the code's not doing what I need it to just yet. This is where I have been stumped for some time.
Also, we need to have each sub-image of the whole image 2 pixels apart from neighbouring sub-images. This explains the small spaces in-between the sub-images in the picture, except for some reason the spaces don't appear around the edges of the first (top-left most) sub-image.
Any advice or help would be greatly appreciated.
Last edited by a moderator: