Re: Optimisation for animated MultipleGradientPaint code
Andrew Thompson wrote:
Can the frame rate of this example code* be made higher?
* The 650ish lines of the code are posted here..
<http://forum.java.sun.com/thread.jspa?threadID=5297524>
--
Andrew T.
PhySci.org
Andrew:
Interesting program. I looked at what your buddy said on the web forum
and without trying those things for myself that is what I would have
guessed (with my vast experience and deductive capabilities). The only
way I can think of to get more performance would be to go to active
rendering. This requires that you draw on a Canvas or a Window as those
are the only components where getting at the BufferStrategy is possible
(as far as I know). I'm not that sure that the performance increase
would be that great. I would guess that the largest cost on your
program is the gradient painting although eliminating the BufferedImage
buffer should help some. I used to do a lot with VolatileImages but the
newer JVMs are much faster than the old ones and if I can't do it by
drawing a BufferedImage I go to active rendering.
Below is a simple active rendering example. It will do about 220 frames
per second maximized on my 1024x768 screen. Your program does about 7
but it is doing considerably more processing. You might see if you can
adapt this framework to your program.
One other thing, I'm running the newer beta version of 1.6 and it will
give me a FlipSubregionBufferStrategy and that is faster than the
BlitSubregionBufferStrategy that you can get with the non-beta 1.6.
So my best guess is if you change to active rendering you can save the
time difference between drawing the BufferedImage to the graphics
context and the time it takes to flip buffers. Also you might get a few
microseconds increase by drawing to the back buffer that is in fast
video memory versus drawing to the BufferedImage that is in regular memory.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
public class test2 extends Canvas implements Runnable {
volatile Thread thread;
volatile BufferStrategy bs;
double angle;
long then = System.currentTimeMillis();
int n;
double rate;
public test2() {
setIgnoreRepaint(true);
setPreferredSize(new Dimension(400,300));
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
if (bs == null) {
createBufferStrategy(2);
bs = getBufferStrategy();
System.out.println(bs);
}
}
});
}
public void start() {
then = System.currentTimeMillis();
thread = new Thread(this);
thread.start();
}
public void stop() {
thread.interrupt();
}
public void run() {
while (!thread.interrupted()) {
render();
}
}
public void render() {
do {
do {
int w = getWidth();
int h = getHeight();
Graphics2D g = (Graphics2D)bs.getDrawGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0,0,w,h);
AffineTransform at = g.getTransform();
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) {
long now = System.currentTimeMillis();
long time = now - then;
then = now;
rate = 100000.0 / time;
}
g.setTransform(at);
g.setColor(Color.RED);
g.drawString(String.format("%3.1f",rate),10,10);
g.dispose();
} while (bs.contentsRestored()) ;
bs.show();
} while (bs.contentsLost()) ;
}
public static void main(String[] args) {
final test2 t2 = new test2();
final Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent we) {
t2.start();
}
public void windowClosing(WindowEvent we) {
t2.stop();
f.dispose();
}
});
f.add(t2,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
--
Knute Johnson
email s/knute/nospam/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access