Jak uzyskać rozdzielczość ekranu w Javie?
Jak można uzyskać rozdzielczość ekranu (szerokość x wysokość) w pikselach?
Używam JFrame i java swing.
10 answers
Możesz uzyskać rozmiar ekranu za pomocą metody Toolkit.getScreenSize()
.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
W konfiguracji wielomonitorowej powinieneś użyć tego :
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
Jeśli chcesz uzyskać rozdzielczość ekranu w DPI, musisz użyć metody getScreenResolution()
na Toolkit
.
Zasoby:
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-04 20:08:17
Ten kod wyliczy urządzenia graficzne w systemie (jeśli zainstalowanych jest wiele monitorów) i możesz użyć tych informacji do określenia powinowactwa monitora lub automatycznego umieszczenia (niektóre systemy używają małego monitora bocznego do wyświetlania w czasie rzeczywistym, gdy aplikacja jest uruchomiona w tle, a taki monitor można zidentyfikować po rozmiarze, kolorach ekranu itp.):
// 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
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-17 13:55:52
Ten Telefon da ci informacje, które chcesz.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-09 20:15:36
Jest to rozdzielczość ekranu, którą dany komponent jest obecnie przypisany (coś jak większość okna głównego jest widoczna na tym ekranie).
public Rectangle getCurrentScreenBounds(Component component) {
return component.getGraphicsConfiguration().getBounds();
}
Użycie:
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
Jeśli chcesz respektować paski narzędzi itp. z tym też będziesz musiał obliczyć:
GraphicsConfiguration gc = component.getGraphicsConfiguration();
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-20 13:08:29
Oto kod funkcjonalny (Java 8), który zwraca pozycję x prawej krawędzi ekranu. Jeżeli nie znaleziono żadnych ekranów, wtedy zwraca 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);
Oto linki do JavaDoc.
/ align = "left" / getlocalgraphicśrodowisko()
/ align = "left" / getScreenDevices()
GraphicsDevice.getDefaultConfiguration()
Konfiguracja graficzna.getBounds()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-05 23:22:59
int resolution =Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(resolution);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-09 20:17:18
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);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-06 12:48:21
Te trzy funkcje zwracają rozmiar ekranu w języku Java. Ten kod obejmuje konfiguracje wielu monitorów i paski zadań. Dołączone funkcje to: getScreenInsets(), getScreenWorkingArea () i getScreenTotalArea () .
Kod:
/**
* 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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-04 19:49:11
Oto fragment kodu, którego często używam. Zwraca pełny dostępny obszar ekranu (nawet w przypadku konfiguracji wielomonitorowych), zachowując natywne pozycje monitora.
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);
}
Na komputerze z dwoma monitorami full-HD, gdzie lewy jest ustawiony jako główny monitor (w ustawieniach systemu Windows), funkcja zwraca
java.awt.Rectangle[x=0,y=0,width=3840,height=1080]
W tej samej konfiguracji, ale z odpowiednim ustawieniem monitora jako głównego, funkcja zwraca
java.awt.Rectangle[x=-1920,y=0,width=3840,height=1080]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-05 11:22:17
int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println(""+screenResolution);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-20 14:25:52