क्या कोई जानता है कि आपको जावा में स्क्रीन चौड़ाई कैसे मिलेगी? मैंने कुछ टूलकिट विधि के बारे में कुछ पढ़ा लेकिन मुझे पूरा यकीन नहीं है कि यह क्या है।आप जावा में स्क्रीन चौड़ाई कैसे प्राप्त करते हैं?
धन्यवाद, एंड्रयू
क्या कोई जानता है कि आपको जावा में स्क्रीन चौड़ाई कैसे मिलेगी? मैंने कुछ टूलकिट विधि के बारे में कुछ पढ़ा लेकिन मुझे पूरा यकीन नहीं है कि यह क्या है।आप जावा में स्क्रीन चौड़ाई कैसे प्राप्त करते हैं?
धन्यवाद, एंड्रयू
निम्नलिखित कोड यह करना चाहिए (यह प्रयास नहीं किया है):
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());
}
}
प्रकार का ऑब्जेक्ट देता है नाइस ग्रैब। इसे दिनों के लिए खोज रहे थे, बस SO पर सही खोज मानदंड नहीं मिल सका। –
आप इसे AWT Toolkit का उपयोग करके प्राप्त कर सकते हैं।
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
ओपी शायद कुछ इस तरह थे:
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Toolkit.getDefaultToolkit().getScreenSize().getWidth()
Toolkit वर्गों के एक नंबर है कि मदद मिलेगी है:
हम प्रयोग करने योग्य अधिकतम विंडो आकार की गणना करने के 1 और 2, का उपयोग करते हुए अंत। प्रासंगिक ग्राफिक्स कॉन्फ़िगरेशन प्राप्त करने के लिए, हम
GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDefaultConfiguration();
का उपयोग करते हैं लेकिन स्मार्ट एकाधिक-मॉनीटर समाधान हो सकते हैं।
प्राथमिक मॉनिटर होने के लिए 'getScreenDevices' ** गारंटी ** सरणी में पहला आइटम है? – Pacerier
एपीआई में निर्दिष्ट नहीं है - GetDefaultScreenDevice के साथ जाने के लिए बेहतर है? http://download.oracle.com/javase/6/docs/api/java/awt/GraphicsEnvironment.html#getDefaultScreenDevice() –
क्या आपका मतलब है 'GraphicsEnvironment.getDefaultScreenDevice() '? – Pacerier
यहां दो विधियां हैं जिनका उपयोग मैं कई मॉनीटर और टास्क-बार इन्सेट्स के लिए करता हूं। यदि आपको दो तरीकों से अलग तरीके की आवश्यकता नहीं है, तो आप निश्चित रूप से ग्राफिक्स कॉन्फ़िगरेशन को दो बार प्राप्त करने से बच सकते हैं।
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;
}
+1 इन्सेट को पकड़ने के तरीके के लिए +1। यदि आप डॉक ओवरलैप किए बिना पूर्ण आकार की खिड़की रखना चाहते हैं तो वे विशेष रूप से ओएस एक्स पर महत्वपूर्ण हो सकते हैं। –
@SoftwareMonkey ग्राफिक्स को नया फ्रेम बनाने के बिना वैसे भी कॉन्फ़िगरेशन प्राप्त करने के लिए है? – Pacerier
-1: कोई 'Toolkit.getDefaultToolkit() नहीं है। GetGraphicsConfiguration() ' – jan
कार्य क्षेत्र प्रदर्शन के डेस्कटॉप क्षेत्र का taskbars, डॉक की गई खिड़कियां, और डॉक की गई टूल बार को छोड़कर है।
क्या आप चाहते हैं स्क्रीन के "कार्य क्षेत्र" है, तो इस का उपयोग करें:
public static int GetScreenWorkingWidth() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
}
public static int GetScreenWorkingHeight() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
}
यह बहु मॉनिटर स्थितियों के लिए काम नहीं करता है – jan
आप स्क्रीन के संकल्प कि एक निश्चित घटक वर्तमान में (के अधिकांश भाग की तरह कुछ करने के लिए असाइन किया गया है की जरूरत है रूट स्क्रीन उस स्क्रीन पर दिखाई दे रही है), आप this answer का उपयोग कर सकते हैं।
या नहीं, कुछ का पता लगाने का एक अच्छा तरीका
Screen.getScreensForRectangle(x, y, width, height).isEmpty();
उपयोग कर रहा है इस बहु-मॉनिटर समाधान तैनात (ऊपर) लॉरेंस Dol से करने के लिए एक सुधार है, दृश्य सीमा के भीतर है। अपने समाधान के रूप में, यह कोड एकाधिक मॉनीटर और टास्क-बार इन्सेट्स के लिए खाता है। शामिल कार्य हैं: getScreenInsets(), getScreenWorkingArea(), और getScreenTotalArea()। लॉरेंस Dol संस्करण से
परिवर्तन:
कोड:
/**
* 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;
}
ध्यान दें, अतिरिक्त देखभाल जब एकाधिक मॉनिटर होने लिया जाना चाहिए। –
आपको स्क्रीन इन्सेट के लिए भी खाता होना चाहिए (मेरा जवाब देखें); कई लोग टास्क बार को स्क्रीन के एक या दूसरी तरफ ले जाना पसंद करते हैं। –