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.
Eclipse.jpg
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.
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
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); } } }