Displaying blank JLabel Icon.

K

Ken Adams

How do you go about displaying a blank(invisible) JLabel Icon. Basically I
have a JLabel that represents a playing card, and when the player isin't
playing I want to set his Icon to be clear, I need to actually set an Icon
because I don't want the panel it's in to be resized. Any suggestions. I
suppose I got probably just setVisible(false) then true when I need it again
but I would prefer another way. Thanks
 
C

Chris Smith

Ken Adams said:
How do you go about displaying a blank(invisible) JLabel Icon. Basically I
have a JLabel that represents a playing card, and when the player isin't
playing I want to set his Icon to be clear, I need to actually set an Icon
because I don't want the panel it's in to be resized. Any suggestions. I
suppose I got probably just setVisible(false) then true when I need it again
but I would prefer another way. Thanks

If you want to use an image that just doesn't appear, you can create a
new transparent BufferedImage, and construct an ImageIcon from that.
Or, a little more work but less resource-hungry, you could define a
simple Icon implementation that knows its width and height but does
nothing when asked to paint itself.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
K

Ken Adams

Chris Smith said:
If you want to use an image that just doesn't appear, you can create a
new transparent BufferedImage, and construct an ImageIcon from that.
Or, a little more work but less resource-hungry, you could define a
simple Icon implementation that knows its width and height but does
nothing when asked to paint itself.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

How exactly do you go about making this transparent BufferedImage?
 
C

Chris Smith

Ken Adams said:
How exactly do you go about making this transparent BufferedImage?

IIRC, if you create a BufferedImage with any color model that includes
transparency, it will be transparent to begin with.

On further reflection, though, my second suggestion was the better one.
This useless image data could get fairly large.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
K

Ken Adams

Chris Smith said:
IIRC, if you create a BufferedImage with any color model that includes
transparency, it will be transparent to begin with.

On further reflection, though, my second suggestion was the better one.
This useless image data could get fairly large.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

So to do the second suggestion, Should I create a class the implements Icon?
If so, How do I override paint to do nothing yet the size of the image takes
up any panel it is in?

Thanks
 
B

Babu Kalakrishnan

Ken said:
So to do the second suggestion, Should I create a class the implements Icon?
If so, How do I override paint to do nothing yet the size of the image takes
up any panel it is in?

Once you start writing the class that implements the Icon interface, it
should be self-evident.

Hint : you would need to provide implementations for two methods
getIconWidth() and getIconHeight() to get the compiler to compile
the class successfully.

BK
 
C

Chris Smith

Ken Adams said:
So to do the second suggestion, Should I create a class the implements Icon?
Yes.

If so, How do I override paint to do nothing yet the size of the image takes
up any panel it is in?

Please let me know if I'm misunderstanding you.

I was under the impression that the point was to create a transparent
icon of a given size. Sizes in Java are calculated bottom-up, not top-
down, so there's no way to take up the entire size of a panel, because
the size of the panel *depends* on the size of the icon. You want to
make this icon the same size as the visible one that you would otherwise
be displaying.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
K

Ken Adams

Chris Smith said:
Please let me know if I'm misunderstanding you.

I was under the impression that the point was to create a transparent
icon of a given size. Sizes in Java are calculated bottom-up, not top-
down, so there's no way to take up the entire size of a panel, because
the size of the panel *depends* on the size of the icon. You want to
make this icon the same size as the visible one that you would otherwise
be displaying.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Ok, I am sure you are getting sick of my questions to a seemingly easy
problem. But I got this for code. What more needs to be done, and yes I do
want to set the icon the size of the icon that was previously there.I set
the label by calling label.setIcon(new BlankIcon(80,100)) but it essential
does the same as calling label.setIcon(null). and that isin't what I want.
Thanks for you patients.

public class BlankIcon implements Icon
{
private int width;
private int height;
public BlankIcon(int width,int height)
{
}
public int getIconHeight()
{
return width;
}
public int getIconWidth()
{
return height;
}
public void paintIcon(Component arg0, Graphics arg1, int arg2, int arg3)
{ }
}
 
K

Ken Adams

Ken Adams said:
Ok, I am sure you are getting sick of my questions to a seemingly easy
problem. But I got this for code. What more needs to be done, and yes I do
want to set the icon the size of the icon that was previously there.I set
the label by calling label.setIcon(new BlankIcon(80,100)) but it essential
does the same as calling label.setIcon(null). and that isin't what I want.
Thanks for you patients.

public class BlankIcon implements Icon
{
private int width;
private int height;
public BlankIcon(int width,int height)
{
}
public int getIconHeight()
{
return width;
}
public int getIconWidth()
{
return height;
}
public void paintIcon(Component arg0, Graphics arg1, int arg2, int
arg3)
{ }
}
Ignore the last post. I made a simple stupid mistake, If you noticed I don't
assign the values in the constructor to my local values. (HEHE) So now it
works great. Thanks alot for the help though, this is exactly what I was
looking for.

:)
 
A

Andrei Kouznetsov

How do you go about displaying a blank(invisible) JLabel Icon. Basically I
have a JLabel that represents a playing card, and when the player isin't
playing I want to set his Icon to be clear, I need to actually set an Icon
because I don't want the panel it's in to be resized. Any suggestions. I
suppose I got probably just setVisible(false) then true when I need it again
but I would prefer another way. Thanks

what you really need is implementation of Icon with possibility to switch
paint image or not:
e.g. setImagePainted(boolean)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top