• 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

Problem with graphics2d and JLable while overloading paintComponent() method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello i have a problem at extending a JLabel. What i want to do is to extend a jlabel to look like a 3d component. That is why i extend the jlabel and overload the method paintComponent().
To get into the problem i will list the code so someone can help. Thank you in advance.
Here is the code:

 
Elvis Ligu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with this code is that, it paints well when it shows for the first time. But, in case you resize the label (in this case the jframe in which it is contained, when program runs) it does not paint well. In this case i paint half of the label with one graphics2d and the other half with an other graphics2d, this way i gave the label a 3d look. When you reseize the label, it changes the colors. For example at tha last line of main method i tell to jlabel to paint a itself as a horizontal cylinder where the white color must shows in top and bottom of it and the blue color in center (i do this using a gradient panel). When you resize it, it interchanges these colors but keeps it 3d look. To be able to see what real problem is you must run the application and whatch the effect it does create.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I havent gone through the code carefully, or tried it myself (too early in the morning for me, and I haven't had my coffee yet), but try making super.paintComponent(grphcs); the first line of your paintComponent()
 
Elvis Ligu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your quick reply. I tried it, and when I do this the only changes it makes is that label's text doesn't show at all. I think that is right, because after calling super.paintComponent() which paints the text, the label's region is painted again so that is why text is gone.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i paint half of the label with one graphics2d and the other half with an other graphics2d


You do?

And you need to dispose() all Graphics objects that you create.

The code looks horribly convoluted, with multiple and chained calls to Color#darker() / brighter() at every repaint. Those colors should be cached, and if there is a possibility of the background being changed during the run of the program, register a PropertyChangeListener for the "background" property and renew the cache when that changes. The Paints could also be cached and renewed via a ComponentListener's componentResized(...)

Finally, I don't see any rendering that couldn't be done with a single LinearGradientPaint instead of two GradientPaints.


edit: Forgot to add, that a custom appearance should be managed by a custom UI delegate -- a LabelUI extending BasicLabelUI or (for massive customization) LabelUI -- not by extending the component class.
 
Elvis Ligu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the line if(grp == null) is only called for the first time when the user does not provide any color. So there is not a problem there. Yes there is a posibility to do this job with only one lineargradientpanel and i did not know it. I just saw some examples on internet, so i am going to revise the code and to figure out how to avoid the problem. However, the use of two gradientpaint does not explain why this problem occurs. You can see what is the problem if you run the code. Thank you for your reply.
 
Elvis Ligu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I fixed it! actually I dropped GradientPaint and I use LinearGradientPaint as you adviced. So thank you very mutch.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic