I have a JFrame that I am sticking a JPanel into to display an image, in this case 1024x1025. The problem I cannot seem to find an answer to is how to ensure my containing JFrame will display the whole image/JPanel. I have something close to this example:
ImagePanel view = new ImagePanel(); //extends JPanel
JFrame Container = new JFrame();
view.setSize(1024, 1025);
container = new JFrame(winTitle);
container.setLayout(new BorderLayout());
container.setSize(view.getSize());
container.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
container.add(view);
container.setVisible(true);
The problem with this obviously is that the JFrame is decorated by the underlying OS, so I have no idea how many pixels to make it larger than the size of the JPanel from system to system. As it is now, the image is cut off on the right and bottom by a handful of pixels each due to the setting of the JFrame to the same size as the JPanel. Is there a way to tell the JFrame to read the size of the Jpanel it is containing and make itself large enough to display the entire JPanel? Thanks!
EDIT
Setting preferred size on the image panels, then setting size (not preferred) on the container, then packing after the add gives me the behavior I was wanting. Can anyone explain whats going on here, I still am a bit fuzzy on order of operations by some swing components.
You should add the image panel to the contentPane of the JFrame. And you should just use pack() on the frame to make it take the appropriate size. It will take the size needed to accomodate the preferred size of its components.
After the container.add() and before the container.setVisible(), add a container.pack().
EDIT: You could also use Box.createVerticalStrut(int) and Box.createHorizontalStrut(int) in one of the sides of your BorderLayout. That should force the layout to leave room for the image.
If you are sure about the Image resolution, then use setPreferredSize(new Dimension(1024, 1025)) for the JFrame and also for the JPanel. Since JFrame uses BorderLayout by default, you dont need to specify container.setLayout(new BorderLayout());.
Related
I have a 800px600px flash app that has a fullscreen button. When doing fullscreen, the height gets fixed on my monitor, but there is a lot of "overflow", or "extra space" to the sides.
Is there a way to hide this extra space, other than adding a "window" movieclip on the of the movie?
(The blackened space is the movie area, I want to hide everything to the left and right of that area.)
EDIT: What I want is not to change the position/size of the stage/work area, but instead to get something that hides whatever is going on outside of the suposed window(800x600). Something like a "mask" to show only what is going on in that square.
check out the fullScreenSourceRect property of the Stage. That clips the stage to only what you want to show in full screen view. It may look pixelated though, depending on how small that source rect is.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#fullScreenSourceRect
Do not quite understand your question, but see if it helps you:
If you are using Event.RESIZE, you can change the size and position of objects in the x and y screen flash, so:
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.dispatchEvent(new Event(Event.RESIZE));
stage.addEventListener(Event.RESIZE, alterarPosicao);
function alterarPosicao(e:Event):void {
fundo_escuro.width = stage.stageWidth;
fundo_escuro.height = stage.stageHeight;
}
Thus, whatever the resolution of the project, the objects are aligned to the stage.
Hugs!
I ended up fixing the problem by just drawing a huge yellow (same color as background) square around the stage and leaving a empty area in the middle for the actual app to show.
It may be a bit "silly" solution, but it got the job done.
I have created a demo by swing and I want to show full text in textarea. I want to add two textarea into a scroll bar to show all component within a scroll bar. So to do that, I create a panel and then add two textarea on this one and then I add this panel into scroll bar. When I running demo, it show correctly and it also show correctly when I resize window larger but it have problem when I resize smaller screen. It does not wrap the text when resize the screen smaller (instead of appearing horizontal scroll bar).
Can you help me to automatically wrapped when resize the screen smaller.
Here my code:
JPanel gui = new JPanel(new GridLayout());
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
JTextArea area = new JTextArea("JTextArea on JPanel inside JScrollPane does not resize properly");
area.setWrapStyleWord(true);
area.setLineWrap(true);
gui.add(area);
JTextArea area1 = new JTextArea("JTextArea on JPanel inside JScrollPane does not resize properly");
area1.setWrapStyleWord(true);
area1.setLineWrap(true);
gui.add(area1);
gui.setBackground(Color.WHITE);
JScrollPane scroller = new JScrollPane(gui);
//scroller.setBorder(new EmptyBorder(2, 3, 2, 3));
JFrame f = new JFrame("Big Text Fields");
f.add(scroller,BorderLayout.CENTER);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
Just omit the scroll pane and make the text area a direct child of the container.
JTextArea does change its preferred size based on the text it contains. From the documentation:
java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane to match the functionality provided by java.awt.TextArea. JTextArea has a preferred size of what is needed to display all of the text, so that it functions properly inside of a JScrollPane. If the value for rows or columns is equal to zero, the preferred size along that axis is used for the viewport preferred size along the same axis.
I have a very long jpanel on a jscrollpane.
I want to be able to jump to a location in the panel.
is there a way to jump to a specific point in the pane?
Setting the horizontal scrollbar value is not enough
Is there a way to mark a position and jump to it (rather than giving X,Y)?
Thanks.
See scrollRectToVisible for scrolling.
Not sure what you mean with "mark a position" but if you want to scroll to for example a text field that is a (direct) child of the panel than you would do:
panel.scrollRectToVisible(textField.getBounds());
I am making a Class that extends JFrame and have my custom background image on it..
I have two problems..
1) I want my background image to stay fixed sized that covers whole screen when maximized. How can I do that?
2) I want to add a transparent button and panels on the frame that does not disturb my background. Is there any easy way to do that?
help will be greatly appreciated... thank you
Load the image into a BufferedImage.
Add a ComponentListener to determine when the frame is resized. Use the frame size to calculate scaling and call BufferedImage.getScaledImage(xScale, yScale) to obtain a scaled image.
In your class you should be overriding paintBackground() to do the painting. Just call g.drawImage(scaledImage, getWidth(), getHeight(), this) to paint the image.
Any components you add to the frame need to call setOpaque(false) so the background gets painted underneath them.
I have a movieclip with the mesurements of a rectangle. When the application is launched the movieclip is being scaled before placed on the stage as following.
menu.width = 400;
menu.scaleY = menu.scaleX;
this is a smaller size than the original.
the position of the movieclip at this moment is in the middle on the x and top of the stage on the y.
when i click iti would like to do a tween with tweenlite wich scales it to its original(bigger) width and height and position it in the center of the stage on x and y.
the problem is when i position it with tweenlite, it gets done according to its old scale and not according to its new(bigger) scale so the movieclip isnt placed in the exact center of the stage.
Anyone know how i can resolve this?
I tried to compensate by adding a bigger number on the position so it gets positioned in the right way.
But when i click it again i would like it to rescale to its beginning scale and position so it would be very messy to compensate again. Is there any easier way to do this kind of tween?
I doubt that i'm being clear to what i want but i hope i am after all.
The easy and way of tween position and scale is probably to add the menu to a container.
You would then on one hand tween the position of the container, and on the other apply the scale to the menu it self without having to recalculate the proportional position if the scale changes.
This way you can even control the registration point of the menu within the container. e.g. to always scale from the middle...
This can be done with a matrix as well if you want to avoid to stack objects, but honestly it can get really tricky whereas the container method is bullet-proof.