• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

resizing an icon in gridlayout

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got an icon that is being displayed with the edges cropped by GridLayout(). I know GridLayout specifies that everything will be the same size, but can this be overridden in an individual compartment?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The GridLayout uses the size of the largest component, so I'm not sure what the problem is.
 
Andy Ranangnai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't really make sense to me, GridLayout creates a grid of equal sized boxes, so the size is based on how big the window is, right?

I know I could just set the frame to be larger but I would prefer to change the ImageIcon's size, I don't want this to be a giant window on the screen. I've been googling around for 'set size ImageIcon java' but have come up with little. I could use paintIcon() I guess but I was hoping there was another way as that would get pretty messy. I'm digging through the API now trying to see if there's a setSize() method inherited from component or something.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what do you expect? Suppose you have a 2x2 grid: did you expect the top left cell (let's say) to be bigger than the others? Then it wouldn't be a grid, would it?

A more reasonable expectation (remember, I don't know what you expect) would be for the layout manager to scale the icon down to fit in the box. But then setting the size of the cell wouldn't be what you would try if you expected that.

So maybe you could draw a picture of what you want. I can't figure out what you mean from your posts so far.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GridBagLayout? Oh, that link doesn't work at the moment. Try this one.
 
Andy Ranangnai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No no, you misunderstand. I'm trying to set the size of the ImageIcon to make it SMALLER than what it's coming out right now, so that it will fit in the gridlayout. Let me re-phrase my question

How can I set (or re-setting would be nice to know too) the size of an ImageIcon? Is this even possible? It seems really simple and like it should be possible but I've been having a really hard time finding any info on it.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Image.getScaledInstance
 
Andy Ranangnai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a getScaledInstance() method in class Image, but when I tried calling it I got thrown a 'cannot find symbol' error.

Here's how it appears in the API in class Image:


And here's how I tried to use it (int hints is used for resampling rates, I just threw a 5 into hints, figured I would adjust it as needed):
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

could someone tell me what the heck is 'int hints'



Yes, that is what the API is for. It will tell you the allowed values.

I found a getScaledInstance() method in class Image, but when I tried calling it I got thrown a 'cannot find symbol' erro.


If you found the method in the "Image" class (like I suggested), then why are you trying to use the method on an "Icon"?
 
Andy Ranangnai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because my question wasn't 'how to I resize an Image' it was 'how do I resize an Icon' so I was trying to give you the benefit of the doubt having read what I asked. If your not going to read it, then why be rude and act like I'm dumb for not using an Image? What ARE you suggesting?

I even tried plugging the image into an Image object, but got the same error. Here's how I did it:


It threw me the same error, I'm just not getting how to use that method.

Also, the API doesn't specify allowed values for 'int hints'. I realized it was probably explained further down and edited my post soon after posting it, but it only says that it's for resampling, it doesn't specify how to use it. I started with 0 and have tried a few others but it still doesn't work. If my questions are so stupid and beneath you that they warrant rudeness, then feel free to leave the thread.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my question wasn't 'how to I resize an Image' it was 'how do I resize an Icon



There is no method to scale an Icon which is why I pointed you to the Image class. Since you where using an "ImageIcon" I thought you would would be able to:

a) read in the image
b) scale the image
c) create an ImageIcon from the Image

I didn't think I needed to spell it out that explicitly.

Also, the API doesn't specify allowed values for 'int hints'.



Yes it does. Don't blame me if you don't know how to read the API or use the information provided there. Of course even if you don't understand the API did you think to do a Google search of "getScaledInstance" to see if you could find other examples. Its time you learn to do some basic problem solving on your own. Don't expect us to spoon feed you the code.

If my questions are so stupid and beneath you



Its your attitude that is beneath me. I'm sorry if forcing you to read the API is beneath you. I expect that when you ask a question you can do a little thinking on your own without us having to write the code for you. I also expect that you can read the API. You can't program if you can't read the API.

Also it would help if you knew how to post a proper question the first time so we don't have to waste time understanding what you are aking. Two of us where questioning why the GridLayout was causing a problem. Basically the GridLayout information was irrelevant to the question and led us on a goose chase. Both Paul and I are well experienced and answering questions and can usually read between the line, but I guess you know better than we do, so you get your wish and I won't bother helping you.
 
Andy Ranangnai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look, I'm on the sun API spec for Image, I swear I see nothing to specify what 'int hints' is beyond that it has to do with resampling. All I've been asking is how to set the size of an Icon, not cause drama. Your attitude pissed me off because you weren't giving me anything to work with, you literally just posted one line of code the first time, then acted like I was dumb for not assuming the rest.

Of course I don't expect things to be spoon fed to me, I thought I made it clear that I had been going through the spec and that I wasn't just running to the forums the moment I didn't understand something. I like it here and people have 'usually' been very friendly and understanding when a concept doesn't immediately click. I try not to flood this forum with mundane questions I haven't bothered to research.

But really, if your going to be rude then why answer at all? I'm a noob man, I'm on a forum asking questions because I don't have any real life friends that are into programming and I can't get something on my own.

Anyway, thanks for the suggestion to read the Image into the Icon, that's all I needed to know I got it to work, I just didn't follow what you were suggesting.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are unbelievable. I just gave you the answer and you are still insulting me!!!

Your attitude (not mine) dictated the tone of the posting. All I did was make suggestions to solve the problem.

I should not have to spoon feed the answer to you.

Your statement that the API did not have the "hint" information was blatantly wrong, so I told you to reread the API for the information. If that is "rude" to you, well I can't help you. Good luck getting help in the future. Others would just have ignored you after being insulted.
 
Andy Ranangnai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wasn't insulting you with that post, I was explaining myself. And I told you I appreciated you clarifying it for me. I'm finished arguing, it's getting pointless by the moment, I just wanted to say thanks for what help you did offer, so thank you.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about, instead of using a JLabel with an ImageIcon, you use a JPanel which paints the image in its paintComponent method:
 
Andy Ranangnai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea, I've been avoiding it but I think I'm gonna have to get more comfortable with paintcomponent objects. I've taken 2 semesters of Java now and the professor spent maybe 2 or 3 class meetings talking about any sort of GUI design, so it's a really weak area for me but something I feel is really important and deserved a lot more time.

I still have the book and it's pretty comprehensive, I'm gonna sit down and figure it out. It seems like paintcomponent gives you the sort of control over images that I like to have. Thanks for the suggestion.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With the Graphics object you get (which is actually a Graphics2D object nearly all of the time) you can do any painting you want; not just images but also lines, rectangles, curves, etc. Just remind yourself to include the super.paintComponent(g) at the start, or the component will not paint itself in the regular way. That includes clearing any previously drawn content.
 
Bring out your dead! Or a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic