Drawing images over a JPanel

A

Abs

Hi people,

I'm using active rendering over a JPanel implementing Runnable and I
have some doubts I want share with you. Which of the following codes is
better (performace, etc...):


1st one:
-----------


BufferedImage im=loadImage(file);
Graphics2D g = (Graphics2D) getGraphics();

while (threadSuspended) {
g.drawImage(im, 0, 0, this);
}
g.dispose();




2nd one:
------------


BufferedImage im=loadImage(file);

while (threadSuspended) {
Graphics2D g = (Graphics2D) getGraphics();
g.drawImage(im, 0, 0, this);
g.dispose();
}




Do I have to flush the BufferedImage too ? when ?



Thanks in advance
 
M

Mark Murphy

Abs said:
1st one:
-----------
BufferedImage im=loadImage(file);
Graphics2D g = (Graphics2D) getGraphics();

while (threadSuspended) {
g.drawImage(im, 0, 0, this);
}
g.dispose();
2nd one:
------------
BufferedImage im=loadImage(file);

while (threadSuspended) {
Graphics2D g = (Graphics2D) getGraphics();
g.drawImage(im, 0, 0, this);
g.dispose();
}

Abs, I'm just starting out with Java, BUT without the context of the
surrounding code I would say that you would normally do the 2nd. But I
think it would have more to do with code readability then anything else.
If the thread is suspended, you probably don't care about the expense of
the getGraphics call. When the thread is not suspended, your going to
have to make the graphics call no matter.

You might what to check out the code here.
http://fivedots.coe.psu.ac.th/~ad/jg/

Read Ch1 and Ch2. and check out his code. The info is a little rough
since it is a draft for a book. I've been expanding on some of his code.
Tweaking it and making "improvements" based on the bits and pieces I
find in other locations.

Mark
 
T

Thomas Weidenfeller

Abs said:
I'm using active rendering over a JPanel implementing Runnable and I
have some doubts I want share with you. Which of the following codes is
better (performace, etc...):

Unless you have some very special requirements (which you didn't tell
us), both methods are equally bad, because you are not following the
Swing/AWT painting model.

See the comp.lang.java.gui FAQ (just posted a few days ago). Related
questions are Q3.1 (esp. the second url in that link), Q3.2, Q3.4 (why
getGraphics() is usually not a good idea), and the stuff in section 7.

/Thomas
 
A

Abs

Thomas said:
Unless you have some very special requirements (which you didn't tell
us), both methods are equally bad, because you are not following the
Swing/AWT painting model.

I'm developing a fullscreen slideshow app, so I thought the Swing
passive rendering method wasn't suitable for it. Which alternatives do I
have ? I don't think Java 2D games use passive rendering at all.
 
T

Thomas Weidenfeller

Abs said:
I'm developing a fullscreen slideshow app,

Ah, that was the information that was missing in your original post. If
the slides change only at a slow rate, e.g. once a second, I would use a
timer. In the timer run method I would replace the reference to the
image, and then call repaint(). In the paintComponent() method I would
just use drawImage() to draw the image.

Or you could have look at the Java media Framework (JMF). I never used
it, but maybe it already supports such applications.

Or you might borrow some ideas from the following:
I don't think Java 2D games use passive rendering at all.

I am not a Java game expert, but all the action game's code I have seen
roughly do the following:

- No own threads, because timing of threads is not predicable.

- A big main loop, the game loop. It checks for user input, runs through
all necessary calculations, prepares the next frame and displays it, and
controls the frame rate (by timing each loop iteration and doing a sleep
on the difference of the loop time and the desired frame rate).

- Usage of an AWT top-level component (Frame or Window), and not Swing
for displaying the screens. Depending on the game, and if supported by
the particular JRE, set to full screen mode (including setting of color
depth, etc.). No other component is used, just the top-level component.

- Usage of the rather interesting BufferStrategy mechanism to either
page-flip or bitblt a new frame on the screen. You can use that with
Swing, too. But it doesn't seem to be popular among game programmers.

/Thomas
 
A

Alex Hunsley

Abs said:
Hi people,

I'm using active rendering over a JPanel implementing Runnable and I
have some doubts I want share with you. Which of the following codes is
better (performace, etc...):


1st one:
-----------


BufferedImage im=loadImage(file);
Graphics2D g = (Graphics2D) getGraphics();

while (threadSuspended) {
g.drawImage(im, 0, 0, this);
}
g.dispose();




2nd one:
------------


BufferedImage im=loadImage(file);

while (threadSuspended) {
Graphics2D g = (Graphics2D) getGraphics();
g.drawImage(im, 0, 0, this);
g.dispose();
}




Do I have to flush the BufferedImage too ? when ?


Btw, where is the above code actually written in the program? Can you
send the full listing?
alex
 
A

Alex Hunsley

Abs said:
I'm developing a fullscreen slideshow app, so I thought the Swing
passive rendering method wasn't suitable for it. Which alternatives do I
have ? I don't think Java 2D games use passive rendering at all.

Swing's rendering is semi-passive: if you call repaint() (which can be
done from any thread), Swing will repaint the component by calling
paintGraphics sometime soon (but not within any guaranteed time).

alex
 
T

Thomas Weidenfeller

Thomas said:
- Usage of the rather interesting BufferStrategy mechanism to either
page-flip or bitblt a new frame on the screen. You can use that with
Swing, too. But it doesn't seem to be popular among game programmers.

That should mean:

Swing doesn't seem to be popular among game programmers.

BTW: All this was of course for J2SE.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top