I'm drawing a minimap for a game and every 40x40 pixels in the game is a pixel on the minimap.
The problem starts when I have a screen which uses windows 10 scaling for example of 125% scaling.
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.
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.
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%.
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?