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.
Related
Here is a very simple JTable on a JFrame example that I extracted from another web site.
import javax.swing.*;
public class JTableSimpleSample {
JFrame f;
JTableSimpleSample(){
f=new JFrame();
String data[][]={ {"101","Amit","670000"},
{"102","Jai","780000"},
{"101","Sachin","700000"}};
String column[]={"ID","NAME","SALARY"};
JTable jt=new JTable(data,column);
jt.setBounds(30,40,200,300);
JScrollPane sp=new JScrollPane(jt);
f.add(sp);
f.setSize(300,400);
f.setVisible(true);
}
public static void main(String[] args) {
new JTableSimpleSample();
}
}
When resizing this window, the behavior is slightly different depending upon which edge is being used for resizing.
Top - resize the frame by clicking the top edge and it looks pretty clean except it does appear the bottom edge of the JFrame is continually getting repainted making it look like multiple lines exist.
Bottom - resize the frame by clicking the bottom edge looks pretty good as making it large shows a black area until the frame gets to repaint. Depending on how fast you resize the frame determines how much of the black area is visible.
Right - resize the frame by clicking the right edge is very similar to when the bottom edge is used when resizing.
Left - resize the frame by clicking the left edge looks terrible. It is no only similar to when it is resized using the top by showing multiple lines on the right edge, but at times the data itself looks a mess.
Using any of the corners results in a combination of 2 from any of the above scenarios.
Question/Objective :
1 - Why such different behavior?
2 - Is it possible to have it so that only an outline of the frame is shown when resizing and instead of a black background when resizing, have it transparent. Something like the following if the bottom right corner is used for resizing. The red lines are used to make it obvious as to the objective but it can very well remain black :
I did review Black outline while resizing JFrame but didn't fully understand the usage of the timer. Was not sure if there exists a property or if this would be managed via a mouse handler tied to the JFrame.
Basically I want to control the size of the content pane even when the application is in full-screen exclusive mode. I want the content to be square, with black bars taking up unused space. Is there a way to do this? Setting the preferred size doesn't work, it just fills the screen anyway. Here's the gist of my code:
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground(Color.BLACK);
setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
setUndecorated(true);
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
getContentPane().setPreferredSize(new Dimension(getHeight(), getHeight()));
getContentPane().setBackground(Color.WHITE);
pack();
This results in the entire screen being white. On my widescreen monitor, I want the middle of the screen to be white, with black bars on the sides. Is there a clean way to do this? I guess I could add a secondary JPanel, size it correctly, and use that for everything, but that seems sloppy. I'd rather use the JFrame's built-in content pane.
I attempted to make an applet program I have Stand alone by adding in:
public static void main(String[] args) {
JFrame frame = new JFrame("StartingPoint");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
StartingPoint sp = new StartingPoint();
frame.getContentPane().add(sp);
sp.init();
frame.pack();
frame.setVisible(true);
sp.start();
}
Right after my public class. When running as just the applet this does nothing, but when Running it as an application it runs as a very small, nearly flat box aside from the heading, and when manually resized, the screen is blank other then the backround color.
Any idea what may cause this?
I have also noticed, each time I resize the frame, what is on it freezes,as if a screen shot of what should happen, and when the screen is resized to nearly full screen I can see at the tip top of the screen a sliver of what should be moving.
I'd just like to say, that dropping an applet into a frame is a really bad idea. You are better off writing the application contents into a separate container (such as JPanel) and adding that to your applet or frame - IMHO.
From the Java Docs...
Window#pack
Causes this Window to be sized to fit the preferred size and layouts
of its subcomponents. The resulting width and height of the window are
automatically enlarged if either of dimensions is less than the
minimum size as specified by the previous call to the setMinimumSize
method.
If the window and/or its owner are not displayable yet, both of them
are made displayable before calculating the preferred size. The Window
is validated after its size is being calculated.
This would suggest that your applet needs to provide a preferredSize if you wish to use pack
You need to set the size of the JFrame:
frame.setSize(500, 400);
It sounds as if you are overriding the paint() method. If so, you will need to call
super.paint(g);
to repaint all child components of the applet container on resize.
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
I'll try to explain my problem as simply as possible but it's a tricky topic and people who haven't encountered the issue probably won't know what I'm talking about.
I want to use a BorderLayout using west, east, north, south, etc. components that are my "normal" components (JLabels, JButtons, etc.) then I want the center component to be an "image" (that is: pixels). To this end I'm using a BufferedImage and using setIcon on a JLabel that is inside a panel that is part of the "center".
However I want my image/pixels to be "fluid": whenever the user resizes the app, I want to compute the exact size of the JLabel (icon/text gap is set to 0) and then create a new image (pixels that I manipulate directly in a BufferedImage but whatever) that has exactly that size.
Now it does work fine. But only when I resize the main window ("window" as in "one of the window of the operating system) by making it bigger.
It doesn't work when I downsize my main window.
The reason, after a lot of testing, is obviously because the size of my JLabel (in which I did a setIcon( img ) is influencing the computation of the layout manager.
So here comes the billion dollar question: how should I use a BorderLayout (or any other layout) so that I can create a "fluid" rectangle of pixels in the center of my app?
Answering my own question with an answer that I will not accept even tough it does work...
The problem can be "worked around" by creating a picture a few pixels smaller than the getVisibleRect of the center area.
So in my case I create an ImageIcon from a BufferedImage that is 20 pixels smaller (both in width and height) than the area that will hold it.
What happens then is that because the picture is smaller it doesn't "block"/prevent the layout manager from putting everything at their correct place when downsizing the main window.
So by using such an hack I get the "fluid" behavior I want.
This is however an hack whose level of hackyness cannot be understated and I'm sure there's a very clean way to solve this.
The reason, after a lot of testing, is
obviously because the size of my
JLabel (in which I did a setIcon( img
) is influencing the computation of
the layout manager.
The preferred size of the JLabel is used in the preferred size of the panel, but this size is ignored when you resize the frame, since the CENTER only gets whatever space is left over after the preferred size of the other 4 components is considered.
To this end I'm using a BufferedImage
and using setIcon on a JLabel that is
inside a panel that is part of the
"center".
Sounds to me like it should work.
Create the panel with a BorderLayout. Add the JLabel to the Center of your main panel. Then add a ComponentListener to the panel. Now when the frame is resized the center panel size will be adjusted to take the space available to it. Now that you know the size of the center panel you can recreate the Icon and add it to your JLabel,
This is how you write a SSCCE:
import java.awt.*;
import javax.swing.*;
public class LabelTest2 extends JFrame
{
public LabelTest2()
{
JLabel picture = new JLabel(new ImageIcon("???.jpg"));
add(picture);
}
public static void main(String[] args)
{
LabelTest2 frame = new LabelTest2();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}