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