Bagaimana seseorang bisa mendapatkan resolusi layar (lebar x tinggi) dalam piksel?
Saya menggunakan JFrame dan metode ayunan java.
java
swing
screen
resolution
AndreDurao
sumber
sumber
Jawaban:
Anda bisa mendapatkan ukuran layar dengan
Toolkit.getScreenSize()
metode ini.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); double width = screenSize.getWidth(); double height = screenSize.getHeight();
Pada konfigurasi multi-monitor, Anda harus menggunakan ini:
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); int width = gd.getDisplayMode().getWidth(); int height = gd.getDisplayMode().getHeight();
Jika Anda ingin mendapatkan resolusi layar dalam DPI, Anda harus menggunakan
getScreenResolution()
metode iniToolkit
.Sumber:
sumber
getScreenSize
mengembalikan 1920x1080.Kode ini akan menghitung perangkat grafis pada sistem (jika beberapa monitor dipasang), dan Anda dapat menggunakan informasi tersebut untuk menentukan afinitas monitor atau penempatan otomatis (beberapa sistem menggunakan monitor samping kecil untuk tampilan waktu nyata saat aplikasi berjalan di latar belakang, dan monitor semacam itu dapat diidentifikasi berdasarkan ukuran, warna layar, dll.):
// 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 }
sumber
Panggilan ini akan memberi Anda informasi yang Anda inginkan.
sumber
Berikut beberapa kode fungsional (Java 8) yang mengembalikan posisi x dari tepi paling kanan dari layar paling kanan. Jika tidak ada layar yang ditemukan, maka ia mengembalikan 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);
Berikut adalah tautan ke JavaDoc.
GraphicsEnvironment.getLocalGraphicsEnvironment ()
GraphicsEnvironment.getScreenDevices ()
GraphicsDevice.getDefaultConfiguration ()
GraphicsConfiguration.getBounds ()
sumber
Ini adalah resolusi layar yang saat ini diberikan komponen tertentu (sesuatu seperti sebagian besar jendela root terlihat di layar itu).
public Rectangle getCurrentScreenBounds(Component component) { return component.getGraphicsConfiguration().getBounds(); }
Pemakaian:
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
Jika Anda ingin menghormati bilah alat, dll. Anda juga harus menghitung dengan ini:
sumber
Ketiga fungsi ini mengembalikan ukuran layar di Java. Kode ini menjelaskan pengaturan multi-monitor dan bilah tugas. Fungsi yang disertakan adalah: getScreenInsets () , getScreenWorkingArea () , dan getScreenTotalArea () .
Kode:
/** * 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; }
sumber
int resolution =Toolkit.getDefaultToolkit().getScreenResolution(); System.out.println(resolution);
sumber
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);
sumber
Berikut adalah potongan kode yang sering saya gunakan. Ini mengembalikan area layar penuh yang tersedia (bahkan pada pengaturan multi-monitor) sambil mempertahankan posisi monitor asli.
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); }
Di komputer dengan dua monitor full-HD, di mana monitor kiri ditetapkan sebagai monitor utama (dalam pengaturan Windows), fungsi ini kembali
java.awt.Rectangle[x=0,y=0,width=3840,height=1080]
Pada pengaturan yang sama, tetapi dengan monitor kanan ditetapkan sebagai monitor utama, fungsi tersebut kembali
java.awt.Rectangle[x=-1920,y=0,width=3840,height=1080]
sumber
int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution(); System.out.println(""+screenResolution);
sumber