How to get the center x and y of desktop with swing - java

I want my jframe to open in the center of a person's monitor.
(By default a jframe will open at coordinate. (0,0))
To achieve this, before setting the frame visible, I use this method.
this.setLocation(x,y);
In theory, monitor screen sizes can be different, meaning the center coordinate will be different for almost all computers.
HERES MY QUESTION:
How would I get the center coordinate of the computer monitor running the swing application?

How would I get the center coordinate of the computer monitor running the swing application?
You can get the center coordinate with java.awt.Toolkit:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int centerX = screenSize.width/2;
int centerY = screenSize.height/2;
However, you don't need it.
Just use:
yourFrame.setLocationRelativeTo(null);
And it will be automatically centered

Try it
//call `setSize` first
this.setSize(300, 600);
Toolkit tk = this.getToolkit();
Dimension dim = tk.getScreenSize();
int x = (int) dim.getWidth() / 2 - this.getWidth() / 2;
int y = (int) dim.getHeight() / 2 - this.getHeight() / 2;
this.setLocation(x, y);
Here this represents JFrame class object.
JFrame will display in the center of the screen.

Query the Toolkit object.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int centerX = screenSize.getWidth() / 2;
int centerY = screenSize.getHeight() / 2;

Swing's documentation states that the window will be centered on screen if setRelativeTo is passed null.
public void setLocationRelativeTo(Component c)
As stated in their docs:
"If the component is null, or the GraphicsConfiguration associated with
this component is null, the window is placed in the center of the screen. The center point can be obtained with the GraphicsEnvironment.getCenterPoint method."
http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setLocationRelativeTo(java.awt.Component)
So simply do:
frame.setLocationRelativeTo(null);
..since your question was really how to get it to open in the center, not how to get the center coordinate.

Related

Size of splash screen IDE

For my thesis work, I need to create an IDE and I want to create a splash screen for the IDE.
So my first question is that I don't know which size for the image I must create for the splash screen.
My second question is that probably a lot of different screen resolutions are available in the market and the screen splash might be bigger or smaller for some computers... Is there anything that we could do to solve this ?
P.S.: I really liked the splash screen from IntelliJ IDE (15), I want to create something like that as a splash screen.
//----------------------------------------------------------------//
Solution for question 2:
//Get the resolution of the screen PC
Toolkit toolkit = Toolkit.getDefaultToolkit();
int X = (toolkit.getScreenSize().width);
int Y = (toolkit.getScreenSize().height);
//Get the pixels of the screen middle resolution
int cX = (toolkit.getScreenSize().width / 2) - ((int)((ImageView) s.lookup("#splash_logo")).getFitWidth())/2;
int cY = (toolkit.getScreenSize().height / 2) - ((int)((ImageView) s.lookup("#splash_logo")).getFitHeight()/2);
//Set the position of Stage to middle
primaryStage.setX(cX);
primaryStage.setY(cY);
If you want to scale it in relation to the monitor's resolution, I'd first get the height and width with the java.awt.Toolkit:
Toolkit toolkit = Toolkit.getDefaultToolkit();
int X = (toolkit.getScreenSize().width)
int Y = (toolkit.getScreenSize().height)
Then you can scale an image that you're using for your splashscreen by using this method:
public BufferedImage scaleImage(int multiplier, BufferedImage img) {
BufferedImage bi = new BufferedImage(multiplier * img.getWidth(null), multiplier * img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D grph = (Graphics2D) bi.getGraphics();
grph.scale(multiplier, multiplier);
// everything drawn with grph from now on will get scaled.
grph.drawImage(img, 0, 0, null);
grph.dispose();
return bi;
}
Then center it on the screen by calculating the middle and setting your component there:
int cX = (toolkit.getScreenSize().width / 2) - (getWidth() / 2);
int cY = (toolkit.getScreenSize().height / 2) - (getHeight() / 2);
component.setLocation(cX, cY);
actual implementation is up to you, but these are the tools I use and it works fine for me. Hope this helps!

setLocation of 2 frames in the middle of the screen

I need to center two JFrame side by side on the screen (for instance as if they were a single frame centered).
To center a single JFrame I used the command:
frame.setLocationRelativeTo(null);
How can I resolve now?
You need to know three things...
The available visible area of the screen
The size of both windows...(this counts as two)
There are a few ways to get the screen size, but what you really want is the viewable area, the area within which it is safe to show windows...
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
// Use this if need to know about a device which is not the default...
//GraphicsDevice lstGDs[] = ge.getScreenDevices();
GraphicsDevice device = ge.getDefaultScreenDevice();
GraphicsConfiguration cf = device.getDefaultConfiguration();
Rectangle bounds = cf.getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);
This now tells you the area within which it is safe to show content, taking into consideration things like the taskbar/dock that some OS's have...
Next, you need to know the size of the window...
frameA.pack();
frameB.pack();
Dimension dimA = frameA.getSize();
Dimension dimB = frameB.getSize();
Now, you need to calculate the position of the windows...
Point pA = new Point(
bounds.x + ((bounds.width / 2) - dimA.width),
bounds.y + ((bounds.height- dimA.height) / 2));
Point pB = new Point(
bounds.x + (bounds.width / 2),
bounds.y + ((bounds.height- dimB.height) / 2));
And there you have it...
Now, having said all that, you might like to take a look at The Use of Multiple JFrames, Good/Bad Practice? and How to Use Split Panes
You can manually set the position of your frame with #setLocation I think

