Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Displaying blank JLabel Icon.

Reply
Thread Tools

Displaying blank JLabel Icon.

 
 
Ken Adams
Guest
Posts: n/a
 
      11-18-2004
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



 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      11-18-2004
Ken Adams <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
Ken Adams
Guest
Posts: n/a
 
      11-18-2004

"Chris Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
> Ken Adams <(E-Mail Removed)> wrote:
>> 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


How exactly do you go about making this transparent BufferedImage?


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      11-18-2004
Ken Adams <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Ken Adams
Guest
Posts: n/a
 
      11-18-2004

"Chris Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
> Ken Adams <(E-Mail Removed)> wrote:
>> 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


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


 
Reply With Quote
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      11-18-2004
Ken Adams wrote:
> "Chris Smith" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed).. .
>
>>Ken Adams <(E-Mail Removed)> wrote:
>>
>>>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.
>>

>
> 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
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      11-18-2004
Ken Adams <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Ken Adams
Guest
Posts: n/a
 
      11-19-2004

"Chris Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
> Ken Adams <(E-Mail Removed)> wrote:
>> 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


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)
{ }
}


 
Reply With Quote
 
Ken Adams
Guest
Posts: n/a
 
      11-19-2004

"Ken Adams" <(E-Mail Removed)> wrote in message
news:EUgnd.340$y72.98@clgrps12...
>
> "Chris Smith" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed).. .
>> Ken Adams <(E-Mail Removed)> wrote:
>>> 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

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




 
Reply With Quote
 
Andrei Kouznetsov
Guest
Posts: n/a
 
      11-19-2004
> 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)

--
Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
blank CD-R and blank DVD-R not recognized by Vista 64 Ultimate =?Utf-8?B?R3JlZyBLaXJrcGF0cmljaw==?= Windows 64bit 13 11-07-2007 12:23 PM
how to write in a Jlabel from a new class ? Damiano Michael Java 2 01-03-2004 08:19 AM
Can JLabel have 2 rows of text? Rpng123 Java 6 10-10-2003 09:35 PM
JLabel behaves differently printing than in GUI Cindy Huyser Java 1 08-07-2003 09:53 PM



Advertisments