I'm working on a simple drawing program where the user draws lines on the screen using the mouse.
When running the program, it eventually gets very 'laggy' when the drawing area gets filled with more and more lines.
I asked about it in SO and also done some research on Google. I understood that 'double buffering' might be the way to go to solve my problem.
I would like to explain my program, explain my idea of implementing double buffering in this program, and then ask two questions.
My program:
The program runs in an update loop that runs 25 times per second.
Each update cycle, the program checks if the mouse is pressed or not.
If it is pressed: A new Line
object is created, composed of the point where the mouse was at the last update-cycle, and the point where the mouse is at the current update cycle.
This very short Line
is added to a List
named lines
.
Every update-cycle the drawing area (a JPanel
) is repainted. Every repaint, it draws everything inside lines
.
The fact that the more the user has drawn, the more the program has to display every update-cycle, is what causes the lagging.
How I'm thinking of implementing double buffering:
Instead of drawing all of the lines each update-cycle, I will do this:
Each update loop, I will paint only the new lines created by the user this update cycle - to a BufferedImage
.
Every update-cycle, the drawing area will repaint, but will only display this BufferedImage
.
My questions:
Is this considered 'double buffering'?
Will I also need to repaint the
BufferedImage
every update cycle? If the answer is 'yes', than this cancels the entire point of the double-buffering in this case - which is to spare the program constantly drawing each object in a large list.
Thanks