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  * @test
  26  * @key headful
  27  * @bug 8149371
  28  * @summary multi-res. image: -Dsun.java2d.uiScale does not work for Window
  29  * icons (some ambiguity for Window.setIconImages()?)
  30  * @run main/othervm/manual -Dsun.java2d.uiScale=2 MultiResIconTest
  31  */
  32 import java.awt.Color;
  33 import java.awt.Graphics;
  34 import java.awt.GridBagConstraints;
  35 import java.awt.GridBagLayout;
  36 import java.awt.event.ActionEvent;
  37 import java.awt.event.ActionListener;
  38 import java.awt.event.WindowAdapter;
  39 import java.awt.event.WindowEvent;
  40 import java.awt.image.BaseMultiResolutionImage;
  41 import java.awt.image.BufferedImage;
  42 import java.util.concurrent.CountDownLatch;
  43 import javax.swing.JButton;
  44 import javax.swing.JDialog;
  45 import javax.swing.JFrame;
  46 import javax.swing.JLabel;
  47 import javax.swing.JPanel;
  48 import javax.swing.SwingUtilities;
  49 
  50 public class MultiResIconTest {
  51 
  52     private static GridBagLayout layout;
  53     private static JPanel mainControlPanel;
  54     private static JPanel resultButtonPanel;
  55     private static JLabel instructionText;
  56     private static JButton passButton;
  57     private static JButton failButton;
  58     private static JDialog f;
  59     private static CountDownLatch latch;
  60     private static TestFrame frame;
  61 
  62     private static BufferedImage generateImage(int x, Color c) {
  63 
  64         BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB);
  65         Graphics g = img.getGraphics();
  66         g.setColor(c);
  67         g.fillRect(0, 0, x, x);
  68         g.setColor(Color.WHITE);
  69         g.fillRect(x / 3, x / 3, x / 3, x / 3);
  70         return img;
  71     }
  72 
  73     public MultiResIconTest() {
  74         try {
  75             latch = new CountDownLatch(1);
  76             createUI();
  77             latch.await();
  78         } catch (Exception ex) {
  79         }
  80     }
  81 
  82     private static void createUI() throws Exception {
  83         SwingUtilities.invokeAndWait(() -> {
  84             frame = new TestFrame();
  85             f = new JDialog(frame);
  86             f.setTitle("Instruction Dialog");
  87             layout = new GridBagLayout();
  88             mainControlPanel = new JPanel(layout);
  89             resultButtonPanel = new JPanel(layout);
  90             GridBagConstraints gbc = new GridBagConstraints();
  91             String instructions
  92                     = "<html>    INSTRUCTIONS:<br><br>"
  93                     + "1) Test frame title icon and frame color should be green."
  94                     + "<br>"
  95                     + "2) Test frame task bar icon should be blue<br>"
  96                     + "3) If color are same as mentioned in 1 and 2 press pass<br>"
  97                     + "   else press fail.<br><br></html>";
  98 
  99             instructionText = new JLabel();
 100             instructionText.setText(instructions);
 101 
 102             gbc.gridx = 0;
 103             gbc.gridy = 0;
 104             gbc.fill = GridBagConstraints.HORIZONTAL;
 105             mainControlPanel.add(instructionText, gbc);
 106             passButton = new JButton("Pass");
 107             passButton.setActionCommand("Pass");
 108             passButton.addActionListener((ActionEvent e) -> {
 109                 latch.countDown();
 110                 f.dispose();
 111                 frame.dispose();
 112             });
 113             failButton = new JButton("Fail");
 114             failButton.setActionCommand("Fail");
 115             failButton.addActionListener(new ActionListener() {
 116                 @Override
 117                 public void actionPerformed(ActionEvent e) {
 118                     latch.countDown();
 119                     f.dispose();
 120                     frame.dispose();
 121                     throw new RuntimeException("Test Failed");
 122                 }
 123             });
 124             gbc.gridx = 1;
 125             gbc.gridy = 0;
 126             resultButtonPanel.add(passButton, gbc);
 127             gbc.gridx = 2;
 128             gbc.gridy = 0;
 129             resultButtonPanel.add(failButton, gbc);
 130 
 131             gbc.gridx = 0;
 132             gbc.gridy = 1;
 133             mainControlPanel.add(resultButtonPanel, gbc);
 134 
 135             f.add(mainControlPanel);
 136             f.setSize(400, 200);
 137             f.setLocationRelativeTo(null);
 138             f.setVisible(true);
 139 
 140             f.addWindowListener(new WindowAdapter() {
 141                 @Override
 142                 public void windowClosing(WindowEvent e) {
 143                     latch.countDown();
 144                     f.dispose();
 145                     frame.dispose();
 146                 }
 147             });
 148         });
 149     }
 150 
 151     private static class TestFrame extends JFrame {
 152 
 153         private static final int W = 200;
 154 
 155         private static final BaseMultiResolutionImage IMG
 156                 = new BaseMultiResolutionImage(
 157                         new BufferedImage[]{generateImage(W, Color.RED),
 158                             generateImage(2 * W, Color.GREEN),
 159                             generateImage(4 * W, Color.BLUE)});
 160 
 161         private static final BaseMultiResolutionImage ICON
 162                 = new BaseMultiResolutionImage(
 163                         new BufferedImage[]{generateImage(16, Color.RED),
 164                             generateImage(32, Color.GREEN),
 165                             generateImage(64, Color.BLUE),
 166                             generateImage(128, Color.BLACK),
 167                             generateImage(256, Color.GRAY)});
 168 
 169         public TestFrame() {
 170             createUI();
 171         }
 172 
 173         private void createUI() {
 174             setTitle("Test Frame");
 175             setIconImage(ICON);
 176             addWindowListener(new WindowAdapter() {
 177                 @Override
 178                 public void windowClosing(WindowEvent e) {
 179                     dispose();
 180                 }
 181             });
 182             setSize(W, W);
 183             setLocation(50, 50);
 184             setResizable(false);
 185             setVisible(true);
 186         }
 187 
 188         @Override
 189         public void paint(Graphics gr) {
 190             gr.drawImage(IMG, 0, 0, this);
 191         }
 192     }
 193 
 194     public static void main(String[] args) {
 195         new MultiResIconTest();
 196     }
 197 }
 198