A program that animates circles is not drawing them fluidly once several hundred are drawn at once. It was suggested to use affine transformation to copy the shapes. This code, refactored to use graphics2D, works, but doesn’t cause any performance boost since it is still filling hundreds of ovals. How to use affinetransformation properly to fill a shape once and then copy/move it?
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
for (int i = 0; i < gameLogic.getParticleArrSize(); i++) {
Graphics2D g2 = (Graphics2D) g;
Color color = new Color(6,6,6);
Ellipse2D oval = new Ellipse2D.Double(
gameLogic.getParticleXCoor(i),
gameLogic.getParticleYCoor(i),
gameLogic.getParticleSize(i),
gameLogic.getParticleSize(i));
g2.setPaint(color);
g2.fill(oval);
g2.translate(15, 15);
g2.fill(oval);
}
}
The only way to know which is faster is to profile two or more implementations. As a concrete example, this kinetic model shows a slight advantage for Gradient mode, using
drawImage(), over Color mode, usingfill(), as seen in thepaintComponent()method ofDisplayPanel. In this context,AffineTransformis beneficial in pre-rendering the more complex gradient image.Addendum:
I doubt that
AffineTransformis the cure. Instead, move the object creation out of the loop, as @camickr suggests. In the example, note how anEnsembleonly needs oneEllipse2D; it usessetFrame()repeatedly. Also, eachParticlealready knows itsColor. Finally, observe how the example measures paint time.