Sign in to follow this  
destructivArts

Drawing Images to a BufferStrategy

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
[CODE]
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();
}
[/CODE]
And this is the draw method of the menu object (mmenu) and it's constructor
[CODE]
private Image bg;

public MainMenu(String bg){
this.bg = Toolkit.getDefaultToolkit().getImage(bg);
}
public void drawMenu(Graphics g){
g.drawImage(bg,100,100,null);
}
[/CODE]

Share this post


Link to post
Share on other sites
Both [i]Toolkit[/i] and [i]Graphics2D[/i] 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 [i]getWidth(null)[/i] and [i]getHeight(null)[/i]. E.g.:

[code]
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.");
// ...
}
[/code]

Share this post


Link to post
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 this post


Link to post
Share on other sites
Maybe it will help
[CODE]
//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();
}
}

[/CODE] Edited by Plutonium

Share this post


Link to post
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 this post


Link to post
Share on other sites
Difference don't know, but my code works :D 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 this post


Link to post
Share on other sites
[quote]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 [i]getResource(URL)[/i] to access it from within the archive. E.g.
[code]
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);
}
[/code]

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

[i]Root Directory[/i][list]
[*][i]platform[/i]
[list]
[*][i]game[/i]
[/list][*][i]assets[/i]
[list]
[*][i]backgrounds[/i]
[/list]
[/list]
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this