• 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

better performance with a custom jtextfield

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I originally posted this on the java.sun forums, http://forums.sun.com/thread.jspa?threadID=5361214&tstart=45, but was looking for a bit more help. Basically I'm drawing a custom jtextfield with a gradient background. The problem comes when text is actually being put into the textfield the cpu jumps and the performance is awful. Im assuming its from the textfield being redrawn with the gradient each time a letter is entered. I dont know the proper way to get around this, and in my previous post on the java.sun forums they had mentioned using a bufferedImage to draw onto, however I fairly new to swing and do not know how to implement that with a jtextfield...here is the code:


If anyone could show me a better way of doing this, or simply how to solve this problem using a bufferedImage i would appreciate it.

Sincerely,
Christopher Dancy
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just a thought..
JPanel (Borderlayout, or gridlayout(1,1)), with your gradient background
JTextfield, transparent, added to the JPanel.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any reaosn to use Graphics.create() and dispose(); just draw on the Graphics object you're given. That will save some time.

You could also cache the GradientPaint object -- i.e., save it in a member variable. You probably want to discard it if the component is resized.
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest: The amount of time saved from those things is marginal compared to what is happening. I've tried What Michael Dunn proposed and that seems to work good for now, though it feels very hackish. Either way its much much better than the other way.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Dunn wrote:just a thought..
JPanel (Borderlayout, or gridlayout(1,1)), with your gradient background
JTextfield, transparent, added to the JPanel.



Along the same lines, you could cache the gradient in a BufferedImage (updated only when the field is resized) and draw it with drawImage().

I guess this works better if the field is not transparent.
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've solved the issue and here is how I did it using a BufferedImage:

 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Dancy wrote:I've solved the issue and here is how I did it using a BufferedImage



I'm glad my suggestion worked out for you.
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I didn't say it before... thank you for the tip.
 
reply
    Bookmark Topic Watch Topic
  • New Topic