--- old/src/java.desktop/windows/classes/sun/awt/Win32GraphicsDevice.java 2016-02-04 23:25:25.539080200 -0800 +++ new/src/java.desktop/windows/classes/sun/awt/Win32GraphicsDevice.java 2016-02-04 23:25:25.238453000 -0800 @@ -523,9 +523,9 @@ dynamicColorModel = null; defaultConfig = null; configs = null; + initScaleFactors(); // pass on to all top-level windows on this display topLevels.notifyListeners(); - initScaleFactors(); } /** --- old/src/java.desktop/windows/classes/sun/awt/windows/WWindowPeer.java 2016-02-04 23:25:27.790716900 -0800 +++ new/src/java.desktop/windows/classes/sun/awt/windows/WWindowPeer.java 2016-02-04 23:25:27.496267500 -0800 @@ -81,6 +81,9 @@ */ private WindowListener windowListener; + private float scaleX; + private float scaleY; + /** * Initialize JNI field IDs */ @@ -185,7 +188,10 @@ } // Express our interest in display changes GraphicsConfiguration gc = getGraphicsConfiguration(); - ((Win32GraphicsDevice)gc.getDevice()).addDisplayChangedListener(this); + Win32GraphicsDevice gd = (Win32GraphicsDevice) gc.getDevice(); + (gd).addDisplayChangedListener(this); + scaleX = gd.getDefaultScaleX(); + scaleY = gd.getDefaultScaleY(); initActiveWindowsTracking((Window)target); @@ -529,8 +535,22 @@ AWTAccessor.getComponentAccessor(). setGraphicsConfiguration((Component)target, winGraphicsConfig); + + float newScaleX = newDev.getDefaultScaleX(); + float newScaleY = newDev.getDefaultScaleY(); + + if (scaleX != newScaleX || scaleY != newScaleY) { + setBounds(rescale(sysX, scaleX, newScaleX), + rescale(sysY, scaleY, newScaleY), + sysW, sysH, SET_BOUNDS); + scaleX = newScaleX; + scaleY = newScaleY; + } } + private static int rescale(int position, float oldScale, float newScale) { + return Math.round(position * oldScale / newScale); + } /** * From the DisplayChangedListener interface. * --- /dev/null 2016-02-04 23:25:29.000000000 -0800 +++ new/test/java/awt/Window/WindowResizingOnDPIChanging/WindowResizingOnDPIChangingTest.html 2016-02-04 23:25:29.478455400 -0800 @@ -0,0 +1,46 @@ + + + + + +Verify that window is properly resized after the display DPI size updating. + +The test is applicable for OSes that allows to change the display DPI size +without the system rebooting (like Windows 8.1 and higher). Press PASS for other +systems. + +1. Run the test. +2. Set the display DPI size to 192 (DPI scale factor 200%) +3. Press Show Window button +4. Check that a window with blue image with string "scale 2x" is shown +5. Set the display DPI size to 96 (DPI scale factor 100%) +6. Check that green image with string "scale: 1x" is shown +7. Check that the window is properly resized in the same way as native applications +8. Check that the window is properly repainted and does not contain drawing artifacts +If so, press PASS, else press FAIL. + + + + + --- /dev/null 2016-02-04 23:25:31.000000000 -0800 +++ new/test/java/awt/Window/WindowResizingOnDPIChanging/WindowResizingOnDPIChangingTest.java 2016-02-04 23:25:30.853491200 -0800 @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.awt.Color; +import java.awt.FlowLayout; +import java.awt.Font; +import java.awt.Frame; +import java.awt.Graphics; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.image.BaseMultiResolutionImage; +import java.awt.image.BufferedImage; +import javax.swing.JApplet; +import javax.swing.JButton; +import javax.swing.SwingUtilities; + +/* @test + * @bug 8147440 8147016 + * @summary HiDPI (Windows): Swing components have incorrect sizes after + * changing display resolution + * @author Alexander Scherbatiy + * @run applet/manual=yesno WindowResizingOnDPIChangingTest.html + */ +public class WindowResizingOnDPIChangingTest extends JApplet { + + @Override + public void init() { + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + JButton button = new JButton("Show Window"); + button.addActionListener((e) -> { + + int x = 100; + int y = 200; + int w = 300; + int h = 400; + + final BaseMultiResolutionImage mriImage + = new BaseMultiResolutionImage( + new BufferedImage[]{ + getImage(1, Color.GREEN, w, h), + getImage(2, Color.BLUE, w, h)}); + + final Frame frame = new Frame("Display DPI changing test") { + + @Override + public void paint(Graphics g) { + super.paint(g); + g.drawImage(mriImage, 0, 0, null); + } + }; + frame.setBounds(x, y, w, h); + frame.addWindowListener(new WindowAdapter() { + + @Override + public void windowClosing(WindowEvent e) { + frame.dispose(); + } + }); + frame.setVisible(true); + }); + getContentPane().setLayout(new FlowLayout()); + getContentPane().add(button); + } + }); + } + + private static BufferedImage getImage(int scale, Color c, int w, int h) { + + int sw = scale * w; + int sh = scale * h; + + BufferedImage image = new BufferedImage( + sw, sh, BufferedImage.TYPE_INT_RGB); + Graphics g = image.getGraphics(); + g.setColor(c); + g.fillRect(0, 0, sw, sh); + + g.setColor(Color.WHITE); + Font f = g.getFont(); + g.setFont(new Font(f.getName(), Font.BOLD, scale * 48)); + g.drawString(String.format("scale: %dx", scale), sw / 6, sh / 2); + return image; + } +} \ No newline at end of file