Results 1 to 20 of 21
Thread: Why doesn't the image draw?
- 11-15-2010, 05:11 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
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:
Gets the 'image' variable from this method:Java Code:public static void drawGraphics(Graphics g) { image = grid[0][0].getTile().getImage(); g.drawImage((Image)image, 0, 0, null); }
and displays this: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);

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!
This works: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); }

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

So-
- The png directory is correct
- The BufferedImage is being called from the second method correctly
- The BufferedImage is being drawn correctly
- The png can be drawn
So why won't the image draw in the second method but not the first?!
-
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!
- 11-15-2010, 12:31 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
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.
- 11-15-2010, 06:36 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
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.
- 11-15-2010, 09:47 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
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.
- 11-16-2010, 12:42 AM #8
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 11-16-2010, 12:51 AM #9
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
- 11-16-2010, 12:52 AM #10
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 11-16-2010, 12:53 AM #11
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
- 11-16-2010, 01:00 AM #12
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 11-16-2010, 01:08 AM #13
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
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-
All in one line too.Java Code:public Image getImage(){return image;}
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.
- 11-16-2010, 01:13 AM #14
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 11-16-2010, 01:15 AM #15
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
-- from a reference on posting programming questions.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.
- 11-16-2010, 01:25 AM #16
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
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.
- 11-16-2010, 01:28 AM #17
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 11-16-2010, 01:36 AM #18
Member
- Join Date
- Jan 2010
- Posts
- 43
- Rep Power
- 0
Rule 11
Thanks again.
- 11-16-2010, 03:05 AM #19
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 11-16-2010, 05:37 PM #20
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
You may be trying to draw your image before it has finished loading.
You could try something like this:
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.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 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.
Similar Threads
-
Best Swing widget to draw and edit image
By bikashg in forum New To JavaReplies: 1Last Post: 04-29-2010, 05:21 PM -
How can i draw a circle on an image uploaded ?
By motress in forum New To JavaReplies: 1Last Post: 04-15-2010, 04:55 PM -
Read and draw a local image.
By ichwar in forum New To JavaReplies: 1Last Post: 07-27-2009, 02:02 PM -
Unable to draw buffered image
By pedjasmek in forum Java 2DReplies: 7Last Post: 08-08-2008, 03:49 PM -
how to draw an image inside of jscrollpane
By paty in forum Java AppletsReplies: 1Last Post: 07-24-2007, 12:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks