# Drawing Images to a BufferStrategy

This topic is 2271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi All! It seems like I've been posting on here a lot lately so let me start by saying Thank you to everyone who has helped me lately!
Now, my latest problem:
I'm using a BufferStrategy in Java to draw directly on a JFrame. The Frame displays and I can draw shapes on it. However, any call to g.drawImage(); (g being a Graphics object) doesn't do anything. I'm not sure what's happening. The code seems to be getting passed over, because I've put print statements before and after it and they both get printed.

Here's the important snippets of code:
This code actually calls the other objects Draw Functions
 private void drawImage(){ BufferStrategy bf = this.getBufferStrategy(); Graphics g = null; try{ g = bf.getDrawGraphics(); //Pass off Graphics to each object so it can draw itself mmenu.drawMenu(g); }finally{ g.dispose(); } bf.show(); Toolkit.getDefaultToolkit().sync(); } 
And this is the draw method of the menu object (mmenu) and it's constructor
 private Image bg; public MainMenu(String bg){ this.bg = Toolkit.getDefaultToolkit().getImage(bg); } public void drawMenu(Graphics g){ g.drawImage(bg,100,100,null); } 

##### Share on other sites
Both Toolkit and Graphics2D are designed to fail silently, which is why you have no indication of a problem other than the image simply not showing. The most likely culprit is the image itself - a bad filename or a large image that AWT loads too slowly to be presented immediately are pretty common. You need to check that the image's dimensions are not (-1, -1) via getWidth(null) and getHeight(null). E.g.:

 public void drawMenu(Graphics g){ if((image == null) || (image.getWidth(null) == -1 || image.getHeight(null) == -1)) throw new RuntimeException("The image wasn't loaded correctly."); // ... } 

##### Share on other sites
Ok, its definitely something to do with that. I don' think it's size because I've loaded this image into Java the exact same way before. I'm using NetBeans to write this code, and it's building the files in a different location than the java files. They look identical though. ie. the path to the resource is the same in both places. Could it also have something to do with the fact that in NetBeans the java/class files are in one package and the images are in a sub package. ie. platform.game has all the java files, while platform.game.Assets.Backgrounds has the image I want.

##### Share on other sites
Maybe it will help
 //loading image void loadImages() { bg = new ImageIcon(this.getClass().getResource("background.png")).getImage(); } //drawing image void draw(Graphics2D g2d) { g2d.drawImage(bg, 0, 0, null); } //this one on init to create shit void init()P createBufferStrategy(2); strategy = getBufferStrategy(); g2d = (Graphics2D) strategy.getDrawGraphics(); } //this one has more code, bet important is this piece of code private void graphics() { strategy = getBufferStrategy(); g2d = (Graphics2D) strategy.getDrawGraphics(); strategy.show(); } //and in game loop I draw it private void gameLoop() { while (running){ graphics(); draw(g2d); g2d.dispose(); } }  Edited by Plutonium

##### Share on other sites
Ok, Most of that makes sense. What's the difference between Image and ImageIcon? also, How do I set resources for each class? Other than that, I'm doing most of the other stuff already.

##### Share on other sites
Difference don't know, but my code works All images is in src folder, and can easily simple get by name (as in my code). If you wanna I can send you my whole project, if you willing to read a lot of code I programmed in netbeans too

##### Share on other sites
Could it also have something to do with the fact that in NetBeans the java/class files are in one package and the images are in a sub package. ie. platform.game has all the java files, while platform.game.Assets.Backgrounds has the image I want[/quote]

If you have your media embedded in a package, then you need to use getResource(URL) to access it from within the archive. E.g.
 public MainMenu(String bg){ // the "/" character specifies the root of your package hierarchy final URL myURL = MainMenu.class.getResource("/platform/game/Assets/Backgrounds/myImage.jpg"); this.bg = Toolkit.getDefaultToolkit().getImage(myURL); } 

The other way to resolve this is to move your image directory outside your package structure. E.g.

Root Directory

• platform

• game
• assets

• backgrounds

Netbeans should easily allow you to create additional folders. I also created a simple example to test this (BufferStrategy + Image loading) and it worked fine.

Edit: Fixed path. Edited by Kyan

1. 1
Rutin
25
2. 2
3. 3
JoeJ
19
4. 4
5. 5

• 10
• 11
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631753
• Total Posts
3002098
×