2010-09-09 28 views
109

Come si può ottenere la risoluzione dello schermo (larghezza x altezza) in pixel?Come posso ottenere la risoluzione dello schermo in java?

Sto utilizzando un metodo JFrame e lo swing java.

+2

puoi fornire ulteriori dettagli su ciò che interroghi. Un rivestimento può portare a centinaia di modi diversi. –

+7

Immagino che non ti preoccupi delle configurazioni di più monitor. Sembra che molti sviluppatori di applicazioni li ignorino. Tutti usano più monitor dove lavoro, quindi dobbiamo sempre pensarci. Analizziamo tutti i monitor e li impostiamo come oggetti dello schermo in modo che possiamo bersagliarli quando apriamo nuovi fotogrammi. Se davvero non hai bisogno di questa funzionalità, suppongo che sia giusto che tu abbia fatto una domanda così aperta e abbia accettato una risposta così in fretta. –

risposta

214

È possibile ottenere le dimensioni dello schermo con il metodo Toolkit.getScreenSize().

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
double width = screenSize.getWidth(); 
double height = screenSize.getHeight(); 

su una configurazione multi-monitor si dovrebbe usare questo:

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
int width = gd.getDisplayMode().getWidth(); 
int height = gd.getDisplayMode().getHeight(); 

Se si desidera ottenere la risoluzione dello schermo in DPI Dovrete utilizzare il metodo getScreenResolution() su Toolkit.


Risorse:

+0

Questo non funziona per me. Ho un monitor 3840x2160, ma 'getScreenSize' restituisce 1920x1080. – ZhekaKozlov

11

Questa chiamata ti darà le informazioni che desideri.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
1
int resolution =Toolkit.getDefaultToolkit().getScreenResolution(); 

System.out.println(resolution); 
15

Questo codice sarà enumerare i dispositivi grafici sul sistema (se sono installati più monitor), e puoi usare queste informazioni a determinare affinità monitor o posizionamento automatico (alcuni sistemi utilizzano un monitor laterale poco per visualizzazioni in tempo reale mentre un'applicazione è in esecuzione in background, e tale monitoraggio possono essere identificati dimensioni, colori dello schermo, etc.):

// Test if each monitor will support my app's window 
// Iterate through each monitor and see what size each is 
GraphicsEnvironment ge  = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
GraphicsDevice[] gs  = ge.getScreenDevices(); 
Dimension   mySize = new Dimension(myWidth, myHeight); 
Dimension   maxSize = new Dimension(minRequiredWidth, minRequiredHeight); 
for (int i = 0; i < gs.length; i++) 
{ 
    DisplayMode dm = gs[i].getDisplayMode(); 
    if (dm.getWidth() > maxSize.getWidth() && dm.getHeight() > maxSize.getHeight()) 
    { // Update the max size found on this monitor 
     maxSize.setSize(dm.getWidth(), dm.getHeight()); 
    } 

    // Do test if it will work here 
} 
1
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
double width = screenSize.getWidth(); 
double height = screenSize.getHeight(); 
framemain.setSize((int)width,(int)height); 
framemain.setResizable(true); 
framemain.setExtendedState(JFrame.MAXIMIZED_BOTH); 
3

Questa è la risoluzione della schermata che il componente assegnato è attualmente assegnato (qualcosa come la maggior parte della finestra di root è visibile su quello schermo).

public Rectangle getCurrentScreenBounds(Component component) { 
    return component.getGraphicsConfiguration().getBounds(); 
} 

Usage:

Rectangle currentScreen = getCurrentScreenBounds(frameOrWhateverComponent); 
int currentScreenWidth = currentScreen.width // current screen width 
int currentScreenHeight = currentScreen.height // current screen height 
// absolute coordinate of current screen > 0 if left of this screen are further screens 
int xOfCurrentScreen = currentScreen.x 

Se si vuole rispettare le barre degli strumenti, ecc avrete bisogno di calcolare con questo, anche:

GraphicsConfiguration gc = component.getGraphicsConfiguration(); 
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc); 
2

Ecco alcuni codice funzionale (Java 8) che restituisce la posizione x del lato destro più a destra della maggior parte dello schermo. Se non viene trovato alcuno schermo, restituisce 0.

GraphicsDevice devices[]; 

    devices = GraphicsEnvironment. 
    getLocalGraphicsEnvironment(). 
    getScreenDevices(); 

    return Stream. 
    of(devices). 
    map(GraphicsDevice::getDefaultConfiguration). 
    map(GraphicsConfiguration::getBounds). 
    mapToInt(bounds -> bounds.x + bounds.width). 
    max(). 
    orElse(0); 

Questi sono collegamenti a JavaDoc.

GraphicsEnvironment.getLocalGraphicsEnvironment()
GraphicsEnvironment.getScreenDevices()
GraphicsDevice.getDefaultConfiguration()
GraphicsConfiguration.getBounds()

1

Queste tre funzioni restituiscono le dimensioni dello schermo in Java. Questo codice rappresenta le configurazioni multi-monitor e le barre delle attività. Le funzioni incluse sono: getScreenInsets(), getScreenWorkingArea() e getScreenTotalArea().

Codice:

/** 
* 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; 
} 
0
int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution(); 
System.out.println(""+screenResolution); 
+0

Benvenuto in Stack Overflow! Anche se questo snippet di codice può risolvere la domanda, [inclusa una spiegazione] (// meta.stackexchange.com/questions/114762/explaining-entely-code-based-answers) aiuta davvero a migliorare la qualità del tuo post. Ricorda che stai rispondendo alla domanda per i lettori in futuro, e queste persone potrebbero non conoscere le ragioni del tuo suggerimento sul codice. Cerca anche di non affollare il tuo codice con commenti esplicativi, questo riduce la leggibilità sia del codice che delle spiegazioni! – kayess

1

Ecco un frammento di codice che uso spesso. Restituisce l'intera area dello schermo disponibile (anche su configurazioni multi-monitor) mantenendo le posizioni native del monitor.

public static Rectangle getMaximumScreenBounds() { 
    int minx=0, miny=0, maxx=0, maxy=0; 
    GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    for(GraphicsDevice device : environment.getScreenDevices()){ 
     Rectangle bounds = device.getDefaultConfiguration().getBounds(); 
     minx = Math.min(minx, bounds.x); 
     miny = Math.min(miny, bounds.y); 
     maxx = Math.max(maxx, bounds.x+bounds.width); 
     maxy = Math.max(maxy, bounds.y+bounds.height); 
    } 
    return new Rectangle(minx, miny, maxx-minx, maxy-miny); 
} 

Su un computer con due monitor full-HD, in cui quello sinistro è impostato come il monitor principale (in impostazioni di Windows), la funzione restituisce

java.awt.Rectangle[x=0,y=0,width=3840,height=1080] 

Sulla stessa messa a punto, ma con il monitor corretto impostato come monitor principale, la funzione restituisce

java.awt.Rectangle[x=-1920,y=0,width=3840,height=1080] 
Problemi correlati