Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Need Help Understanding Where I went wrong

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    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);
    			}	
    		}
    }
    Last edited by basedoverlord12; November 30th, 2011 at 09:44 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Need Help Understanding Where I went wrong

    How do execute your code? I don't see a main method.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need Help Understanding Where I went wrong

    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.

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    package code;
     
    public class Driver {
    	public static void main(String[] args) {
    		new FifteenPuzzle("images/butterflySmall.png");
    	}
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default 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?

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Need Help Understanding Where I went wrong

    Who wrote the subImageAdder() method?
    Can you ask them how the code works?

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default 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.

  10. #10
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help Understanding Where I went wrong

    Thanks for time and advice, I realized how to get it to work.

Similar Threads

  1. Help understanding this syntax -- new object returned
    By wy125 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 5th, 2011, 01:18 PM
  2. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 02:55 PM
  3. [SOLVED] Help with understanding the following commented codes!
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 2nd, 2011, 05:26 PM
  4. Need help understanding method calls and such
    By hackman2007 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 14th, 2010, 09:18 AM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 11:18 AM