Displaying blank JLabel Icon.

Discussion in 'Java' started by Ken Adams, Nov 18, 2004.

  1. Ken Adams

    Ken Adams Guest

    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
     
    Ken Adams, Nov 18, 2004
    #1
    1. Advertising

  2. Ken Adams

    Chris Smith Guest

    Ken Adams <> 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
     
    Chris Smith, Nov 18, 2004
    #2
    1. Advertising

  3. Ken Adams

    Ken Adams Guest

    "Chris Smith" <> wrote in message
    news:...
    > Ken Adams <> 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?
     
    Ken Adams, Nov 18, 2004
    #3
  4. Ken Adams

    Chris Smith Guest

    Ken Adams <> 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
     
    Chris Smith, Nov 18, 2004
    #4
  5. Ken Adams

    Ken Adams Guest

    "Chris Smith" <> wrote in message
    news:...
    > Ken Adams <> 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
     
    Ken Adams, Nov 18, 2004
    #5
  6. Ken Adams wrote:
    > "Chris Smith" <> wrote in message
    > news:...
    >
    >>Ken Adams <> 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
     
    Babu Kalakrishnan, Nov 18, 2004
    #6
  7. Ken Adams

    Chris Smith Guest

    Ken Adams <> 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
     
    Chris Smith, Nov 18, 2004
    #7
  8. Ken Adams

    Ken Adams Guest

    "Chris Smith" <> wrote in message
    news:...
    > Ken Adams <> 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)
    { }
    }
     
    Ken Adams, Nov 19, 2004
    #8
  9. Ken Adams

    Ken Adams Guest

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

    :)
     
    Ken Adams, Nov 19, 2004
    #9
  10. > 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
     
    Andrei Kouznetsov, Nov 19, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miro Durcik

    app.rc resource's icon to .net Icon

    Miro Durcik, Oct 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,432
    Miro Durcik
    Oct 9, 2004
  2. Cindy Huyser
    Replies:
    1
    Views:
    537
    Cindy Huyser
    Aug 7, 2003
  3. Dan Polansky
    Replies:
    3
    Views:
    862
    Larry Barowski
    Feb 12, 2007
  4. 2.pl
    Replies:
    2
    Views:
    761
  5. Bodi
    Replies:
    1
    Views:
    314
    Lasse Reichstein Nielsen
    Nov 18, 2003
Loading...

Share This Page