Currently, I have a full screen JFrame. Within that JFrame, is a panel called MainPanel which is 1280 by 640, a portion of the full screen game. The MainPanel is where my game is rendered to. Is there any way to scale this MainPanel to fit JFrame without having to adjust all the component sizes of sprites and such? I'm thinking of rendering MainPanel to an image and drawing it over JFrame, but I do not know how to go about this.
Thanks.
By default, the layout manager of a JFrame is BorderLayout, and adding a JPanel to it without any arguments will place it in CENTER, which will automatically scale to the size of the JFrame. It will ignore any size you have given the JPanel.
Related
I've been using Java Swing for quite some time now and I never found a solution to this problem. When I create a JFrame the window surrounding it is actually smaller than the frame. In the included picture below my JFrame size is 800x600. The 2 white lines crosses at the center of the frame, 400,300. As you can see they are not at the center of the window. If I stretch the window right and down I can see some of the black background of the frame was hidden. When the black background is revealed you can see the the lines do indeed cross at the center (2nd picture).
Why is it working like that? Anything I can do to solve this problem? Im making a game where the playable character is in the center of the screen so this causes me a lot of problem. The 1st image is larger because i've left the code in the background. As we can see it's a standard JFrame creation.
Not centered because part of the frame is hidden:
centered when frame is fully revealed:
my JFrame size is 800x600
You are doing things backward.
The frame has decorations (ie. the title bar and borders). The panel where you do the painting is added to the frame, so therefore it will be less than the size of the frame.
The proper approach is to override the getPreferredSize() method of the JPanel where you do the custom painting to return the desired size of the panel.
Then you add the panel to the frame you invoke the pack() method on the frame. Now the frame will be sized slightly larger (to fit the complete panel and the frame decorations) and your painting will be accurate.
I have a JScrollPane whose viewport is a JPanel. The JPanel contains smaller JPanels that take up the entire viewport's width, and the big JPanel is set to a FlowLayout. The user should be able to add as many JPanels as they want (well, up to 200 for my purposes), and they should be able to scroll down the JScrollPane to see everything they have added. Basically I'm just trying to make the JScrollPane grow. I'm calculating what I thought the JPanel's height should be like this:
Dimension dimension = new Dimension(width, smallPanel.height * totalPanels
+ ((FlowLayout) getLayout()).getVGap * totalPanels);
setPreferredSize(dimension);
And it mostly works, but as you add more, it starts cutting the smaller JPanels off, and the bottom panel eventually isn't shown. Is there a way that I can determine the size of the JPanel for the viewport so that I wouldn't have to calculate its dimensions with variables? Like pack() does for JFrame? Or do I need to keep guessing and checking?
Thanks
I am designing a large form with the Eclipse WindowBuilder plugin. The class extends JPanel and uses GridBagLayout as its LayoutManager.
Now my panel has become vertically larger than my screen size and I cannot make the JPanel any larger by dragging its borders.
How can I vertically extend the JPanel so it's still usabe with WindowBuilder?
Now my panel has become vertically larger than my screen size and I
cannot make the JPanel any larger by dragging its borders.
How can I vertically extend the JPanel so it's still usabe with
WindowBuilder?
put JPanel to the JScrollPane
If I set the size of JPanel first, then set the panel to be the content pane of an unsized frame, the frame will be squeezed into a very small rectangle in the top-left corner of the screen.
But if I set the size of frame first and then set the panel to be its content pane the frame will be properly drawn.
Why does this happen and how do I solve this if I really want to specify the size of JPanel rather than JFrame?
You should tell the frame to pack itself when it is first shown:
frame.pack();
frame.setVisible(true);
This will cause the frame to adopt a size that suits its contents.
I have a JInternal Frame and I want to draw a circle(using 2Dgraphics) in it and make it flexible. I mean when I change the size of frame the circle become smaller or in making frame larger circle also become larger. Can somebody help?
You would draw in the paintComponent method of a JPanel or JComponent that is held in the JInternalFrame's contentPane, same as you would draw in any other JPanel. I'd get the dimensions of the JPanel at the start of the paintComponent method and use those values to tell how big to draw the circle.
Also, if you add the JPanel directly to the JInternalFrame's contentPane, it will be added by default BorderLayout.CENTER, and so when the JInternalFrame changes size, the JPanel also changes size, it's paintComponent will be called by the JVM, and the new drawing will be resized automatically.
add a WindowListener to the jInteralFrame and redraw whenever the size changes