我有一个使用可视化脚本(sikuli-script)的程序,允许用户创建自己的可视脚本。该程序需要跨多个可能具有不同屏幕分辨率的系统工作。
Sikuli在逐个像素的基础上匹配突出显示的图像,因此在具有不同分辨率的系统上将无法找到图像。
因此有没有办法可以通过java代码更改windows中的分辨率设置?
全屏显示不是一个选项,因为需要捕获的图像来自不同的软件包,即我的软件包位于图像需要来自的上方(并且在捕获时最小化)
答案 0 :(得分:4)
因此有没有办法可以通过java代码更改windows中的分辨率设置?
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.不要这样做,可能会让用户烦恼,而且代码缺乏可以改变Native OS
中的分辨率(我的同事在三个屏幕上有超过100次切换,任何更改分辨率以更改镜头{{1 }和size
,结果很简单)
3.使用location
JLabel
(使用适当的Icons
放入容器中),或将LayoutManager
放入Icons
或JList
那么你永远不会/不关心什么
4.仅使用JTable
而不是为屏幕分辨率设置或更改,您可以在屏幕上为LayoutManager
设置较小的Size
(对于JFrame
),让我们用户在她/他的屏幕上决定最终Apps start_up