2010-09-09 17 views
109

पिक्सेल में स्क्रीन रिज़ॉल्यूशन (चौड़ाई x ऊंचाई) कैसे प्राप्त कर सकता है?जावा में स्क्रीन रिज़ॉल्यूशन कैसे प्राप्त किया जा सकता है?

मैं एक जेएफआरएएम और जावा स्विंग विधियों का उपयोग कर रहा हूं।

+2

तुम क्या आप question.One लाइनर सौ अलग अलग तरीकों को जन्म दे सकता बारे में कुछ और विस्तार प्रदान कर सकते हैं। –

+7

मुझे लगता है कि आपको एकाधिक मॉनीटर सेटअप की परवाह नहीं है। ऐसा लगता है कि कई एप्लिकेशन डेवलपर इन्हें अनदेखा करते हैं। हर कोई कई मॉनीटर का उपयोग करता है जहां मैं काम करता हूं, इसलिए हमें हमेशा उनके बारे में सोचना पड़ता है। हम सभी मॉनीटरों की जांच करते हैं और उन्हें स्क्रीन ऑब्जेक्ट्स के रूप में सेट करते हैं ताकि जब हम नए फ्रेम खोलें तो हम उन्हें लक्षित कर सकते हैं। यदि आपको वास्तव में इस कार्यक्षमता की आवश्यकता नहीं है, तो मुझे लगता है कि यह ठीक है कि आपने इस तरह के एक खुले अंत प्रश्न पूछा और इतनी जल्दी एक जवाब स्वीकार कर लिया। –

उत्तर

214

आप Toolkit.getScreenSize() विधि के साथ स्क्रीन का आकार प्राप्त कर सकते हैं।

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

एक बहु की निगरानी विन्यास पर आप इस का उपयोग करना चाहिए:

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

आप डीपीआई में स्क्रीन रिज़ॉल्यूशन प्राप्त करना चाहते हैं आप getScreenResolution() विधि का उपयोग करना होगा Toolkit पर।


संसाधन:

+0

यह मेरे लिए काम नहीं करता है। मेरे पास 3840x2160 मॉनीटर है, लेकिन 'getScreenSize' 1920x1080 लौटाता है। – ZhekaKozlov

11

यह कॉल आपको वह जानकारी देगा जो आप चाहते हैं।

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

System.out.println(resolution); 
15

इस कोड को सिस्टम पर ग्राफिक्स उपकरणों की गणना करेगा (एकाधिक मॉनिटर स्थापित कर रहे हैं), और आप उस जानकारी का उपयोग कर सकते हैं

// 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

इस स्क्रीन का संकल्प है कि दिए गए घटक वर्तमान में निर्दिष्ट किया जाए (कुछ जड़ खिड़की के अधिकांश भाग की तरह है कि स्क्रीन पर दिखाई देता है)।

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

उपयोग:

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 

आप टूलबार का सम्मान करना चाहते हैं, आदि तुम भी इस के साथ गणना करने के लिए की आवश्यकता होगी:

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

यहाँ कुछ कार्यात्मक कोड है (जावा 8) जो दाएं अधिकांश स्क्रीन के दाएं किनारे की एक्स स्थिति देता है। यदि कोई स्क्रीन नहीं मिलती है, तो यह 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); 

यहां जावाडॉक के लिंक हैं।

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

1

इन तीन कार्यों जावा में स्क्रीन आकार लौट आते हैं। यह कोड बहु-मॉनीटर सेटअप और टास्क बार के लिए खाता है। शामिल फ़ंक्शंस हैं: getScreenInsets(), getScreenWorkingArea(), और getScreenTotalArea()

कोड:

/** 
* 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

स्टैक ओवरफ़्लो में आपका स्वागत है! हालांकि यह कोड स्निपेट प्रश्न हल कर सकता है, [एक स्पष्टीकरण सहित] (// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) वास्तव में आपकी पोस्ट की गुणवत्ता में सुधार करने में मदद करता है। याद रखें कि आप भविष्य में पाठकों के लिए प्रश्न का उत्तर दे रहे हैं, और वे लोग आपके कोड सुझाव के कारणों को नहीं जानते हैं। स्पष्टीकरण टिप्पणियों के साथ अपने कोड को भीड़ न करने का प्रयास करें, इससे कोड और स्पष्टीकरण दोनों की पठनीयता कम हो जाती है! – kayess

1

यहाँ कोड का एक टुकड़ा मैं अक्सर इस्तेमाल करते हैं। देशी मॉनीटर पोजीशन को बनाए रखते हुए यह पूर्ण उपलब्ध स्क्रीन क्षेत्र (यहां तक ​​कि मल्टी-मॉनीटर सेटअप पर भी) देता है।

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); 
} 

साथ दो पूर्ण HD पर नज़र रखता है, जहां छोड़ दिया एक (विंडोज सेटिंग्स में) मुख्य मॉनिटर के रूप में सेट किया गया है एक कंप्यूटर पर, फ़ंक्शन

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

समान सेटअप पर, लेकिन साथ सही पर नजर रखने के मुख्य मॉनिटर के रूप में निर्धारित करते हैं, फ़ंक्शन

java.awt.Rectangle[x=-1920,y=0,width=3840,height=1080] 
संबंधित मुद्दे