Drawing Icons

J

Jason Cavett

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":

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?
 
L

Larry A Barowski

Jason Cavett said:
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.
 
J

Jason Cavett

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.

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.
 
L

Larry A Barowski

Jason Cavett said:
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.
 
J

Jason Cavett

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.

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.
 
D

Daniel Pitts

Jason said:
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":

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

Jason Cavett

Jason said:
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":
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?

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

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.)
 
L

Larry A Barowski

Jason Cavett said:
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.
 
J

Jason Cavett

What are your icons returning for getIconWidth() ?
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.
 
J

Jason Cavett

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top