File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Problem with graphics2d and JLable while overloading paintComponent() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with graphics2d and JLable while overloading paintComponent() method" Watch "Problem with graphics2d and JLable while overloading paintComponent() method" New topic
Author

Problem with graphics2d and JLable while overloading paintComponent() method

Elvis Ligu
Greenhorn

Joined: Aug 01, 2011
Posts: 5
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

Joined: Aug 01, 2011
Posts: 5
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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10255
    
    8

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()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Elvis Ligu
Greenhorn

Joined: Aug 01, 2011
Posts: 5
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.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4529
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Elvis Ligu
Greenhorn

Joined: Aug 01, 2011
Posts: 5
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

Joined: Aug 01, 2011
Posts: 5
Ok I fixed it! actually I dropped GradientPaint and I use LinearGradientPaint as you adviced. So thank you very mutch.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with graphics2d and JLable while overloading paintComponent() method