Results 1 to 14 of 14
- 11-26-2011, 03:01 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
Need Help with Loading couple images to a BufferedImage.
Greetings EveryOne

I am making a board game, I want to load the board and the pieces with their current locations into a BufferedImage then paint the BufferedImage to the screen, is this a good aproch? if not can you show me a better one?
Here is the code that I came up with:
The output is a 50*50 black square, the image doesn't shows up, even it's in the same directory as the applet.Java Code:import java.applet.*; import java.awt.*; import java.awt.image.*; public class ImageExampleWithBuffer extends Applet { private int WinWidth = 200; private int WinHeight = 200; Image IMG; BufferedImage BuffImg = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB); @Override public void init() { setSize(WinWidth, WinHeight); IMG = getImage(getDocumentBase(), "Smily.jpg"); Graphics2D Graph = BuffImg.createGraphics(); Graph.drawImage(IMG, 50, 50, this); repaint(); } @Override public void paint(Graphics G) { G.drawImage(BuffImg, 50, 50, this); } }
Please SomeOne Help, Thank You.Knowledge Is Power! Don't give me a fish but teach me how to catch one.gif)
I am not asking you to code it for me, what i need is guidance so i can learn
- 11-26-2011, 06:26 PM #2
Re: Need Help with Loading couple images to a BufferedImage.
To load a image from a file enter in this code:
BufferedImage img = null;
try {
img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
}
Be sure to substitute strawberry.jpg for the actual file.
If the code is running in an applet, then its just as easy to obtain the image from the applet codebase:
try {
URL url = new URL(getCodeBase(), "strawberry.jpg");
img = ImageIO.read(url);
} catch (IOException e) {
}
Image I/O which has support for GIF, PNG, JPEG, BMP, and WBMP, recognises the contents of the file as a JPEG format image, and decodes it into a BufferedImage which can be directly used by Java 2D.
Hope this helps. :)Last edited by SourCookie; 11-26-2011 at 06:28 PM.
- 11-26-2011, 06:26 PM #3
Re: Need Help with Loading couple images to a BufferedImage.
How are you executing the program?
Do you get any error messages?
- 11-26-2011, 07:11 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
Re: Need Help with Loading couple images to a BufferedImage.
Knowledge Is Power! Don't give me a fish but teach me how to catch one.gif)
I am not asking you to code it for me, what i need is guidance so i can learn
- 11-26-2011, 07:13 PM #5
Re: Need Help with Loading couple images to a BufferedImage.
Read the API doc for the getImage method. It probably doesn't do what you want when you want it.
- 11-26-2011, 07:31 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
Re: Need Help with Loading couple images to a BufferedImage.
Knowledge Is Power! Don't give me a fish but teach me how to catch one.gif)
I am not asking you to code it for me, what i need is guidance so i can learn
- 11-26-2011, 09:11 PM #7
Re: Need Help with Loading couple images to a BufferedImage.
That says to me that the image data might not be loaded UNTIL you attempt to draw the image on the screen.When this applet attempts to draw the image on the screen, the data will be loaded.
Your code does NOT attempt to draw the image on the screen so it might not be loaded.
Try adding this to your paint method:
G.drawImage(IMG, 250, 250, this); // This causes the image to be loaded
and if that works, add this following the above (all in paint):
Graphics2D Graph = BuffImg.createGraphics();
Graph.drawImage(IMG, 0, 0, this);
G.drawImage(BuffImg, 100, 100, this);
I got the image to draw on the screen with both of the above.Last edited by Norm; 11-26-2011 at 09:14 PM.
- 11-27-2011, 03:29 AM #8
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
Re: Need Help with Loading couple images to a BufferedImage.
Thank you Sir Norm, You are awesome

Here is the code in case someone curious:
Any comment, suggestion I will be glad to heart it.Java Code:import java.applet.*; import java.awt.*; import java.awt.image.*; public class ImageExampleWithBuffer extends Applet { private int WinWidth = 200; private int WinHeight = 200; Image IMG; BufferedImage BuffImg = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB); Graphics2D Graph = BuffImg.createGraphics(); @Override public void init() { setSize(WinWidth, WinHeight); IMG = getImage(getDocumentBase(), "WhiteSmily.jpg"); repaint(); } @Override public void paint(Graphics G) { Graph.drawImage(IMG, 0, 0, this); G.drawImage(BuffImg, 50, 50, this); } }Last edited by Shikatsu; 11-27-2011 at 03:32 AM.
Knowledge Is Power! Don't give me a fish but teach me how to catch one.gif)
I am not asking you to code it for me, what i need is guidance so i can learn
- 11-27-2011, 07:36 AM #9
Re: Need Help with Loading couple images to a BufferedImage.
Why do they call it rush hour when nothing moves? - Robin Williams
- 11-27-2011, 01:08 PM #10
Re: Need Help with Loading couple images to a BufferedImage.
DB Nice post on the ranch
- 11-27-2011, 02:43 PM #11
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
Re: Need Help with Loading couple images to a BufferedImage.
It's a against the rules to post the same question on a different forum or something?
Knowledge Is Power! Don't give me a fish but teach me how to catch one.gif)
I am not asking you to code it for me, what i need is guidance so i can learn
-
Re: Need Help with Loading couple images to a BufferedImage.
Did you read the link that Darryl gave you on the ranch regarding this? Please read the information given to you before making us restate that which has been stated before.
- 11-27-2011, 03:15 PM #13
Member
- Join Date
- Nov 2011
- Posts
- 49
- Rep Power
- 0
Re: Need Help with Loading couple images to a BufferedImage.
I have read that on coderanch, I asked here before I noticed that post, and I was wondering if it's the same here.
imho, I won't be annoyed if someone came and asked me a question then he/she went to someone else to get a second opinion, it's like getting a second opinion from another doctor, should that be annoying? I am just wondering.
Even though, I posted there because no one answered for a while and I don't have much free time and when I do I need to achieve the most I can in that time.
Thank you for you understanding.Knowledge Is Power! Don't give me a fish but teach me how to catch one.gif)
I am not asking you to code it for me, what i need is guidance so i can learn
-
Re: Need Help with Loading couple images to a BufferedImage.
The principles of etiquette are the same everywhere.
Again read the link. We're not annoyed by multiposting. We're annoyed by your not being forthcoming about it.imho, I won't be annoyed if someone came and asked me a question then he/she went to someone else to get a second opinion, it's like getting a second opinion from another doctor, should that be annoying? I am just wondering.
And by multiposting without notification, you show that you're not respectful of our time. As the link states, cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.Even though, I posted there because no one answered for a while and I don't have much free time and when I do I need to achieve the most I can in that time.
Ditto.Thank you for you understanding.
Similar Threads
-
Loading Images
By TacoManStan in forum Java GamingReplies: 9Last Post: 09-22-2011, 11:18 PM -
Loading files(namely images)
By jammas615 in forum New To JavaReplies: 1Last Post: 07-11-2011, 10:06 AM -
Loading images from an Array
By pinkette in forum New To JavaReplies: 5Last Post: 04-15-2011, 11:37 AM -
Loading Images - Imp
By Thulasiraman in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 09:33 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks