/* * 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; } }