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 16 of 16

Thread: Java sliding block puzzle issues

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Java sliding block puzzle issues

    I'm working on this task for my university, and I've come up to an impass.

    Im trying to display in a system.out.println method to display the width and height of an image.

    So far i've gotten it to load the image correctly, but whenever I put in the code to display the width and height (Using the getWidth and getHeight methods) the compiler tells me that it "Cannot find symbol" even when I declare the getWidth and getHeight in the code, I have no idea why this is, can anyone help me.

    import java.awt.image.*;
    import javax.imageio.*;
    import java.io.File;
     
    class SlidingBlockModel
    {
    	BufferedImage original_image; // the image to be cut up into blocks
    	int numCols;  // number of columns in the grid of blocks
    	int numRows;  // number of rows in the grid of blocks
    	int empty_col; // holds the column index of the one empty grid element
    	int empty_row; // holds the row index of the one empty grid element
    	BufferedImage[][] puzzle; // the two '[][]' allows a 2-D array
                   				  // to be created - each element is an image
     
     
    	public static void main(String[] args)
    		{
    			SlidingBlockModel myModel;
     
    			myModel = new SlidingBlockModel(5, 4, "Horseman-War.jpg");
     
    		}
     
     
     
    	SlidingBlockModel(int nc, int nr, String image_filename )
    		{
    			numCols = nc;
    			numRows = nr;
     
    			original_image = null;
     
    			try
     
    			{
    				original_image = ImageIO.read(new File(image_filename));
    				System.out.println("image " + image_filename + " loaded in ok.");
    			}
     
     
    			catch (Exception e)
     
    			{
    				System.out.println("Sorry - couldn't load in file " + image_filename);
     
    			}
     
    		System.out.println("Width of the window " +w);
    		System.out.println("Height of the window " +h);
     
    		}
     
    }

    Here is the error message:

    C:\Users\Vesper\Desktop\Practical Programming - CI2250\Workshop 5\Task 2\SlidingBlockModel.java:48: cannot find symbol
    symbol : variable w
    location: class SlidingBlockModel
    System.out.println("Width of the window " +w);
    ^
    C:\Users\Vesper\Desktop\Practical Programming - CI2250\Workshop 5\Task 2\SlidingBlockModel.java:49: cannot find symbol
    symbol : variable h
    location: class SlidingBlockModel
    System.out.println("Height of the window " +h);
    ^
    2 errors

    Tool completed with exit code 1


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java sliding block puzzle issues

    The error is pretty self-explanatory: what are w and h? Where are you declaring them?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java sliding block puzzle issues

    I declared them before using :

    int w = getWidth();
    int h = getHeight();

    I put this in the class, in the constructor, everywhere but it gave me the same error saying that it couldn't find them, even though they were in the code.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java sliding block puzzle issues

    Sorry, but they are not in the code you posted. That's what's causing the Exception. I don't really know what to tell you if I'm not looking at the same code as you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Java sliding block puzzle issues

    import java.awt.image.*;
    import javax.imageio.*;
    import java.io.File;
     
     
    class SlidingBlockModel
    {
    	BufferedImage original_image; // the image to be cut up into blocks
    	int numCols;  // number of columns in the grid of blocks
    	int numRows;  // number of rows in the grid of blocks
    	int empty_col; // holds the column index of the one empty grid element
    	int empty_row; // holds the row index of the one empty grid element
    	BufferedImage[][] puzzle; // the two '[][]' allows a 2-D array
                   				  // to be created - each element is an image
     
     
    	public static void main(String[] args)
    		{
    			SlidingBlockModel myModel;
     
    			myModel = new SlidingBlockModel(5, 4, "Horseman-War.jpg");
     
    		}
     
     
     
    	SlidingBlockModel(int nc, int nr, String image_filename )
    		{
    			numCols = nc;
    			numRows = nr;
     
    			int w = getWidth();
    			int h = getHeight();
     
    			original_image = null;
     
     
    			try
     
    			{
    				original_image = ImageIO.read(new File(image_filename));
    				System.out.println("image " + image_filename + " loaded in ok.");
    			}
     
     
    			catch (Exception e)
     
    			{
    				System.out.println("Sorry - couldn't load in file " + image_filename);
     
    			}
     
    		System.out.println("Width of the window " +w);
    		System.out.println("Height of the window " +h);
     
    		}
     
    }

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java sliding block puzzle issues

    Okay, so now what error are you getting?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java sliding block puzzle issues

    C:\Users\Vesper\Desktop\Practical Programming - CI2250\Workshop 5\Task 2\SlidingBlockModel.java:32: cannot find symbol
    symbol : method getWidth()
    location: class SlidingBlockModel
    int w = getWidth();
    ^
    C:\Users\Vesper\Desktop\Practical Programming - CI2250\Workshop 5\Task 2\SlidingBlockModel.java:33: cannot find symbol
    symbol : method getHeight()
    location: class SlidingBlockModel
    int h = getHeight();
    ^
    2 errors

    Tool completed with exit code 1

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java sliding block puzzle issues

    That's a different exception from before. Now it's telling you that it can't find getWidth() or getHeight(). That makes sense, because neither method is declared in your SlidingBlockModel.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java sliding block puzzle issues

    SlidingBlockModel(int nc, int nr, String image_filename )
    {
    numCols = nc;
    numRows = nr;

    int w = getWidth();
    int h = getHeight();


    original_image = null;



    Pretty sure I declared it....

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java sliding block puzzle issues

    No, you didn't. You're calling it. But where is it declared? What methods do you expect to be called when the code reaches that line?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java sliding block puzzle issues

    But how do I declare it? and where would I declare it, everything i've tried doesn't seem to work.

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java sliding block puzzle issues

    Quote Originally Posted by Vesper View Post
    But how do I declare it? and where would I declare it, everything i've tried doesn't seem to work.
    Recommended reading: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java sliding block puzzle issues

    That still tells me nothing, it's trying to find getWidth and getHeight which are part of the Java library, this is what im finding confusing. It's not finding something that i've already imported from and as such my code isn't working.

  14. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java sliding block puzzle issues

    Quote Originally Posted by Vesper View Post
    That still tells me nothing, it's trying to find getWidth and getHeight which are part of the Java library, this is what im finding confusing. It's not finding something that i've already imported from and as such my code isn't working.
    Calm down. That tutorial tells you exactly how to declare a method.

    If you want to call a method that's in another class, you need an instance of that class. What class's getWidth() and getHeight() are you trying to call?

    Had you read the rest of the tutorial instead of insisting that it tells you nothing, you would have read this: Using Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java sliding block puzzle issues

    Problem fixed, everything you linked me was completely opposite of the fix that was found.

  16. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java sliding block puzzle issues

    Quote Originally Posted by Vesper View Post
    Problem fixed, everything you linked me was completely opposite of the fix that was found.
    Awesome attitude. Your problem was that you were trying to call a method that hadn't been declared, which probably meant you needed to be using an instance of some class that did contain that method. The tutorials I linked you to explained exactly this.

    Good luck getting help in the future.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Search Word puzzle game
    By lew1s in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 9th, 2011, 04:23 AM
  2. JavaScript pic puzzle
    By fr334a11 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2011, 08:29 AM
  3. Help to write Kakuro puzzle solver
    By divadola in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 11:10 AM
  4. Creating Block cipher without the use of inbuilt java funcs
    By fortune2k in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 15th, 2010, 09:43 AM
  5. [SOLVED] Problem in implementation of Fifteen Puzzle with 2D Arrays
    By bruint in forum Collections and Generics
    Replies: 8
    Last Post: May 3rd, 2009, 10:37 PM

Tags for this Thread