2009-12-20 15 views
40

क्या कोई जानता है कि आपको जावा में स्क्रीन चौड़ाई कैसे मिलेगी? मैंने कुछ टूलकिट विधि के बारे में कुछ पढ़ा लेकिन मुझे पूरा यकीन नहीं है कि यह क्या है।आप जावा में स्क्रीन चौड़ाई कैसे प्राप्त करते हैं?

धन्यवाद, एंड्रयू

+5

ध्यान दें, अतिरिक्त देखभाल जब एकाधिक मॉनिटर होने लिया जाना चाहिए। –

+1

आपको स्क्रीन इन्सेट के लिए भी खाता होना चाहिए (मेरा जवाब देखें); कई लोग टास्क बार को स्क्रीन के एक या दूसरी तरफ ले जाना पसंद करते हैं। –

उत्तर

77
java.awt.Toolkit.getDefaultToolkit().getScreenSize() 
+0

मैककन: रिटर्न? – Hydroid

+1

@ हाइड्रॉइड http://stackoverflow.com/a/8101318/632951 – Pacerier

+0

प्रकार 'आयाम' – KJaeg

5

निम्नलिखित कोड यह करना चाहिए (यह प्रयास नहीं किया है):

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
GraphicsDevice gd = ge.getDefaultScreenDevice(); 
gd.getDefaultConfiguration().getBounds().getWidth(); 

संपादित करें:

एकाधिक मॉनिटर का उपयोग करना चाहिए के लिए निम्नलिखित कोड (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()); 
     } 
    } 
+0

प्रकार का ऑब्जेक्ट देता है नाइस ग्रैब। इसे दिनों के लिए खोज रहे थे, बस SO पर सही खोज मानदंड नहीं मिल सका। –

2

ओपी शायद कुछ इस तरह थे:

Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 
1
Toolkit.getDefaultToolkit().getScreenSize().getWidth() 
5

Toolkit वर्गों के एक नंबर है कि मदद मिलेगी है:

  1. getScreenSize - कच्चे स्क्रीन आकार
  2. getScreenInsets - टूलबार का आकार हो जाता है , गोदी
  3. getScreenResolution - डीपीआई

हम प्रयोग करने योग्य अधिकतम विंडो आकार की गणना करने के 1 और 2, का उपयोग करते हुए अंत। प्रासंगिक ग्राफिक्स कॉन्फ़िगरेशन प्राप्त करने के लिए, हम

GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDefaultConfiguration(); 

का उपयोग करते हैं लेकिन स्मार्ट एकाधिक-मॉनीटर समाधान हो सकते हैं।

+0

प्राथमिक मॉनिटर होने के लिए 'getScreenDevices' ** गारंटी ** सरणी में पहला आइटम है? – Pacerier

+0

एपीआई में निर्दिष्ट नहीं है - GetDefaultScreenDevice के साथ जाने के लिए बेहतर है? http://download.oracle.com/javase/6/docs/api/java/awt/GraphicsEnvironment.html#getDefaultScreenDevice() –

+0

क्या आपका मतलब है 'GraphicsEnvironment.getDefaultScreenDevice() '? – Pacerier

18

यहां दो विधियां हैं जिनका उपयोग मैं कई मॉनीटर और टास्क-बार इन्सेट्स के लिए करता हूं। यदि आपको दो तरीकों से अलग तरीके की आवश्यकता नहीं है, तो आप निश्चित रूप से ग्राफिक्स कॉन्फ़िगरेशन को दो बार प्राप्त करने से बच सकते हैं।

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; 
    } 
+0

+1 इन्सेट को पकड़ने के तरीके के लिए +1। यदि आप डॉक ओवरलैप किए बिना पूर्ण आकार की खिड़की रखना चाहते हैं तो वे विशेष रूप से ओएस एक्स पर महत्वपूर्ण हो सकते हैं। –

+0

@SoftwareMonkey ग्राफिक्स को नया फ्रेम बनाने के बिना वैसे भी कॉन्फ़िगरेशन प्राप्त करने के लिए है? – Pacerier

+0

-1: कोई 'Toolkit.getDefaultToolkit() नहीं है। GetGraphicsConfiguration() ' – jan

16

कार्य क्षेत्र प्रदर्शन के डेस्कटॉप क्षेत्र का taskbars, डॉक की गई खिड़कियां, और डॉक की गई टूल बार को छोड़कर है।

क्या आप चाहते हैं स्क्रीन के "कार्य क्षेत्र" है, तो इस का उपयोग करें:

public static int GetScreenWorkingWidth() { 
    return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width; 
} 

public static int GetScreenWorkingHeight() { 
    return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height; 
} 
+1

यह बहु मॉनिटर स्थितियों के लिए काम नहीं करता है – jan

0

आप स्क्रीन के संकल्प कि एक निश्चित घटक वर्तमान में (के अधिकांश भाग की तरह कुछ करने के लिए असाइन किया गया है की जरूरत है रूट स्क्रीन उस स्क्रीन पर दिखाई दे रही है), आप this answer का उपयोग कर सकते हैं।

0

या नहीं, कुछ का पता लगाने का एक अच्छा तरीका

Screen.getScreensForRectangle(x, y, width, height).isEmpty(); 
0

उपयोग कर रहा है इस बहु-मॉनिटर समाधान तैनात (ऊपर) लॉरेंस Dol से करने के लिए एक सुधार है, दृश्य सीमा के भीतर है। अपने समाधान के रूप में, यह कोड एकाधिक मॉनीटर और टास्क-बार इन्सेट्स के लिए खाता है। शामिल कार्य हैं: getScreenInsets(), getScreenWorkingArea(), और getScreenTotalArea()। लॉरेंस Dol संस्करण से

परिवर्तन:

  • यह ग्राफिक्स विन्यास में दो बार हो रही बचा जाता है।
  • कुल स्क्रीन क्षेत्र प्राप्त करने के लिए एक फ़ंक्शन जोड़ा गया।
  • स्पष्टता के लिए चर का नाम बदलें।
  • जोड़ा गया Javadocs।

कोड:

/** 
* 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; 
} 
संबंधित मुद्दे