
SwingComponentWrapper: auto dpi-scaling broken
... at least as I understand the intention of BASE_SCALE_FACTOR and the implementation of the fallback to dpi if no explicit scale factors are set :-)
Here's a failing test case:
Code:
/**
* Auto-DPI-scaling not working.
*/
@Test
public void testDPIScaling() {
if (Toolkit.getDefaultToolkit().getScreenResolution() == PlatformDefaults.getDefaultDPI()) {
LOG.info("dpi == default, nothing to test: " +
Toolkit.getDefaultToolkit().getScreenResolution());
return;
}
float factor = (float) Toolkit.getDefaultToolkit().getScreenResolution() / PlatformDefaults.getDefaultDPI();
SwingComponentWrapper wrapper = new SwingComponentWrapper(new JButton());
assertEquals("dpi scaling factor", factor, wrapper.getPixelUnitFactor(true));
}
Reason is the implementation of its method getHorizontalScreenDPI (same for vertical):
Code:
public final int getHorizontalScreenDPI()
{
return PlatformDefaults.getDefaultDPI();
}
which is used in the scaling method like:
Code:
return (isHor ? getHorizontalScreenDPI() : getVerticalScreenDPI()) / (float) PlatformDefaults.getDefaultDPI();
which then always is ... 1.
Fix is to implement the getHorizontalScreenDPI to return the toolkit's screenResolution
Code:
public final int getHorizontalScreenDPI() {
try {
return c.getToolkit().getScreenResolution();
} catch (HeadlessException ex) {
return PlatformDefaults.getDefaultDPI();
}
}
Cheers
Jeanette