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 
  25 /*
  26   @test
  27   @bug 8142861 8143062 8147016
  28   @summary Check if multiresolution image behaves properly
  29            on HiDPI + non-HiDPI display pair.
  30   @author a.stepanov
  31   @library /lib/testlibrary
  32   @build jdk.testlibrary.OSInfo
  33   @run applet/manual=yesno MultiDisplayTest.html
  34 */
  35 
  36 
  37 import java.applet.Applet;
  38 import java.awt.*;
  39 import java.awt.event.*;
  40 import java.awt.image.*;
  41 import jdk.testlibrary.OSInfo;
  42 
  43 
  44 public class MultiDisplayTest extends Applet {
  45 
  46     private static final int W = 200, H = 200;
  47 
  48     private static final BaseMultiResolutionImage IMG =
  49         new BaseMultiResolutionImage(new BufferedImage[]{
  50         generateImage(1, Color.BLACK), generateImage(2, Color.BLUE)});
  51 
  52     private static boolean checkOS() {
  53         OSInfo.OSType os = OSInfo.getOSType();
  54         return (os.equals(OSInfo.OSType.WINDOWS) ||
  55             os.equals(OSInfo.OSType.MACOSX));
  56     }
  57 
  58     public void init() { this.setLayout(new BorderLayout()); }
  59 
  60     public void start() {
  61 
  62         Button b = new Button("Start");
  63         b.setEnabled(checkOS());
  64 
  65         b.addActionListener(new ActionListener() {
  66             @Override
  67             public void actionPerformed(ActionEvent e) {
  68 
  69                 ParentFrame p = new ParentFrame();
  70                 new ChildDialog(p);
  71             }
  72         });
  73 
  74         add(b, BorderLayout.CENTER);
  75 
  76         validate();
  77         setVisible(true);
  78     }
  79 
  80 
  81     private static BufferedImage generateImage(int scale, Color c) {
  82 
  83         BufferedImage image = new BufferedImage(
  84             scale * W, scale * H, BufferedImage.TYPE_INT_RGB);
  85         Graphics g = image.getGraphics();
  86         g.setColor(c);
  87         g.fillRect(0, 0, scale * W, scale * H);
  88 
  89         g.setColor(Color.WHITE);
  90         Font f = g.getFont();
  91         g.setFont(new Font(f.getName(), Font.BOLD, scale * 48));
  92         g.drawChars((scale + "X").toCharArray(), 0, 2, scale * W / 2, scale * H / 2);
  93 
  94         return image;
  95     }
  96 
  97     private static class ParentFrame extends Frame {
  98 
  99         public ParentFrame() {
 100             EventQueue.invokeLater(this::CreateUI);
 101         }
 102 
 103         private void CreateUI() {
 104 
 105             addWindowListener(new WindowAdapter() {
 106                 @Override
 107                 public void windowClosing(WindowEvent e) { dispose(); }
 108             });
 109             setSize(W, H);
 110             setLocation(50, 50);
 111             setTitle("parent");
 112             setResizable(false);
 113             setVisible(true);
 114         }
 115 
 116         @Override
 117         public void paint(Graphics gr) {
 118 
 119             Graphics2D g = (Graphics2D) gr;
 120             if (g != null) { g.drawImage(IMG, 0, 0, this); }
 121         }
 122     }
 123 
 124     private static class ChildDialog extends Dialog {
 125 
 126         public ChildDialog(Frame f) {
 127             super(f);
 128             EventQueue.invokeLater(this::CreateUI);
 129         }
 130 
 131         private void CreateUI() {
 132 
 133             addWindowListener(new WindowAdapter() {
 134                 @Override
 135                 public void windowClosing(WindowEvent e) { dispose(); }
 136             });
 137             setSize(W, H);
 138             setTitle("child");
 139             setResizable(false);
 140             setModal(true);
 141             setVisible(true);
 142         }
 143 
 144         @Override
 145         public void paint(Graphics gr) {
 146 
 147             Graphics2D g = (Graphics2D) gr;
 148             if (g != null) { g.drawImage(IMG, 0, 0, this); }
 149         }
 150     }
 151 }