1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.awt.Color; 25 import java.awt.FlowLayout; 26 import java.awt.Font; 27 import java.awt.Frame; 28 import java.awt.Graphics; 29 import java.awt.event.WindowAdapter; 30 import java.awt.event.WindowEvent; 31 import java.awt.image.BaseMultiResolutionImage; 32 import java.awt.image.BufferedImage; 33 import javax.swing.JApplet; 34 import javax.swing.JButton; 35 import javax.swing.SwingUtilities; 36 37 /* @test 38 * @bug 8147440 8147016 39 * @summary HiDPI (Windows): Swing components have incorrect sizes after 40 * changing display resolution 41 * @author Alexander Scherbatiy 42 * @run applet/manual=yesno WindowResizingOnDPIChangingTest.html 43 */ 44 public class WindowResizingOnDPIChangingTest extends JApplet { 45 46 @Override 47 public void init() { 48 SwingUtilities.invokeLater(new Runnable() { 49 50 @Override 51 public void run() { 52 JButton button = new JButton("Show Window"); 53 button.addActionListener((e) -> { 54 55 int x = 100; 56 int y = 200; 57 int w = 300; 58 int h = 400; 59 60 final BaseMultiResolutionImage mriImage 61 = new BaseMultiResolutionImage( 62 new BufferedImage[]{ 63 getImage(1, Color.GREEN, w, h), 64 getImage(2, Color.BLUE, w, h)}); 65 66 final Frame frame = new Frame("Display DPI changing test") { 67 68 @Override 69 public void paint(Graphics g) { 70 super.paint(g); 71 g.drawImage(mriImage, 0, 0, null); 72 } 73 }; 74 frame.setBounds(x, y, w, h); 75 frame.addWindowListener(new WindowAdapter() { 76 77 @Override 78 public void windowClosing(WindowEvent e) { 79 frame.dispose(); 80 } 81 }); 82 frame.setVisible(true); 83 }); 84 getContentPane().setLayout(new FlowLayout()); 85 getContentPane().add(button); 86 } 87 }); 88 } 89 90 private static BufferedImage getImage(int scale, Color c, int w, int h) { 91 92 int sw = scale * w; 93 int sh = scale * h; 94 95 BufferedImage image = new BufferedImage( 96 sw, sh, BufferedImage.TYPE_INT_RGB); 97 Graphics g = image.getGraphics(); 98 g.setColor(c); 99 g.fillRect(0, 0, sw, sh); 100 101 g.setColor(Color.WHITE); 102 Font f = g.getFont(); 103 g.setFont(new Font(f.getName(), Font.BOLD, scale * 48)); 104 g.drawString(String.format("scale: %dx", scale), sw / 6, sh / 2); 105 return image; 106 } 107 }