Re: thread-safe graphics access
Stefan Ram wrote:
In a book on Java game programming, I found a game that uses a
component (called ?this? below), which is a JPanel.
This component has a custom thread to update it.
This thread, simplified, does this to update the game display:
this.getGraphics().drawImage( gameImage, 0, 0, null );
I notice that this is an access of a Swing component from
within the custom thread, outside of the EDT, though I am not
sure what constitutes the actual offending access
(?getGraphics? or ?drawImage?).
An older Sun tutorial says:
?Swing components can be accessed by only one thread at a time.
Generally, this thread is the event-dispatching thread.?
So, can I get by without using ?InvokeAndWait?, when I am
confident that no one else is accessing the graphics object of
my custom component at this time?
- I can make sure that no other thread of my program
accesses this JPanel.
- I do not expect Swing to draw to the graphics object
of this component.
But what happens, if a Swing menu is added to the program,
gets openend and then overlaps this custom component?
So, in the end, I should wrap this access with an invoke
method?
The whole game is a kind of loop:
while( running)
{ update( gameImage );
this.getGraphics().drawImage( gameImage, 0, 0, null ); }
So, if I have to wrap the final statement, how do I do this?
My first impression is that this needs ?InvokeAndWait?,
because this is most close to the original behavior as shown
above. Or may/should I use ?InvokeLater? in this case?
I'm not a fan of using the getGraphics() to draw on components when
doing animation. I still prefer to do all drawing in the
paintComponent() method. That way if the system needs a repaint it just
does it. Also, to get fast and smooth (which is often more important
than fast) I like to use a BufferedImage as my drawing area and then use
EventQueue.invokeAndWait() on a paintImmediately() call. On a
multi-processor machine you can use a tight loop with no waits to create
delays. This is very smooth and much better than any of the timers on
Windows machines. It works fine on Linux too but the timers are better
there as well. In the past I used to use active rendering with a
BufferStrategy but the performance on Linux was poor and as of the later
1.6 compilers I get better performance with BufferedImage. Using a
compatible image also helps.
If your graphics card is fast enough you can uncomment the delay loop in
the run() method and get exactly 200 fps. If you have a slow computer
or card, try changing the timing value.
Oh, and pardon my use of the same variable names, I do realize it can be
a little confusing.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.lang.reflect.*;
import javax.swing.*;
public class test3 extends JPanel implements Runnable {
volatile BufferedImage bi;
volatile long then;
long now,time;
Thread thread;
double angle;
int n;
double rate;
public test3() {
setPreferredSize(new Dimension(400,300));
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
GraphicsConfiguration gc = getGraphicsConfiguration();
bi = gc.createCompatibleImage(getWidth(),getHeight(),
BufferedImage.OPAQUE);
}
});
}
public void start() {
then = System.nanoTime();
thread = new Thread(this);
thread.start();
}
public void stop() {
thread.interrupt();
}
public void run() {
try {
long now = 0;
long then = System.nanoTime();
while (true) {
render();
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
paintImmediately(getBounds());
}
});
} catch (InvocationTargetException ite) {
System.out.println(ite);
}
/*
while (now < then + 5000000)
now = System.nanoTime();
then = now;
*/
}
} catch (InterruptedException ie) {
System.out.println(ie);
}
}
public void render() {
int w = getWidth();
int h = getHeight();
Graphics2D g = bi.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0,0,w,h);
g.setColor(Color.RED);
g.drawString(String.format("%5.1f",rate),10,12);
angle += 0.001;
g.rotate(angle,w/2,h/2);
g.setColor(Color.BLUE);
g.fillRect(w/2 - 100,h/2 - 100,200,200);
if (++n % 100 == 0) {
now = System.nanoTime();
time = now - then;
then = now;
rate = 100000000000.0 / time;
}
g.dispose();
}
public void paintComponent(Graphics g) {
g.drawImage(bi,0,0,null);
}
public static void main(String[] args) {
final test3 t3 = new test3();
final JFrame f = new JFrame();
f.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent we) {
t3.start();
}
public void windowClosing(WindowEvent we) {
t3.stop();
f.dispose();
}
});
f.add(t3,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
--
Knute Johnson
email s/nospam/knute2009/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access