1 Attachment(s)
Need Help Understanding Where I went wrong
Hello guys I'm a beginner and very new to programming. I am working on an assignment but when I run my code I get the wrong image. My goal is to break the image up into 16 pieces with a 2 pixel wide gap between them. This is what I'm currently getting when I run the code.
Attachment 877
I wrote for loops to create the broken up images and also to add them. I think the loops I have should work because they seem to make sense to me. I am gradually getting more confused as to whether my subimage is not being fully created because there is a problem in my subImageCreator method or whether it is being created but not added to the image because there is an issue with my subImageAdder method. In my subImageCreator method the loops I have I think should add an image to every column and row. This is what I am most unsure about along with my subImageAdder method. Any help would be appreciated
This HelpfulImageMethods was given to help.
Code Java:
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 is where I started writing my code
Code Java:
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 support.HelpfulImageMethods;
public class FifteenPuzzle {
private JFrame _window;
private BufferedImage _image;
public FifteenPuzzle(String filePath) {
_image = HelpfulImageMethods.loadImage(filePath);
_window = new JFrame("15 Puzzle");
Container p = _window.getContentPane();
p.setLayout(null);
p.setPreferredSize(new Dimension(310,310));
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 = _image.getWidth()/4;
int subImageHeight = _image.getHeight()/4;
for (int col = 1; col < 4; col++){
BufferedImage rowImages = HelpfulImageMethods.createSubImage(_image, 0, col*subImageHeight, subImageWidth, subImageHeight);
list.add(rowImages);
}
for (int row = 0; row < 4; row++){
BufferedImage columnImages = HelpfulImageMethods.createSubImage(_image, row*subImageWidth, 0, subImageWidth, subImageHeight);
list.add(columnImages);
}
return list;
}
public void subImageAdder(){
Iterator<BufferedImage> i = this.subImageCreator().iterator();
int subImageWidth = _image.getWidth()/4;
int subImageHeight = _image.getHeight()/4;
for (int col = 1; col < 4; col++){
ImageIcon icon = new ImageIcon(i.next());
JLabel label = new JLabel(icon);
label.setBounds(2, 2+col*(2+subImageHeight), subImageWidth, subImageHeight);
_window.add(label);
}
for (int row = 0; row < 4; row++){
ImageIcon icon = new ImageIcon(i.next());
JLabel label = new JLabel(icon);
label.setBounds(2+row*(2+subImageWidth), 2, subImageWidth, subImageHeight);
_window.add(label);
}
}
}
Re: Need Help Understanding Where I went wrong
How do execute your code? I don't see a main method.
Re: Need Help Understanding Where I went wrong
Quote:
am working on an assignment but when I run my code I get the wrong image.
What do you mean by wrong image?
And as said by Norm, we are unable to compile and run your code to see the actual flow.
Re: Need Help Understanding Where I went wrong
This Driver class was included in the code included, sorry for not posting everything before and by wrong image I mean the picture I get is incomplete. It only has 1 row and column.
Code Java:
package code;
public class Driver {
public static void main(String[] args) {
new FifteenPuzzle("images/butterflySmall.png");
}
}
Re: Need Help Understanding Where I went wrong
Can you explain how your program works to generate the sub images.
Does it start at the top right and move down to the bottom and then move left one column and move down again?
How is it supposed to work?
Re: Need Help Understanding Where I went wrong
I am honestly unsure as to how it creates the subimages and the order of it. I was told by my teaching assistant I would need for loops to break the image into parts. I assumed when writing my loop that from each col=1 to 4 there would be an image created and the same for rows. I thought by doing this it would create a 4x4 image. I guess this is wrong though
Re: Need Help Understanding Where I went wrong
Who wrote the subImageAdder() method?
Can you ask them how the code works?
Re: Need Help Understanding Where I went wrong
Most of that method I got helped with by my instructor. He told me an iterator was needed and explain/showed what it does. In the instructions for creating the image it said " To display an image on a JLabel you must wrap it in an ImageIcon. To do this, take a BufferedImage and pass it as an argument to the ImageIcon constructor."
So the iterator I thought held the buffered image and from my instructions I passed the iterator as the argument to the imageicon. The (i.next()) so each column and row would display the next image of the list. Then added the icon to the jlabel and the label to the window. The for loops I have in this method I just took from my subImageCreator method because I thought it would do the same thing.
Re: Need Help Understanding Where I went wrong
You need to look at where you are placing the imaged labels.
Take a piece of paper and mark it as a grid and write down in each grid the x,y values for the imaged label that is to go in that square of the grid.
Then look at your code to see if you create and place one imaged label in each grid square.
Re: Need Help Understanding Where I went wrong
Thanks for time and advice, I realized how to get it to work.