Java Jpanel Zoom Button (netbeans 7.4 Gui buiilder)

I have a paint component where I draw some shapes but the problem my shapes exceed jPanel Size . because of that I search a solution of zoom jpanel. I don't know if this possible could someone help me I use net beans gui builder
If you want the shapes to size according to your JPanel, make use of the JPanel's getWidth() and getHeight() e.g.
int x = (int)(getWidth() * 0.1);
int y = (int)(getHeight() * 0.1);
int width = (int)(getWidth() * 0.8);
int height = (int)(getheight() * 0.8):
g.fillRect(x, y, width, height);
See a full running example here
You can see here how to customize initialization code for your JPanel in GUI Builder
Add your JPanel to a JScrollPane and add the JScrollPane to the container you were originally adding your JPanel to. Make sure to setPreferredSize to a size large enough to see all of your drawings whenever you draw a new shape or however you are doing your graphics.

How to setVisible a JinternalFrame in the center of the JdesktopPane?

Actually i wanna show the JinternalFrame in the center of the JDesktopPane and i used this methode as i use it in Jframes but it didn't work :
Extraction ex=new Extraction();//Extraction is the name of the JintenalFrame
jDesktopPane1.add(ex);
ex.setLocationRelativeTo(this);
ex.setVisible(true);
So i am asking if there is another methode so i can display the JinternalFrame in the center of the JdesktoPane.
Thank you
Try something like :
JDesktopPane mainPanel;
JInternalFrame jif_test = new JInternalFrame();
public void centerJIF(JInternalFrame jif) {
Dimension desktopSize = mainPanel.getSize();
Dimension jInternalFrameSize = jif.getSize();
int width = (desktopSize.width - jInternalFrameSize.width) / 2;
int height = (desktopSize.height - jInternalFrameSize.height) / 2;
jif.setLocation(width, height);
jif.setVisible(true);
}
And
centerJIF(jif_test);
A javax.swing.JInternalFrame lacks the convenient setLocationRelativeTo(null) implementation found in java.awt.Window, but you can use a similar approach. Just subtract half the width and height of the internal frame from the corresponding center coordinates of the desktop pane. Use the new coordinates in your call to setLocation().
You'll also want to adjust the internal frame's dimensions if necessary.

Open window on mouse co-ordinates

I was wondering if it was possible to open a window where the mouse currently is? I have the current mouse co-ordinate but am unable to find what to do with the x y values when displaying my window.
Hopefully someone could point me in the direction of the appropriate method.
Thanks
If you haven't already, using the MouseInfo class will get the x and y position.
Point location = MouseInfo.getPointerInfo().getLocation();
You specified that you want to use a JFrame in this case, so setting the location of the JFrame to this x and y point will do so.
Point location = MouseInfo.getPointerInfo().getLocation();
int x = (int) location.getX();
int y = (int) location.getY();
JFrame frame = new JFrame(); //this is just the initialization of the window
frame.setLocation(x, y);
user1181445 was right, but I think you can set the frame location without creating the variables x and y:
Point location = MouseInfo.getPointerInfo().getLocation();
JFrame frame = new JFrame(); //this is just the initialization of the window
frame.setLocation(location);

Categories