• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

paint() and performance

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,

I have an app that draws lines of text on a canvas component. Canvas is put into a scrollpane and vertical scrolling is carried out when up or down keys are pressed. Text is first read from a text file and everything works perfectly for as long as the text file size is relatively small. When I try to use a text file that's about 150Kb in size (~21400 words / ~140700 chars), the program starts to work unpleasantly slowly, and scrolling doesn't go smoothly any more and becomes rather jerky.
Windows Task Manager says that about 20Mb of memory is used by my program when it works with a large file like that. That seems like _A LOT_...
My paint method isn't really overloaded with calculations. It just retrieves words and corresponding font sizes from ArrayLists and draws them, calculating coordinates for every word and performing word wrapping and breaking if necessary as well.
It seems that the paint method works through the whole canvas drawing the text on it every time it's called. The canvas size gets really large when there's such a lot of text being drawn, but the scrollpane's viewport size is small, so we don't need to repaint the whole canvas as we don't see the most of it.
Is there any workaround here? Any common approach in cases like this that everybody knows but me?... ))
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you limit yourself to the AWT (vis–a–vis Swing) then I would make an offscreen buffer (an image), and render the text to it, and paint it in the paint method. This should relieve the paint method of re–calculating everything with each small movement of the scrollbar. Also, I would look into the LineBreakMeasurer class, possibly in conjunction with the TextLayout class to ease the calculation burden.
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Craig, again, thanks a lot! Will try that.
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, the double-buffering seems a good way out here except for one thing: the offscreen image requires its size to be set when the image is created, but I'm only able to calculate the size when all text has been drawn onto it.
I still can't find ways to update the image size once it was created...
[ September 14, 2004: Message edited by: Dmitry Danilov ]
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two possibilities come to mind:
1 - you could make a dry run through the layout, calculating the layout but not drawing anything, and then use the dimensions from this to create your offscreen image.
2 - you could render the text to the image and then use the final dimensions of the layout/rendering process to clip the image
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

2 - you could render the text to the image and then use the final dimensions of the layout/rendering process to clip the image



Hmmm, in this case I have to intitially set the image size to always be bigger than the final dimensions, and how do I know how big they'll be?...
Please, correct me if I'm wrong...
[ September 15, 2004: Message edited by: Dmitry Danilov ]
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do I know how big they'll be?...
If you know the width that you want for the image, perhaps the width of its parent viewport, you could set a much larger height to be sure to accommodate the data. To get an idea of this you could try to estimate the length according to file size based on some tests, basically noticing what size you end up with for smaller files and trying to find some rough correlation to file size and final image dimensions. This assumes that these files are somewhat similiar in their distribution of attributes... Otherwise, if you're out it the wilderness with respect to estimation of size you could try using an arbitrary, big number for the height.
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That might be an idea....
Thanks mate!
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wondering if I could use the repaint(int x, int y, int width, int height) method of the Component class to repaint only the viewport sized rectangle of the canvas and not the entire one. Would that help save the precious resourses? Or do I misunderstand the applicability of the method?
 
reply
    Bookmark Topic Watch Topic
  • New Topic