ventana toda tamaño resolucion redimensionar que para pantalla obtener minimizar maximizar los cualquier componentes codigo aplicacion ajustar adaptar abarque java swing screen resolution

toda - ¿Cómo puedo obtener una resolución de pantalla en java?



obtener resolucion de pantalla java (10)

Aquí hay un código funcional (Java 8) que devuelve la posición x del borde derecho de la pantalla más a la derecha. Si no se encuentran pantallas, entonces devuelve 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);

Aquí hay enlaces a JavaDoc.

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

¿Cómo se puede obtener la resolución de la pantalla (ancho x alto) en píxeles?

Estoy usando un JFrame y los métodos de swing de Java.


Aquí hay un fragmento de código que uso a menudo. Devuelve el área de pantalla completa disponible (incluso en configuraciones de monitores múltiples) mientras conserva las posiciones del monitor nativo.

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

En una computadora con dos monitores Full HD, donde el izquierdo está configurado como el monitor principal (en la configuración de Windows), la función regresa

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

En la misma configuración, pero con el monitor correcto configurado como monitor principal, la función regresa

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


Esta es la resolución de la pantalla que el componente dado está asignado actualmente (algo así como la mayor parte de la ventana raíz es visible en esa pantalla).

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

Uso:

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

Si desea respetar las barras de herramientas, etc., deberá calcular con esto también:

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


Esta llamada le dará la información que desea.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();


Estas tres funciones devuelven el tamaño de la pantalla en Java. Este código cuenta para configuraciones de múltiples monitores y barras de tareas. Las funciones incluidas son: getScreenInsets () , getScreenWorkingArea () y getScreenTotalArea () .

Código:

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


Este código enumerará los dispositivos gráficos en el sistema (si hay instalados varios monitores) y puede usar esa información para determinar la afinidad o ubicación automática del monitor (algunos sistemas usan un pequeño monitor lateral para pantallas en tiempo real mientras se ejecuta una aplicación). el fondo, y dicho monitor se puede identificar por tamaño, colores de pantalla, 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 }


Puede obtener el tamaño de pantalla con el método Toolkit.getScreenSize() .

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

En una configuración de monitores múltiples, debe usar esto:

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

Si desea obtener la resolución de pantalla en DPI, deberá usar el método getScreenResolution() en el Toolkit .

Recursos:


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


int resolution =Toolkit.getDefaultToolkit().getScreenResolution(); System.out.println(resolution);


int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution(); System.out.println(""+screenResolution);