在 Java 中更改屏幕分辨率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11225113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:17:59  来源:igfitidea点击:

Change screen resolution in Java

javaswingscreen-resolutionsikuli

提问by Jamesbraders

I have a program that uses visual scripting (sikuli-script) to allow the users to create their own visual scripts. The program needs to work across multiple systems that could have different screen resolutions.

我有一个程序,它使用可视化脚本 ( sikuli-script) 来允许用户创建自己的可视化脚本。该程序需要在可能具有不同屏幕分辨率的多个系统上运行。

Sikuli matches highlighted images on a pixel-by-pixel basis, so on systems with different resolutions will fail to find images.

Sikuli 逐个像素地匹配突出显示的图像,因此在具有不同分辨率的系统上将无法找到图像。

Therefore is there a way that I can change the resolution settings in windows through java code?

因此,有没有办法通过java代码更改Windows中的分辨率设置?

Going full-screen is not an option as images that need to be captured come from different software packages, i.e. my software package sits above where the images need to come from (and is minimized when a capture is taking place)

全屏不是一种选择,因为需要捕获的图像来自不同的软件包,即我的软件包位于图像需要来自的位置上方(并且在进行捕获时最小化)

回答by mKorbel

Therefore is there a way that I can change the resolution settings in windows through java code?

因此,有没有办法通过java代码更改Windows中的分辨率设置?

1.yes is possible, why not

1.是的,为什么不可以

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.image.BufferStrategy;

public class MultiBufferTest {

    private Frame mainFrame;
    private static Color[] COLORS = new Color[]{
        Color.red, Color.blue, Color.green, Color.white, Color.black,
        Color.yellow, Color.gray, Color.cyan, Color.pink, Color.lightGray,
        Color.magenta, Color.orange, Color.darkGray};
    private static DisplayMode[] BEST_DISPLAY_MODES = new DisplayMode[]{
        new DisplayMode(640, 480, 32, 0),
        new DisplayMode(640, 480, 16, 0),
        new DisplayMode(640, 480, 8, 0)};

    public MultiBufferTest(int numBuffers, GraphicsDevice device) {
        try {
            GraphicsConfiguration gc = device.getDefaultConfiguration();
            mainFrame = new Frame(gc);
            mainFrame.setUndecorated(true);
            mainFrame.setIgnoreRepaint(true);
            device.setFullScreenWindow(mainFrame);
            if (device.isDisplayChangeSupported()) {
                chooseBestDisplayMode(device);
            }
            Rectangle bounds = mainFrame.getBounds();
            mainFrame.createBufferStrategy(numBuffers);
            BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();
            for (float lag = 2000.0f; lag > 0.00000006f; lag = lag / 1.33f) {
                for (int i = 0; i < numBuffers; i++) {
                    Graphics g = bufferStrategy.getDrawGraphics();
                    if (!bufferStrategy.contentsLost()) {
                        g.setColor(COLORS[i]);
                        g.fillRect(0, 0, bounds.width, bounds.height);
                        bufferStrategy.show();
                        g.dispose();
                    }
                    try {
                        Thread.sleep((int) lag);
                    } catch (InterruptedException e) {
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            device.setFullScreenWindow(null);
        }
    }

    private static DisplayMode getBestDisplayMode(GraphicsDevice device) {
        for (int x = 0; x < BEST_DISPLAY_MODES.length; x++) {
            DisplayMode[] modes = device.getDisplayModes();
            for (int i = 0; i < modes.length; i++) {
                if (modes[i].getWidth() == BEST_DISPLAY_MODES[x].getWidth()
                        && modes[i].getHeight() == BEST_DISPLAY_MODES[x].getHeight()
                        && modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth()) {
                    return BEST_DISPLAY_MODES[x];
                }
            }
        }
        return null;
    }

    public static void chooseBestDisplayMode(GraphicsDevice device) {
        DisplayMode best = getBestDisplayMode(device);
        if (best != null) {
            device.setDisplayMode(best);
        }
    }

    public static void main(String[] args) {
        try {
            int numBuffers = 2;
            if (args != null && args.length > 0) {
                numBuffers = Integer.parseInt(args[0]);
                if (numBuffers < 2 || numBuffers > COLORS.length) {
                    System.err.println("Must specify between 2 and " + COLORS.length + " buffers");
                    System.exit(1);
                }
            }
            GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice device = env.getDefaultScreenDevice();
            MultiBufferTest test = new MultiBufferTest(numBuffers, device);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.exit(0);
    }
}

2.don't do that, could annoying users, and on code lack can to change resolution in Native OS(my coworker has more than 100shortcuts on three screen, any change for resolution to change shotcuts sizeand location, result is simple mess)

2.不要做,可能恼人的用户,并在代码缺乏能在变化的决议Native OS(我的同事有三个以上的屏幕上100shortcuts,对分辨率的变化shotcuts任何改变sizelocation,结果是一团糟简单)

3.use JLabelfor Icons(layed with proper LayoutManagerinto container), or put Iconsto the JListor JTable, then you never / don't care about something

3.使用JLabelIcons(适当奠定LayoutManager倒入容器),或放IconsJListJTable,那么你永远/不关心的东西

4.use only LayoutManagerrather than possitioning or change for screen resolution, you can set for smaller Size(for JFrame) on the screen for Apps start_up, let's user decide about final Dimmensionon her/his screen

4.只使用LayoutManager而不是定位或改变屏幕分辨率,您可以在屏幕上设置更小Size(for JFrameApps start_up,让用户决定Dimmension她/他的屏幕上的最终结果