Forums Register Login

Sizing an application to an image

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
Welcome to JavaRanch

We usually discuss that sort of thing on the Swing forum, so I shall move you there.
+Pie Number of slices to send: Send
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.



+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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!
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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 :
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2925 times.
Similar Threads
ScrollPane trouble
resize svg image
setting the size of 'JDialog' problem
ideas wanted on making this custom JTable cell renderer more efficient
Resizing TextArea()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:01:10.