Java Forum / General / February 2008
Drawing Icons
Jason Cavett - 28 Feb 2008 15:30 GMT I am working on a tree cell renderer (currently, extends DefaultTreeCellRenderer) and I want to be able to "draw" the icons as I need them. Here's an explanation of what I mean.
If the tree is displaying a node and the node is in its default state, I want this: [Icon]
If the tree is displaying a node and the node is "opened":
> [Icon] If the tree is displaying a node and the node is "opened" and has a note attached:
> [Icon{n}] Basically, I want to be able to draw what I need (on the fly) so I can build up the icon. I do realize I could just create every possible icon and display the right one depending on the state of the underlying node, but that seems to be very inefficient, especially as I add more nodes.
Hopefully this message makes sense. Can anybody provide any insight?
Larry A Barowski - 28 Feb 2008 18:07 GMT >I am working on a tree cell renderer (currently, extends > DefaultTreeCellRenderer) and I want to be able to "draw" the icons as > I need them. Create your own icon class that implements javax.swing.Icon. It can draw itself as a composition of sub-images, or entirely from scratch using drawing primitives.
Jason Cavett - 28 Feb 2008 20:08 GMT On Feb 28, 1:07 pm, "Larry A Barowski" <ThisisLarrybarAtEngDotAuburnDotLearninginstitution> wrote:
> >I am working on a tree cell renderer (currently, extends > > DefaultTreeCellRenderer) and I want to be able to "draw" the icons as [quoted text clipped - 3 lines] > It can draw itself as a composition of sub-images, or entirely > from scratch using drawing primitives. Great idea, thank you.
I have this pretty much working except for one minor problem...
In order to correctly draw the icon, it appears that I need to know the size of the icon before I draw it. However, I unfortunately do not know the width of the icon until later. Is there any way that I can "draw" the icon and then resize it so the entire thing is viewable. (Currently, I set the size to 16x16 px, but in some cases it will need to be 32x16 px.)
Thanks again for your help, Larry.
Larry A Barowski - 28 Feb 2008 21:11 GMT > In order to correctly draw the icon, it appears that I need to know > the size of the icon before I draw it. However, I unfortunately do > not know the width of the icon until later. Is there any way that I > can "draw" the icon and then resize it so the entire thing is > viewable. (Currently, I set the size to 16x16 px, but in some cases > it will need to be 32x16 px.) If you are constructing the icon in getTreeCellRendererComponent(), it should have all the information it needs to compute its width in the constructor. If it is constructed ahead of time, you can add a "configure()" method that will adjust the width and call that from getTreeCellRendererComponent(). Depending on the specifics, you may need to "go through the motions" of painting the icon in order to determine the size, and if the size is dependent on font size, etc., do the rest of the renderer component configuration first, then use a Graphics object from getGraphics() for your computations. That may not -exactly- match the Graphics that will be used for painting because of antialiasing, etc., so if text metrics figure into the width, you should probably make the width one pixel more than the computed value.
Jason Cavett - 28 Feb 2008 21:12 GMT On Feb 28, 1:07 pm, "Larry A Barowski" <ThisisLarrybarAtEngDotAuburnDotLearninginstitution> wrote:
> >I am working on a tree cell renderer (currently, extends > > DefaultTreeCellRenderer) and I want to be able to "draw" the icons as [quoted text clipped - 3 lines] > It can draw itself as a composition of sub-images, or entirely > from scratch using drawing primitives. In addition, I have a weird thing going on with the icons which I have not able to figure out. They draw themselves (and it looks good, too). Except...the icons are BEHIND the text of the tree nodes.
(Best I can do with text) [TIrCeOeNN]ode Text
Anyway...I'm working on that problem right now. I figure I forgot to do something simple.
Larry A Barowski - 29 Feb 2008 05:07 GMT > In addition, I have a weird thing going on with the icons which I have > not able to figure out. They draw themselves (and it looks good, > too). Except...the icons are BEHIND the text of the tree nodes. What are your icons returning for getIconWidth() ? I'm guessing it is zero or much less than the required width.
Jason Cavett - 29 Feb 2008 14:57 GMT On Feb 29, 12:07 am, "Larry A Barowski" <ThisisLarrybarAtEngDotAuburnDotLearninginstitution> wrote:
> > In addition, I have a weird thing going on with the icons which I have > > not able to figure out. They draw themselves (and it looks good, [quoted text clipped - 3 lines] > I'm guessing it is zero or much less than the required > width. That's actually what's weird.
At the end of the paint function (that I implemented in my icon class) the icons are returning - IconWidth = 16 OR 32 (depends on which type of icon I construct), IconHeight = 16.
However, before the paint function occurs, IconWidth and IconHeight are both 0.
...so...
I tried to START with the IconWidth = 16 and the IconHeight = 16. Well, that does work. The text is now shifted over. Thank you. However, I'm now back to the problem where my icons can be different widths depending on the state of the node. I know you posted about it up above, but I'm not sure I understand.
Thanks again for your help.
Jason Cavett - 29 Feb 2008 16:26 GMT > On Feb 29, 12:07 am, "Larry A Barowski" > [quoted text clipped - 29 lines] > > Thanks again for your help. Alright. I got it.
Basically, I have to make sure all my "widths" (and height I suppose, although I'm forcing that to be = 16px) have to be calculated BEFORE the paintIcon(java.awt.Component, java.awt.Graphics, int, int) method.
Now, it works beautifully. Thanks again for your help.
Daniel Pitts - 28 Feb 2008 22:35 GMT > I am working on a tree cell renderer (currently, extends > DefaultTreeCellRenderer) and I want to be able to "draw" the icons as [quoted text clipped - 18 lines] > > Hopefully this message makes sense. Can anybody provide any insight? You can create an Icon for every state that node can have (which looks like three from here). You should create an image (for example a png image) for those states, and then use ImageIcon()
 Signature Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Jason Cavett - 28 Feb 2008 23:26 GMT On Feb 28, 5:35 pm, Daniel Pitts <newsgroup.spamfil...@virtualinfinity.net> wrote:
> > I am working on a tree cell renderer (currently, extends > > DefaultTreeCellRenderer) and I want to be able to "draw" the icons as [quoted text clipped - 25 lines] > -- > Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/> That's what I was doing originally. However, due to new requirements the number of icons are going to at least double. Additionally, since the application will (most likely) be skinned in the future, I don't want to have to make 6 times the amount of icons. It *should* be easier to just build each icon as I need it. And, it works (see my other posts), except for the fact that the text is overtop the icon within the JTree and I can't figure out how to shift it over. (This seeemingly happened automatically when I was using an ImageIcon.)
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|