# Rotating background using Swing: performance issues

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;
bgAngle);
repaint();
}
}, 0, 30, TimeUnit.MILLISECONDS);


Do you see anything that could be optimized? Or outright another way to do what I want?

Here is something you could do which would work for computers not fast enough to rotate the image every 30 ms. Instead of requesting a repaint every 30ms, define the angularVelocity (rads / second) and call a repaint every 30 ms or as fast as possible, if it can't make 30 ms. Then compute angleRad = angularVelocity * (System.currentTimeMillis() - startTime).