[英]How to draw adjacents single pixels with windows 10 scaling of over 100% in java awt graphics
I'm drawing a minimap for a game and every 40x40 pixels in the game is a pixel on the minimap.我正在为游戏绘制小地图,游戏中每 40x40 像素就是小地图上的一个像素。
The problem starts when I have a screen which uses windows 10 scaling for example of 125% scaling.当我有一个使用 windows 10 缩放(例如 125% 缩放)的屏幕时,问题就开始了。
Pixels drawn with g.drawLine(x,y,x,y)
fill 1 pixel in a 2x2 space with scaling 125%, but are adjacent with 100% scaling.使用
g.drawLine(x,y,x,y)
绘制的像素在 2x2 空间中填充 1 个像素,缩放比例为 125%,但相邻比例为 100%。
https://prnt.sc/10uiez6 https://prnt.sc/10uiez6
https://prnt.sc/10uieai https://prnt.sc/10uieai
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class WindowsScaleTesting {
public static void main(final String[] args) {
final JFrame frame = new JFrame("windows 10 scaling minimum example line");
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
final JPanel board = new JPanel() {
@Override
public void paint(final Graphics g) {
for(int x = 0; x < 100; x++) {
for(int y = 0; y < 100; y++) {
g.setColor(Color.gray);
g.drawLine(x, y, x, y);
}
}
}
};
board.setSize((1900),(1070));
board.setDoubleBuffered(true);
frame.add(board);
frame.setSize((1900),(1070));
frame.setVisible(true);
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
}
Pixels drawn with g.drawRect(x,y,1,1)
fill 3 pixels in a 2x2 space with scaling 125%, but are adjacent with 100% scaling.使用
g.drawRect(x,y,1,1)
绘制的像素在 2x2 空间中填充 3 个像素,缩放比例为 125%,但相邻比例为 100%。
https://prnt.sc/10uig1w https://prnt.sc/10uig1w
https://prnt.sc/10uieai https://prnt.sc/10uieai
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class WindowsScaleTesting2 {
public static void main(final String[] args) {
final JFrame frame = new JFrame("windows 10 scaling minimum example rectangle");
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
final JPanel board = new JPanel() {
@Override
public void paint(final Graphics g) {
for(int x = 0; x < 100; x++) {
for(int y = 0; y < 100; y++) {
g.setColor(Color.gray);
g.drawRect(x, y, 1, 1);
}
}
}
};
board.setSize((1900),(1070));
board.setDoubleBuffered(true);
frame.add(board);
frame.setSize((1900),(1070));
frame.setVisible(true);
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
}
To reproduce the pictures above you need 2 screens with different scaling (100% and 125%) or you need to have 1 screen and switch the scaling from 100% to 125%.要重现上面的图片,您需要 2 个具有不同缩放比例的屏幕(100% 和 125%),或者您需要 1 个屏幕并将缩放比例从 100% 切换到 125%。
How do I draw pixels with no spaces in between on 125% or other scaling and how do I recognize windows 10 scaling in java?如何在 125% 或其他缩放之间绘制没有空格的像素,以及如何识别 java 中的 windows 10 缩放?
One solution to the problem is to fix the scaling in the vm arguments.该问题的一种解决方案是修复 vm arguments 中的缩放。
Adding -Dsun.java2d.uiScale=1.0
as a vm argument to the programm in eclipse solves the scaling issue, credits to Christian Hujer in this answer https://superuser.com/a/1194728/377633在 eclipse 中的程序中添加
-Dsun.java2d.uiScale=1.0
作为 vm 参数解决了缩放问题,在这个答案https://superuser.com/a/1194728/37736 中归功于 Christian Hujer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.