1

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.

https://prnt.sc/10uiez6

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.

https://prnt.sc/10uig1w

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%.

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?

HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37

1 Answers1

1

One solution to the problem is to fix the scaling in the 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

HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37