Mirage Source

Free ORPG making software.
It is currently Tue Apr 23, 2024 7:56 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Jul 28, 2009 2:07 pm 
Offline
Persistant Poster
User avatar

Joined: Thu Aug 17, 2006 5:27 pm
Posts: 866
Location: United Kingdom
Simple right?

Well, I developed a little something on my laptop last night then brought it to my dual screen linux setup here... the default approach places the JPane in the center of both my screens.. so half on both... interesting. By default approach, I mean:

Code:
Toolkit toolkit = Toolkit.getDefaultToolkit(); 
Dimension screenResolution = toolkit.getScreenSize();

JFrame mainWindow = new JFrame("Main Window"); 
mainWindow.setSize(300, 350); 

int x = (screenResolution.width - mainWindow.getWidth()) / 2; 
int y = (screenResolution.height - mainWindow.getHeight()) / 2; 

mainWindow.setLocation(x, y);


So we are grabbing the screen size from the JVM which is fine, unfortunately in linux (not sure if it treats it differently in windows) this happens to be the overall size of all your monitors in their current configuration - I'm guessing this is due to the way X (not the above variable, the linux display thing) handles everything.

Anyway, you can actually tell Java to give you all of the display devices it can find (this can be anything from a monitor to a printer...), find out which ones are raster devices (thus eliminating LEDs, printers etc) and grab the default system display of that type - our users primary monitor. You can then ask for the specifics of that monitor, plot a rectangle object with those vars, and offset your window to that instead - ensuring your app always opens bang in the middle of their primary monitor. Yay.

Here is my code:

Code:
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;

public class GUIUtils {
   
   public static Point getCenterLocation(Dimension d){      
      Point center = new Point();
      Rectangle screenResolution = getPrimaryScreen().getBounds();
      
      center.x = (screenResolution.width - d.width) / 2;
      center.y = (screenResolution.height - d.height) / 2;
      
      return (center);
   }
   
   public static GraphicsConfiguration getPrimaryScreen(){
      GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
      GraphicsDevice[] devs = env.getScreenDevices();
      GraphicsConfiguration config = null;
      
      int size = (devs == null) ? 0 : devs.length;
      for (int i = 0; i < size && config == null; i++)
      {
         int type = devs[i].getType();
         if(type == GraphicsDevice.TYPE_RASTER_SCREEN)
         {
            config = devs[i].getDefaultConfiguration();
         }
      }
      
      config.getBounds();
      
      return(config);
   }

}


Now, whenever you want to properly center a JPane or window, just use this:

Code:
yourJPaneName.setLocation(GUIUtils.getCenterLocation(yourJPaneName.getSize()));


Super simple and very handy. A nice little piece of code I'll keep hold of for a good while I think, true cross-platform consideration :3

Fox


Top
 Profile  
 
PostPosted: Tue Jul 28, 2009 7:00 pm 
Offline
Pro

Joined: Mon May 29, 2006 2:58 pm
Posts: 370
Windows, as far as I know, treats each screen as a seperate entity, and split programs, when maximized and such, default to the primary screen. Though, there are some programs that allow Windows to treat them both as one screen, so they may treat it differently.

_________________
Image


Top
 Profile  
 
PostPosted: Thu Jul 30, 2009 9:31 pm 
Offline
Regular

Joined: Sun Apr 26, 2009 11:22 pm
Posts: 43
Location: Cincinnati, OH
Google Talk: rj.cox101@gmail.com
If your looking for the easy solution to centering just...
Code:
JFrame myFrame;
// Add controls...
myFrame.pack();
myFrame.setLocationRelativeTo(null);

I doubt it will center it on the primary monitor but it's better than coke's default approach :D.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group