aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Sizing an application to an image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Sizing an application to an image" Watch "Sizing an application to an image" New topic
Author

Sizing an application to an image

Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
I'm not really a beginner, but this question seems so fundamental I thought I should post it here. I have an application using a JFrame. I create a JPanel which loads an image. It registers its preferred size to the dimensions of the image. Then I add the JPanel to the JFrame and pack it. Sometimes the JFrame resizes correctly to the size of the image, but sometimes it sets its size with extra space on the right and bottom. I can set the size explicitly to the size of the image (I know its dimensions, of course) and that always works, but I'd prefer it to be data driven. Here is the relevant code:

Application:

JPanel:


Any idea what's going on? TIA!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Welcome to JavaRanch

We usually discuss that sort of thing on the Swing forum, so I shall move you there.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Chris, from your code it seems that the contentpane of the JFrame has BorderLayout, and that you are adding the panel containing the image as BorderLayout.CENTER. Instinct tells me that this is the source of your problem. as I am pretty sure the amount of space allocated to Center depends on what is happening with NORTH, SOUTH, EAST,WEST.

I would experiment with different locations within BorderLayout, or with different layouts, to see if that makes a difference.

just a hunch.



Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
Thanks for the response. Since I have just one component, a JPanel, I didn't think the layout manager would matter. But I tried different layout managers anyway, just to see. It didn't help: I got the exact same results. Sometimes I have extra space at the right and bottom edges. I'll keep trying different things, but if anyone has some insight, I'd like to hear it.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
OK, I would tend to agree with what you say, if you only have one component. It's an interesting problem.

Just to summarize, you are absolutely certain the extra space is not part of the JPanel, but part of the content pane? Pack is suppose to deal with this. I thought.

The problem as you describe it should not be related to the fact you have an image on the JPanel. Because you have programatically set the preferred Dimension of the JPanel to the size of the image. Do you agree? So It should happen with any old JPanel, or not at all. This provides a direction for your detective work.

In border layout, NORTH, SOUTH, EAST, WEST take as much space as they can/need. CENTER tends to take what is left. So if you have more than one compponent, and your fram is not big enough, what is in Center will lose out, and it is center that will show extra space if any if the other four are taller/wider.

Have you tried BorderLayout.NORTH or SOUTH? I'm guessing now.

You may find this course on Layout useful, it's not part of the Java Tutorial

http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html

If you solve it, let us know.
Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
Okay, I seemed to have solved it. Instead of adding the JPanel to the JFrame, I did this:

But it bugs me that I shouldn't have needed to do that. I still don't know why JFrame wasn't sizing correctly around the single JPanel. I solved it, but it's not really satisfying.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I agree with you. It shouldn't have been necessary. Is your code in the first post complete? If you feel so inclined, make the complete code of the original available, here or by private message, I'm interested in playing with it a bit myself.

regards.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Please don't send code around by private message. Read this FAQ. Everybody should be able to see the code and have an opportunity to comment.
Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
Fred Hamilton wrote:If you feel so inclined, make the complete code of the original available. I'm interested in playing with it a bit myself.


I think I posted all the relevant portions of the code. The rest is just regular game code. Thanks for all the comments!
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Chris Nash wrote:
Fred Hamilton wrote:If you feel so inclined, make the complete code of the original available. I'm interested in playing with it a bit myself.


I think I posted all the relevant portions of the code. The rest is just regular game code. Thanks for all the comments!


OK, well I can't see anything wrong in what you posted, so who knows what the issue is. I'll keep an eye out for that sort of problem in my own projects.

cheers.
axel axek
Greenhorn

Joined: May 28, 2009
Posts: 20
have you tried....panelImg.setLocation(0,0) for the panel with the image?..or panelImg.setBounds(0,0,panelImg.getWidth(),panelImg.getHeight())???
seeing your code I realized that you have created a new class for your panel (titlePanel), so you could also try :
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sizing an application to an image