setLocation of 2 frames in the middle of the screen - java

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

Related

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

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.

Applet - align text to bottom middle

Is there a way to align text from g.drawString() to the bottom center of a java applet?
I am also hoping for a way that is fluid between full-screen and small screen.
Yes, start by taking a look at Measuring Text
String text = "Happy at the bottom";
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = (getHeight() - fm.getHeight()) + fm.getAscent();
g.drawString(text, x, y);
The next question that needs to asked, is could what you want be achieved by using a JLabel and a BorderLayout (or other compound layout)....?

pushMatrix to center of window

I'm looking to make a graphic in Processing that's centered in the middle of the window. I want to be able to change the size of the window and have the graphic remain centred no matter what, so I intend to do this through centering the matrix itself.
How would I go about doing this? Normally I would translate the matrix to the center of the window based on the size of the window itself, but if I'm changing the size then it won't work.
Suggestions?
here, I got this old code that kind of do this...
import processing.opengl.*;
int newCanvasWidth = MIN_WINDOW_WIDTH; // made global to use in draw
int newCanvasHeight = MIN_WINDOW_HEIGHT;
java.awt.Insets insets; //"An Insets object is a representation of the borders of a container"
//from http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Insets.html
void setup()
{
size(200, 200); // always first line
frame.pack(); //frame.pack() no need for setResizable... plus insets
insets = frame.getInsets();
frame.setResizable(true);
/// for debuging, system depende`nt, at least screen is...
print("MIN_WINDOW_WIDTH = " + MIN_WINDOW_WIDTH);
print(" MIN_WINDOW_HEIGHT = " + MIN_WINDOW_HEIGHT);
print(" screenWidth = " + displayWidth);
println(" screenHeight = " + displayHeight);
}
void draw()
{
background(255);
ellipse(width/2, height/2, width/2, height/2);
}

How to detect the available part of the main display that's available for normal windows ?

On most desktop environments, you have one ore more action bars and sometimes docked windows.
A maximized window will used all the space that is not used by these different things on the side of the screen. That's what I'm calling the "available space"
Is there a java API to detect the available space, and if possible to listen to the changes that may occur ?
The basic idea is to get a reference to the screen device and subtract the screen insets from the screen bounds as follows
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
Rectangle bounds = new Rectangle(0, 0, 0, 0);
if (gd != null) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
bounds = gc.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);
}
Now, your next problem is going to be to determine which screen device you actually want, this example simple uses the default screen ;)

How do you get the screen width in java?

Does anyone know how you would get the screen width in java? I read something about some toolkit method but I'm not quite sure what that is.
Thanks,
Andrew
java.awt.Toolkit.getDefaultToolkit().getScreenSize()
Here are the two methods I use, which account for multiple monitors and task-bar insets. If you don't need the two methods separately, you can, of course, avoid getting the graphics config twice.
static public Rectangle getScreenBounds(Window wnd) {
Rectangle sb;
Insets si=getScreenInsets(wnd);
if(wnd==null) {
sb=GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration()
.getBounds();
}
else {
sb=wnd
.getGraphicsConfiguration()
.getBounds();
}
sb.x +=si.left;
sb.y +=si.top;
sb.width -=si.left+si.right;
sb.height-=si.top+si.bottom;
return sb;
}
static public Insets getScreenInsets(Window wnd) {
Insets si;
if(wnd==null) {
si=Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration());
}
else {
si=wnd.getToolkit().getScreenInsets(wnd.getGraphicsConfiguration());
}
return si;
}
The working area is the desktop area of the display, excluding taskbars, docked windows, and docked tool bars.
If what you want is the "working area" of the screen, use this:
public static int GetScreenWorkingWidth() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
}
public static int GetScreenWorkingHeight() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
}
Toolkit has a number of classes that would help:
getScreenSize - raw screen size
getScreenInsets - gets size of toolbar, dock
getScreenResolution - dpi
We end up using 1 and 2, to compute usable maximum window size. To get the relevant GraphicsConfiguration, we use
GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDefaultConfiguration();
but there may be smarter multiple-monitor solutions.
The following code should do it (haven't tried it):
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.getDefaultConfiguration().getBounds().getWidth();
edit:
For multiple monitors you should use the following code (taken from the javadoc of java.awt.GraphicsConfiguration:
Rectangle virtualBounds = new Rectangle();
GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] gs =
ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc =
gd.getConfigurations();
for (int i=0; i < gc.length; i++) {
virtualBounds =
virtualBounds.union(gc[i].getBounds());
}
}
The OP probably wanted something like this:
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Toolkit.getDefaultToolkit().getScreenSize().getWidth()
You can get it by using the AWT Toolkit.
Toolkit.getScreenSize().
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
If you need the resolution of the screen that a certain component is currently assigned to (something like most part of the root window is visible on that screen), you can use this answer.
A good way of detecting whether or not something is within visual bounds, is using
Screen.getScreensForRectangle(x, y, width, height).isEmpty();
This is an improvement to the multi-monitor solution posted (above) by Lawrence Dol. As in his solution, this code accounts for multiple monitors and task-bar insets. The included functions are: getScreenInsets(), getScreenWorkingArea(), and getScreenTotalArea().
Changes from the Lawrence Dol version:
This avoids getting the graphics configuration twice.
Added a function for getting the total screen area.
Renamed the variables for clarity.
Added Javadocs.
Code:
/**
* getScreenInsets, This returns the insets of the screen, which are defined by any task bars
* that have been set up by the user. This function accounts for multi-monitor setups. If a
* window is supplied, then the the monitor that contains the window will be used. If a window
* is not supplied, then the primary monitor will be used.
*/
static public Insets getScreenInsets(Window windowOrNull) {
Insets insets;
if (windowOrNull == null) {
insets = Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration());
} else {
insets = windowOrNull.getToolkit().getScreenInsets(
windowOrNull.getGraphicsConfiguration());
}
return insets;
}
/**
* getScreenWorkingArea, This returns the working area of the screen. (The working area excludes
* any task bars.) This function accounts for multi-monitor setups. If a window is supplied,
* then the the monitor that contains the window will be used. If a window is not supplied, then
* the primary monitor will be used.
*/
static public Rectangle getScreenWorkingArea(Window windowOrNull) {
Insets insets;
Rectangle bounds;
if (windowOrNull == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
insets = Toolkit.getDefaultToolkit().getScreenInsets(ge.getDefaultScreenDevice()
.getDefaultConfiguration());
bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
} else {
GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
insets = windowOrNull.getToolkit().getScreenInsets(gc);
bounds = gc.getBounds();
}
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);
return bounds;
}
/**
* getScreenTotalArea, This returns the total area of the screen. (The total area includes any
* task bars.) This function accounts for multi-monitor setups. If a window is supplied, then
* the the monitor that contains the window will be used. If a window is not supplied, then the
* primary monitor will be used.
*/
static public Rectangle getScreenTotalArea(Window windowOrNull) {
Rectangle bounds;
if (windowOrNull == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
} else {
GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
bounds = gc.getBounds();
}
return bounds;
}

Categories