In a jPanel, I'm displaying a 1203x1203 background that I rotate every 30ms. Problem is, Swing is known to be a slow library, and I assume rotating a huge picture at a quick rate would be quite resources demanding. But I've a powerful PC, and even if on mine it runs fine, I wonder if on slower machines it will be as fluid.
public static BufferedImage createRotatedCopy(BufferedImage img, double rotation) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage rot = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
double theta = 2 * Math.PI * rotation;
AffineTransform xform = new AffineTransform();
xform.translate(0.5 * h, 0.5 * w);
xform.rotate(theta);
xform.translate(-0.5 * w, -0.5 * h);
Graphics2D g = (Graphics2D) rot.createGraphics();
g.drawImage(img, xform, null);
g.dispose();
return rot;
}
I wonder first if there is any way to optimize it. What I can do, is to use a smaller picture, apply the rotation and resize it. But I don't know if it would be more efficient.
And the rotation callback is:
exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
bgAngle += 0.001;
bgAngle = (bgAngle >= 1) ? 0 : bgAngle;
rotBg = Menu.createRotatedCopy(toBufferedImage(bg.getImage()),
bgAngle);
repaint();
}
}, 0, 30, TimeUnit.MILLISECONDS);
Do you see anything that could be optimized? Or outright another way to do what I want?