Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

painting hundreds of Rectangles in JPanel  RSS feed

 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers!

I need some advise on how to make my paintComponent go faster.


I'm bulding a InDesign like program. A user can drag rectangles, change border, columns, colors on the rectangle. All rectangles have diffrent z-index so you can draw a rectangle over a rectangle.

Every time the mouse is moved over the JPanel that acts like a container for the rectangles 4 JSpinners is changed with the x, y , width and height for all selected rectangles.

If I have say 50-100 rectangles and drag them around the screen it is very slow. Is there someway of making drawing faster for 50-100 rectangles that is beeing dragged around the screen?
 
Campbell Ritchie
Marshal
Posts: 55724
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difficult. You probably can't make a great improvement.

Are you using AffineTransforms or anything similar? Those are a lot slower. I had a similar app once with lots of shapes, although I could only drag the most recent shape. I found that the drawing method for each shape took approx 1.6 microseconds, but it was longer on the first attempt. With an AffineTransform applied, it took about 53 microseconds on the first attempt, rather faster subsequently, but it was enough of a difference to cause an unpleasant delay and multiple edges whenever another window was dragged across my app. I tried several things, suggested by something in the Java tutorial probably here; look near the bottom for "performance" and the link they give there, like using Floats instead of Doubles for the 2D objects, and using opaque objects, but that didn't help.
Eventually in a fit of desperation, I put a javax.swing.Timer in my main JFrame class and set it to repaint the panel with the display on. A bit of trial and error suggested that the rendering problem were reduced to imperceptible with the Timer set to 333 (3 "ticks" per second).

Sorry I can't be any more helpful, but that is what I did when I had a similar problem.

CR
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mathias Nilsson:
Every time the mouse is moved over the JPanel that acts like a container for the rectangles 4 JSpinners is changed with the x, y , width and height for all selected rectangles.


Do you think updating the spinners is what's making things slow?

If I have say 50-100 rectangles and drag them around the screen it is very slow. Is there someway of making drawing faster for 50-100 rectangles that is beeing dragged around the screen?


Or do you think that actually drawing the rectangles is what's slow?

One trick here is you can paint the 50-100 rectangles onto something like a BufferedImage at the beginning of the drag, then paint only the image while the mouse is moving.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both!

I have commented out the Spinners and improvement are better but not enough. I thought of a buffered image to. I have 2 rulers for measure mm for x and y and grids for A4, A5 documents. I have put thoose in buffered image and they are only painted ones.

The problem as I see it to but all the rectangles in an buffered image is that when I use the drag tool and "mark" all rectangles it van be done. But when i release the drag rectangle all rectangles marked will be repainted and can be resized , reshaped and so on. So it think that an buffered image could make the paint slower in some circumstances.

Any other optimization to think about. I do not use double in Rectangle2D just plain Rectangle2D with rounded corners if that is what the user have choosen.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!