Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Baine

[java] using g.drawImage(..) in linux

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''m following along with David Brackeen''s book, "Developing Games in Java". I originally was coding exclusively with windows, but I''ve recently installed Mandrake 10, and I was wondering how the code would hold up. Everything seems fine, except that the graphics don''t seem to be scaling properly with linux. The screen resolution seems to be at 1024 x 768, however the background image remains at what I think is 800 x 600, causing some weird effects when the foreground images move off of the background. Everything scales properly within Windows. I was wondering if anyone had come across this before, or if anyone has an idea about what may be happening. I''ve experienced the problem specifically with running the MouseLookTest, and InputManagerTest programs, though I''m fairly certain that any graphical application that uses the same framework as these two would show the same problems. Thanks for any help Ernie

Share this post


Link to post
Share on other sites
Advertisement
That all sounds kinda vague. Some sample code and a screen shot would be helpful to help us help you in helping yourself to a heaping helping of help.....

Share this post


Link to post
Share on other sites
hehe I was hoping for a quick fix from someone had read the book before, but here's a snippet..

for drawing. . .

// drawing the images

public void draw(Graphics2D g) {
// draw background

g.drawImage(bgImage, 0, 0, null);

// draw sprite

g.drawImage(player.getImage(),
Math.round(player.getX()),
Math.round(player.getY()),
null);
}


for setting up the screen. . .

// initializes the screen

private static final DisplayMode POSSIBLE_MODES[] = {
new DisplayMode(1024,768, 24, 0),
new DisplayMode(800, 600, 32, 0),
new DisplayMode(800, 600, 24, 0),
new DisplayMode(800, 600, 16, 0),
new DisplayMode(640, 480, 32, 0),
new DisplayMode(640, 480, 24, 0),
new DisplayMode(640, 480, 16, 0)
};


public void init() {
screen = new ScreenManager();
DisplayMode displayMode =
screen.findFirstCompatibleMode(POSSIBLE_MODES);
screen.setFullScreen(displayMode);

Window window = screen.getFullScreenWindow();
window.setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));
window.setBackground(Color.blue);
window.setForeground(Color.white);

isRunning = true;
}


as for a screen shot, you'll have to forgive me, but i'm a little new to this tag stuff.

[url]www.cs.mun.ca/~ehardy/sshot.png[/url]


you can (hopefully) see the trails outside of the background, left by the blue guy who can be moved around the screen. With Windows, the background fills the screen and there are no trails. Hopefully this helps a little more. Thanks for the quick reply, and let me know if there's anything else i can help with.

Thanks again.
Ernie

*edited cuz I screwed up the url tag*



[edited by - Baine on June 6, 2004 9:56:14 PM]

[edited by - Baine on June 6, 2004 9:57:50 PM]

Share this post


Link to post
Share on other sites
This sounds familiar to a bug I experienced years ago....

Try setting the high unused bits in your RGB to FF and see if that fixs it!

But you shouldn't have to do it though....

[edited by - chaoslab on June 7, 2004 5:06:16 AM]

Share this post


Link to post
Share on other sites
Nowhere are you finding out what the display mode was set to. You are taking the first compatable one that is given. On Windows you might have been getting 800x600, but on Linux maybe you got 1024x768. If you are going to allow any resolution, then you have to plan for it in your drawing code.

g.drawImage(bgImage,0,0,null);

should be:

g.drawImage(bgImage, 0, 0, screenWidth, screenHeight, null);

Although you may want to pre-scale the image, as scaling on the fly can be slow.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites
quote:
Original post by CaptainJester
Nowhere are you finding out what the display mode was set to. You are taking the first compatable one that is given. On Windows you might have been getting 800x600, but on Linux maybe you got 1024x768. If you are going to allow any resolution, then you have to plan for it in your drawing code.

g.drawImage(bgImage,0,0,null);

should be:

g.drawImage(bgImage, 0, 0, screenWidth, screenHeight, null);

Although you may want to pre-scale the image, as scaling on the fly can be slow.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

I have to agree here. The "trailing" effect is from the frame buffer not being initialized before each painting, which is not a problem if you get the background image to scale completely because drawing the image will wipe out any data previously in the frame buffer. (whoa, total deja vu there. maybe I should check through my post history to see if I actually have posted this before).

Anyway, if you are drawing a background image the size of the screen, you obviously don''t need to init the frame buffer, and in fact would probably not WANT to anyway, as it would be an unnecessary step, and unnecessary steps == slowness. However, it''s always good to know the root cause of a problem.

To scale the image once, and reuse it later without rescaling it, off the top of my head (so don''t take this code literally, use it as a jumping off point):

public void init(){
Image originalImage; //initialized however, you''ve already done this bit

Image scaledImage=new BufferedImage();
Graphics sig=scaledImage.getGraphics();
sig.drawImage(originalImage, 0, 0, windowWidth, windowHeight, this);
}


it''s something close to that (I don''t have my books with me right now). Anyway, as you can see the idea is to get a graphics object from the image itself, and paint to THAT. You might even be able to paint ontop of the original image, like

orignalImage.getGraphics().drawImage(originalImage, 0, 0, windowWidth, windowHeight, this);

and just continue to use the original image as normal.



capn_midnight | Captain Midnight | deviantArt
ACM | SIGGRAPH | Generation5
"I''m tired of all this nonsense about beauty being only skin-deep. That''s deep enough. What do you want - an adorable pancreas?" -Jean Kerr

Share this post


Link to post
Share on other sites
Thanks for the suggestions guys.. Scaling the bitmap to fit worked, but it was slow as you indicated it would be. I played around a bit more, and it seems like the display mode isn''t been changed. Even when I code it to run explicity at 800 x 600..


public void init() {
screen = new ScreenManager();
screen.setFullScreen(new DisplayMode(800, 600, 24, 0));

Window window = screen.getFullScreenWindow();
window.setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));
window.setBackground(Color.blue);
window.setForeground(Color.white);

isRunning = true;
}


it seems like under linux the app is forced to run at the resolution the desktop is currently set at. Switching the desktop resolution to 800x600 fixes the problem. Any ideas as to why this is happening?

Thanks again,
Ernie

p.s. sorry for the newb-ish questions.. but i''m in the process of cutting my teeth on linux.

Share this post


Link to post
Share on other sites
Hmm.. I think I''ve isolated it. It''s my bad for not looking closely enough in the first place.

the display mode is set within this block of code..

public void setFullScreen(DisplayMode displayMode) {
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setIgnoreRepaint(true);
frame.setResizable(false);

device.setFullScreenWindow(frame);

if(displayMode != null && device.isDisplayChangeSupported())
{
try {
device.setDisplayMode(displayMode);
}
catch(IllegalArgumentException ex) {
// ignore - illegal mode for this device

}
}
frame.createBufferStrategy(2);
}


After testing, it seems that device.isDisplayChangeSupported() always returns false under linux. Is there a workaround for this, or is it a limitation of java full screen graphics under linux?

Again, thanks a lot.
Ernie

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!