Results 1 to 16 of 16
  1. #1
    PmRg is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Problem Compiling

    Hi, there I get this error while compiling my code the problem is in the main method, last line when it tries to check the 2 int's myModel.getImageRect(9,9);
    this is to check if the model size is good for my image size.

    Java Code:
    import java.awt.image.*;
    import javax.imageio.*;
    import java.io.File;
    import javax.swing.*;
    
    public 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
    	 SlidingBlockModel myModel;
    				
    	public static void main(String[] args)
    			{
    				
    				SlidingBlockModel myModel;
    				myModel = new SlidingBlockModel(5, 4, "KTM.jpg");
    				
    				myModel.getImageRect(9,9);
    				
    			}
    		
    	public SlidingBlockModel (int input_numCols, int input_numRows, String image_filename)
    			{
    				numCols = input_numCols;
    				numRows = input_numRows;
    				
    				BufferedImage original_image = null;
    			
    				try
    					{
    						original_image = ImageIO.read(new File(image_filename));
    						System.out.println("image " + image_filename + " loaded in ok." );
    						System.out.println("High is " + original_image.getHeight() + " and width is " + original_image.getWidth());
    					}
    			   catch (Exception e)
    					{
    						System.out.println("Sorry - couldn't load in file " + image_filename);
    					}
    			}
    			
    
    					
    	public BufferedImage getImageRect(int thisCol, int thisRow)
    			{
    					int w = original_image.getWidth()/numCols;
    					int h = original_image.getHeight()/numRows;
    					int x = thisCol*w;
    					int y = thisRow*h;
    			
    		    		return original_image;  
    	
    			}
    			
    	public BufferedImage getSubimage(int x, int y,int w,int h)
    			 {
    				return original_image;
    		     }
    
    			
    			
    }
    this compiles but gives me an error when I RUN

    image KTM.jpg loaded in ok.
    High is 214 and width is 319
    Exception in thread "main" java.lang.NullPointerException
    at SlidingBlockModel.getImageRect(SlidingBlockModel.j ava:51)
    at SlidingBlockModel.main(SlidingBlockModel.java:24)


    Thanks
    Pedro

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You actually don't have a compilation problem as your code likely compiles fine. Rather you have a NPE thrown when you run the code. The reason for this is because your code has two original_image BufferedImage variables, one declared in the class, which is null, and one declared in the constructor which is not null. When you call the getImageRect() method, your code tries to call methods on the null original_image, and this causes the NPE to be thrown. A fix for this is to not re-declare this variable in the constructor but rather use the class instance variable in the constructor.

  3. #3
    PmRg is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    ok greattt thanks! now I took //BufferedImage original_image = null; OFF and it runs with no error still not sure if its supposed to work like this because my instructions are saying:

    You can test your getImageRect method from within your main method, to some extent. Call the method, using the name of the object you have created (e.g. myModel.getRect(...). Use two suitable integer numbers as parameters. This method will return a BufferedImage. Print out the width and height of the image and check that these values make sense, given the size of the original image and the number of rows and columns you have used to make the SlidingBlockModel object.

    how do I call the new image width and high ? do I use the int w and int h ?

    thanks again

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Let's see your latest incarnation of your code, so we can see exactly what changes you've made. If you could fix your indentation so that it's consistent, it would be easier for us to read. Usually you don't indent the curly braces themselves but rather the code within the curly brace block.

  5. #5
    PmRg is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    last code compiling and running
    Java Code:
    import java.awt.image.*;
    import javax.imageio.*;
    import java.io.File;
    import javax.swing.*;
    
    public 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, "KTM.jpg");
    				
    				myModel.getImageRect(4,2);
    				
    			}
    		
    	public SlidingBlockModel (int input_numCols, int input_numRows, String image_filename)
    			{
    				numCols = input_numCols;
    				numRows = input_numRows;
    				
    				//BufferedImage original_image = null;
    			
    				try
    					{
    						original_image = ImageIO.read(new File(image_filename));
    						System.out.println("image " + image_filename + " loaded in ok." );
    						System.out.println("High is " + original_image.getHeight() + " and width is " + original_image.getWidth());
    					}
    			   catch (Exception e)
    					{
    						System.out.println("Sorry - couldn't load in file " + image_filename);
    					}
    			}
    			
    
    					
    	public BufferedImage getImageRect(int thisCol, int thisRow)
    			{
    					int w = original_image.getWidth()/numCols;
    					int h = original_image.getHeight()/numRows;
    					int x = thisCol*w;
    					int y = thisRow*h;
    						System.out.println("High is " + h + " and width is " + w);
    		    		return original_image.getSubimage(x,y,w,h);
    	
    			}
    			
    	
    
    			
    			
    }
    Running now, but it depends in wich value I have in myModel.getImageRect(4,2);

    if I change this it will give a run error.

    Thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This seems to work for me:

    Java Code:
       public static void main(String[] args) {
          SlidingBlockModel myModel;
          int numCols = 5;
          int numRows = 4;
          myModel = new SlidingBlockModel(numCols, numRows, "KTM.jpg");
          for (int col = 0; col < numCols; col++) {
             for (int row = 0; row < numRows; row++) {
                myModel.getImageRect(col, row);
             }
          }
          //myModel.getImageRect(4, 3);
       }

  7. #7
    PmRg is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    ok thanks! this actually works.

    but, depends the numbers I put in, with some numbers there is a run error, I guess thats when the numbers wont be fit to my image size?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by PmRg View Post
    ok thanks! this actually works.

    but, depends the numbers I put in, with some numbers there is a run error, I guess thats when the numbers wont be fit to my image size?
    Hard to say -- you never tell us the offending numbers!

  9. #9
    PmRg is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    try 6,6 7,6 5,5

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by PmRg View Post
    try 6,6 7,6 5,5
    Sure, makes sense that it will fail if any of the numbers are at or above the max row and column numbers that you use to constructor your SlidingBlockModel class. If you think on this a bit, you'll see why.

  11. #11
    PmRg is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    ahhhh GOOD GOOD, thanks a lot for your help, really appreciate it :D

    Also I think I managed to do my next task its compiling :D although can't really test it running no errors hope it's ok, since you have more experience can you just scan trough it?

    Java Code:
    import java.awt.image.*;
    import javax.imageio.*;
    import java.io.File;
    import javax.swing.*;
    
    public 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;
         			int numCols = 5;
         			int numRows = 4;
         			myModel = new SlidingBlockModel(numCols, numRows, "KTM.jpg");
        			  for (int col = 0; col < numCols; col++) {
            			 for (int row = 0; row < numRows; row++) {
               				 myModel.getImageRect(col, row);
            					 }
         		 }
    				
    				myModel.getImageRect(4,2);
    				
    			}
    		
    	public SlidingBlockModel (int input_numCols, int input_numRows, String image_filename)
    			{
    				numCols = input_numCols;
    				numRows = input_numRows;
    				
    				//BufferedImage original_image = null;
    			
    				try
    					{
    						original_image = ImageIO.read(new File(image_filename));
    						System.out.println("image " + image_filename + " loaded in ok." );
    						System.out.println("High is " + original_image.getHeight() + " and width is " + original_image.getWidth());
    					}
    			   catch (Exception e)
    					{
    						System.out.println("Sorry - couldn't load in file " + image_filename);
    					}
    				
    			puzzle = new BufferedImage[numCols][numRows];
    				for (int i = 0;i<numCols;i++)
    				{
    			 		for(int j = 0;j<numRows;j++)
    			 		{
    			 			puzzle[i][j] = getImageRect(i,j);
    			 		}
    			 	}
    			 	
    			 	empty_row = 0;
                	empty_col = 0;
                    puzzle[empty_col][empty_row] = null;
    			 	
    			}
    			
    
    					
    	public BufferedImage getImageRect(int thisCol, int thisRow)
    			{
    					int w = original_image.getWidth()/numCols;
    					int h = original_image.getHeight()/numRows;
    					int x = thisCol*w;
    					int y = thisRow*h;
    						System.out.println("High is " + h + " and width is " + w);
    		    		return original_image.getSubimage(x,y,w,h);
    	
    			}
    	
    	public BufferedImage getSubimage(int thisCol, int thisRow)
    			{
    					if ((thisCol < 0) || (thisCol >= numCols))
    					{
    						return original_image;
    					}
    
    					if ((thisRow < 0) || (thisRow >= numRows))
    					{
    						return original_image;
    					}
    
    					return puzzle[thisCol][thisRow];
    			
    			}
    					
    }
    forgot to mention this will cut the original image to make it a puzzle.

    thanks
    Last edited by PmRg; 03-13-2011 at 05:19 PM.

  12. #12
    PmRg is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Question

    Now I need some help with the drawImage its always giving me erros :S

    Java Code:
    	Rectangle getRect(int thisCol, int thisRow)
    	{
    		// if input is out of range, return "null"
    		if(thisCol <0 || thisRow < 0)
    			return null;
    		if(thisCol>=numCols || thisRow>=numRows)
    			return null;
    
    		// otherwise, make and return the Rectangle
    		int w = getWidth()/numCols;
    		int h = getHeight()/numRows;
    
    		int x = thisCol*w;
    		int y = thisRow*h;
    
    		Rectangle myRect = new Rectangle(x,y,w,h);
    		return myRect;
    	}
    
    	public void paint(Graphics g)
    	{
    		g.setColor(Color.gray);
    		g.fillRect(0,0,getWidth(), getHeight());
    		g.setColor(Color.black);
    
    
    		Graphics2D g2 = (Graphics2D)g;
    		// we'll use Graphics2D for it's "draw" method -
    		// neater than the Graphics "drawRect" suppled
    		// (which you could also use)
    
    		for (int i = 0;i<numCols;i++)
    		{
    			for(int j = 0;j<numRows;j++)
    			{
    				Rectangle r = getRect(i,j);
    				BufferedImage bi = myModel.getSubimage(i,j); 
    				g2.draw(bi, int x, int y, int w, int h); //// !??!?!? help
    			}
    		}
    	}
    	
    	
    }
    I think the problem is in the 1st parameter image img?, then I need to use the rectangle r int's and image observer is null.

    thnaks

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You have to post the error messages and indicate which lines cause them. It's the weekend and our mind-reading abilities are deficient.

  14. #14
    PmRg is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    import java.awt.image.*;
    
    class SlidingBlockPanel extends JPanel implements MouseListener
    {
    	int numCols;
    	int numRows;
    	SlidingBlockModel myModel;
    
    	public static void main(String[] args)
    	{
    		SlidingBlockFrame w = new SlidingBlockFrame();
    		w.setVisible(true);
    	}
    
    
    	public SlidingBlockPanel(int nc, int nr)
    	{
    		numCols = nc;
    		numRows = nr;
    		myModel = new SlidingBlockModel(numCols, numRows, "KTM.jpg");
    		addMouseListener(this);
    	}
    	int getCol(int x)
    	{
    		return x*numCols/getWidth();
    	}
    	int getRow(int y)
    	{
    		return y*numRows/getHeight();
    	}
    
    	public void mouseReleased(MouseEvent event)
    	{
    	}
    	public void mousePressed(MouseEvent event)
    	{
    	}
    	public void mouseClicked(MouseEvent event)
    	{
    		int thisCol = getCol(event.getX());
    		System.out.println
    		("you clicked in column " + thisCol);
    
    	}
    	public void mouseEntered(MouseEvent event)
    	{
    	}
    	public void mouseExited(MouseEvent event)
    	{
    	}
    
    	Rectangle getRect(int thisCol, int thisRow)
    	{
    		// if input is out of range, return "null"
    		if(thisCol <0 || thisRow < 0)
    			return null;
    		if(thisCol>=numCols || thisRow>=numRows)
    			return null;
    
    		// otherwise, make and return the Rectangle
    		int w = getWidth()/numCols;
    		int h = getHeight()/numRows;
    
    		int x = thisCol*w;
    		int y = thisRow*h;
    
    		Rectangle myRect = new Rectangle(x,y,w,h);
    		return myRect;
    	}
    
    	public void paint(Graphics g)
    	{
    		g.setColor(Color.gray);
    		g.fillRect(0,0,getWidth(), getHeight());
    		g.setColor(Color.black);
    
    
    		Graphics2D g2 = (Graphics2D)g;
    		// we'll use Graphics2D for it's "draw" method -
    		// neater than the Graphics "drawRect" suppled
    		// (which you could also use)
    
    		for (int i = 0;i<numCols;i++)
    		{
    			for(int j = 0;j<numRows;j++)
    			{
    				Rectangle r = getRect(i,j);
    				BufferedImage bi = myModel.getSubimage(i,j); 
    				g2.drawImage(myModel.bi ,r.x ,r.y ,r.width ,r.height ,null ); // ERROR HERE
    			}
    		}
    	}
    	
    	
    }
    I changed the line but its the same... ://


    SlidingBlockPanel.java:94: cannot find symbol
    symbol : variable bi
    location: class SlidingBlockModel
    g2.drawImage(myModel.bi ,r.x ,r.y ,r.width ,r.height ,null );
    ^
    1 error


    my mind is melting by now... lol

  15. #15
    PmRg is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    FIXED


    g2.drawImage(bi ,r.x ,r.y ,r.width ,r.height ,null );

    :DDDD

  16. #16
    PmRg is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Now stuck on game logic dunno how to use the math.abs they r saying I have to use

    Java Code:
    import java.awt.image.*;
    import javax.imageio.*;
    import java.io.File;
    import javax.swing.*;
    
    public 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;
         			int numCols = 5;
         			int numRows = 4;
         			myModel = new SlidingBlockModel(numCols, numRows, "KTM.jpg");
        			  for (int col = 0; col < numCols; col++) {
            			 for (int row = 0; row < numRows; row++) {
               				 myModel.getImageRect(col, row);
            					 }
         		 }
    				
    				myModel.getImageRect(4,2);
    				
    			}
    		
    	public SlidingBlockModel (int input_numCols, int input_numRows, String image_filename)
    			{
    				numCols = input_numCols;
    				numRows = input_numRows;
    				
    				//BufferedImage original_image = null;
    			
    				try
    					{
    						original_image = ImageIO.read(new File(image_filename));
    						System.out.println("image " + image_filename + " loaded in ok." );
    						System.out.println("High is " + original_image.getHeight() + " and width is " + original_image.getWidth());
    					}
    			   catch (Exception e)
    					{
    						System.out.println("Sorry - couldn't load in file " + image_filename);
    					}
    				
    			puzzle = new BufferedImage[numCols][numRows];
    				for (int i = 0;i<numCols;i++)
    				{
    			 		for(int j = 0;j<numRows;j++)
    			 		{
    			 			puzzle[i][j] = getImageRect(i,j);
    			 		}
    			 	}
    			 	
    			 	empty_row = 0;
                	empty_col = 0;
                    puzzle[empty_col][empty_row] = null;	
    			}
    			
    	public BufferedImage getImageRect(int thisCol, int thisRow)
    			{
    					int w = original_image.getWidth()/numCols;
    					int h = original_image.getHeight()/numRows;
    					int x = thisCol*w;
    					int y = thisRow*h;
    						System.out.println("High is " + h + " and width is " + w);
    
    		    		return original_image.getSubimage(x,y,w,h);
    			}
    	
    	public BufferedImage getSubimage(int thisCol, int thisRow)
    			{
    					if ((thisCol < 0) || (thisCol >= numCols))
    					{
    						return original_image;
    					}
    
    					if ((thisRow < 0) || (thisRow >= numRows))
    					{
    						return original_image;
    					}
    					
    					return puzzle[thisCol][thisRow];
    			}
    			
    	boolean slideBlock(int thisCol, int thisRow)
    			{
    				if(thisCol<0 || thisCol>=empty_col)
    					return false;
    				if(thisRow<0 || thisRow>=empty_row)
    					return false;
    					
    				
    					
    				//if(empty_col==thisCol
    			
    			
    			return true;
    			
    				
    			}
    }
    SlideBlock method :S :S :S :S :S :S :confused::confused::confused:

Similar Threads

  1. Facing problem in compiling user defined package
    By UJJAL DHAR in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 12:40 PM
  2. Having a Problem Compiling with 2 methods
    By Hey Taclone in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 09:58 AM
  3. problem compiling servlet
    By vivek_9912 in forum Java Servlet
    Replies: 1
    Last Post: 02-13-2009, 12:37 PM
  4. problem compiling servlets
    By boy22 in forum Java Servlet
    Replies: 1
    Last Post: 08-03-2007, 11:05 AM
  5. getting problem in compiling java program?
    By sathish04021984 in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •