Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default Why doesn't the image draw?

    I've been debugging this thing for an hour trying to figure out why the image won't draw and I'll give you all the information I know about it. I just have no idea.

    This method:

    Java Code:
    public static void drawGraphics(Graphics g)
    	{
    		image = grid[0][0].getTile().getImage();
    		g.drawImage((Image)image, 0, 0, null);
    	}
    Gets the 'image' variable from this method:

    Java Code:
    //This file exists
    			Image img = Toolkit.getDefaultToolkit().getImage("C:/Users/Micah/Documents/java/attempts/Castle Siege a4/data/tiles/images/null.png");
    
    			image = new BufferedImage(250, 250, BufferedImage.TYPE_INT_ARGB);
    			Graphics2D g = (Graphics2D)image.getGraphics();
    
    			//Draw a blue square for debugging purposes
    			g.setColor(Color.BLUE);
    			g.fillRect(100, 100, 250, 250);
    
    			g.drawImage(img, 0, 0, null);
    and displays this:



    but if I change the first method to try and draw the desired image on it's own without trying to get the image from the second method it works fine. The weird thing is they are doing the SAME operation and are in the SAME directory. No error messages at all, it just doesn't work!

    Java Code:
    	public static void drawGraphics(Graphics g)
    	{
    		Image img2 = Toolkit.getDefaultToolkit().getImage("C:/Users/Micah/Documents/java/attempts/Castle Siege a4/data/tiles/images/null.png");
    
    		g.drawImage((Image)img2, 0, 0, null);
    	}
    This works:



    and here you can see it in the same folder together:



    So-

    1. The png directory is correct
    2. The BufferedImage is being called from the second method correctly
    3. The BufferedImage is being drawn correctly
    4. The png can be drawn


    So why won't the image draw in the second method but not the first?!

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

    Default

    Please click on and check out the 3rd link in my signature links below, the one on creating an SSCCE, because this is likely what you'll need if we're to have a chance of figuring this thing out.

    Luck!

  3. #3
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    S: Yes
    S: Yes
    C: Yup
    C: Yuppers
    E: Mmmhmm

    Thanks dude. Been at this a long time. May not be a java veteran but I know how a forum works. So do you know what the problem might be?

  4. #4
    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 MrFish View Post
    S: Yes
    S: Yes
    C: Yup
    C: Yuppers
    E: Mmmhmm

    Thanks dude. Been at this a long time. May not be a java veteran but I know how a forum works. So do you know what the problem might be?
    The problem is that you've got a bug in your program. The other problem is that we can't read minds and guess what that bug might be or where it might be. Did you read the link? It explains why creating and posting an SSCCE will help us to figure these things out. It's up to you and how much you need our help.

    PS: SA replies will not help you either.

    Much luck.

  5. #5
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Well I don't know why there is a problem or it wouldn't be happening. I've given you the relevant code and visual aids. I also gave you some of my debugging conclusions. What more do you need?

  6. #6
    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 MrFish View Post
    Well I don't know why there is a problem or it wouldn't be happening. I've given you the relevant code and visual aids. I also gave you some of my debugging conclusions. What more do you need?
    OK, here's an important question I have for you: How do you know that you've given us the relevant code if you have no idea what's causing the error?

    Again, we need an SSCCE to be able to run and test your code, modify it and determine what's wrong, but obviously I can't convince you no matter how hard I try, and I'm done trying.

    Best of luck.

  7. #7
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    The relevant code is the code that isn't working. After an hour/hour and a half of debugging I think I would have to be pretty dumb not to know what wasn't working.

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by MrFish View Post
    The relevant code is the code that isn't working. After an hour/hour and a half of debugging I think I would have to be pretty dumb not to know what wasn't working.
    Yep, you're obviously a genius, and so you don't need our help.

    But seriously, most errors are due to things that you think you've ruled out but have over looked. When you assume that it's one thing, you're bound to be mistaken, and until the error has been fully solved all possibilities are open, and so your safest bet is to assume nothing. What is your religious objection to creating this SSCCE? You're obviously not received a solution here with the information that you've so far given us? So what's the harm in your doing this?
    Last edited by curmudgeon; 11-16-2010 at 12:50 AM.

  9. #9
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    Yep, you're a frikkin' genius. So you obviously don't need our help!
    Get off Fubarable's d***. How about you make a relevant post and stop acting like a troll.

    Rule #25: Relation to the original topic decreases with every single post.

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by MrFish View Post
    Get off Fubarable's ****. How about you make a relevant post and stop acting like a troll.

    Rule #25: Relation to the original topic decreases with every single post.
    How about posting the requested information and watching your language?

  11. #11
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    How about posting the requested information
    Done and done. Any more advice?

  12. #12
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by MrFish View Post
    Done and done.
    Yes, where is the Short, Self Contained, Correct (Compilable), Example that you claimed to have created and posted? A minimal running example?

  13. #13
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    Yes, where is the Short, Self Contained, Correct (Compilable), Example that you claimed to have created and posted? A minimal running example?
    Short: 277 words and 3 helper images to make the problem more clear. %90 of the code was removed from the methods so you only got what was relevant.

    Self Contained: If you want me to post 5 class files in order for you to run the program in full that would be ridiculous. I have, however, posted all that needs to be looked at for my problem. There is literally nothing that you don't see except a get method that looks like this-

    Java Code:
    public Image getImage(){return image;}
    All in one line too.

    Correct (Compilable): Sure is. Compiled it before posting.

    What about my original post doesn't follow these rules?

    Rule #11: All your carefully picked arguments can easily be ignored.

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by MrFish View Post
    What about my original post doesn't follow these rules?
    The most important one -- that all of them need to be satisfied. The idea being if others can see the smallest program possible that reproduces the problem, and be able to run and modify the program themselves they'll likely be able to help you solve the dilemma. No one wants to slog through pages of code, but if you put in the effort to shrink it, many will put in the effort to help you.

    If all the information necessary to solve your problem were in your initial post, don't you think you'd have solutions posted already? I feel pretty comfortable with graphics programming and I sure don't see enough information to be able to solve it, Fubarable didn't see enough information, and the legions who have not bothered to post in this thread likely didn't see enough information.

  15. #15
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    When asking about code

    The most effective way to be precise about a code problem is to provide a minimal bug-demonstrating test case. What's a minimal test case? It's an illustration of the problem; just enough code to exhibit the undesirable behavior and no more. How do you make a minimal test case? If you know what line or section of code is producing the problematic behavior, make a copy of it and add just enough supporting code to produce a complete example (i.e. enough that the source is acceptable to the compiler/interpreter/whatever application processes it). If you can't narrow it down to a particular section, make a copy of the source and start removing chunks that don't affect the problematic behavior. The smaller your minimal test case is, the better (see the section called “Volume is not precision”).

    Generating a really small minimal test case will not always be possible, but trying to is good discipline. It may help you learn what you need to solve the problem on your own — and even when it doesn't, hackers like to see that you have tried. It will make them more cooperative.
    -- from a reference on posting programming questions.

  16. #16
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    A flowchart just for you-



    Is it beginning to make sense? The problem is obviously pinpointed at the Image related methods. The png exists and can be called by the given location- which I've shown you. So it seems the second method is having trouble drawing the image for whatever reason. Being an expert at graphics, you should know the problems that might occur in that area.

  17. #17
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by MrFish View Post
    A flowchart just for you-

    Is it beginning to make sense? The problem is obviously pinpointed at the Image related methods. The png exists and can be called by the given location- which I've shown you. So it seems the second method is having trouble drawing the image for whatever reason. Being an expert at graphics, you should know the problems that might occur in that area.
    I'm not the expert, you are. Good luck getting help Einstein.

  18. #18
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Rule 11

    Thanks again.

  19. #19
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    This post/question is not to Mr Fish but to any one else out there who is comfortable with Java graphics programming: Please chime in -- do you feel that the information provided by Mr. Fish is adequate for solving his problem, and if so, what is the solution? I'll gladly swallow my pride and admit I'm wrong if proven wrong. Please prove me wrong.

  20. #20
    ybobjoe is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by MrFish View Post
    Java Code:
    	public static void drawGraphics(Graphics g)
    	{
    		Image img2 = Toolkit.getDefaultToolkit().getImage("C:/Users/Micah/Documents/java/attempts/Castle Siege a4/data/tiles/images/null.png");
    
    		g.drawImage((Image)img2, 0, 0, null);
    	}
    You may be trying to draw your image before it has finished loading.

    You could try something like this:
    Java Code:
    	public static void drawGraphics(Graphics g)
    	{
    		Image img2 = Toolkit.getDefaultToolkit().getImage("C:/Users/Micah/Documents/java/attempts/Castle Siege a4/data/tiles/images/null.png");
    		while(img2.getWidth(componentName) == -1){;}
    		g.drawImage((Image)img2, 0, 0, null);
    	}
    The width and height will return -1 until the image has finished loading. If this fixes the problem I suggest you use a MediaTracker when loading your files to avoid these types of problems.

    The people above me just wanted code that they could run and use a debugger on, also the location of code can generate different results so having a full(but short) working example, that we can run, helps a lot.

Page 1 of 2 12 LastLast

Similar Threads

  1. Best Swing widget to draw and edit image
    By bikashg in forum New To Java
    Replies: 1
    Last Post: 04-29-2010, 05:21 PM
  2. How can i draw a circle on an image uploaded ?
    By motress in forum New To Java
    Replies: 1
    Last Post: 04-15-2010, 04:55 PM
  3. Read and draw a local image.
    By ichwar in forum New To Java
    Replies: 1
    Last Post: 07-27-2009, 02:02 PM
  4. Unable to draw buffered image
    By pedjasmek in forum Java 2D
    Replies: 7
    Last Post: 08-08-2008, 03:49 PM
  5. how to draw an image inside of jscrollpane
    By paty in forum Java Applets
    Replies: 1
    Last Post: 07-24-2007, 12:44 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
  •