1 /*
   2  * Copyright (c) 2014, 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 import java.awt.BorderLayout;
  24 import java.awt.Canvas;
  25 import java.awt.Dimension;
  26 import java.awt.Graphics;
  27 import java.awt.Graphics2D;
  28 import java.awt.Image;
  29 import java.awt.image.BufferedImage;
  30 import javax.swing.JApplet;
  31 import javax.swing.JCheckBox;
  32 import javax.swing.JComponent;
  33 import javax.swing.SwingUtilities;
  34 
  35 /* @test
  36  * @bug 8032667
  37  * @summary [macosx] Components cannot be rendered in HiDPI to BufferedImage
  38  * @run applet/manual=yesno bug8032667.html
  39  */
  40 public class bug8032667 extends JApplet {
  41 
  42     static final int scale = 2;
  43     static final int width = 130;
  44     static final int height = 50;
  45     static final int scaledWidth = scale * width;
  46     static final int scaledHeight = scale * height;
  47 
  48     @Override
  49     public void init() {
  50         SwingUtilities.invokeLater(new Runnable() {
  51 
  52             @Override
  53             public void run() {
  54 
  55                 final Image image1 = getImage(getCheckBox("Deselected", false));
  56                 final Image image2 = getImage(getCheckBox("Selected", true));
  57 
  58                 Canvas canvas = new Canvas() {
  59 
  60                     @Override
  61                     public void paint(Graphics g) {
  62                         super.paint(g);
  63                         g.drawImage(image1, 0, 0, scaledWidth, scaledHeight, this);
  64                         g.drawImage(image2, 0, scaledHeight + 5,
  65                                 scaledWidth, scaledHeight, this);
  66                     }
  67                 };
  68 
  69                 getContentPane().add(canvas, BorderLayout.CENTER);
  70             }
  71         });
  72     }
  73 
  74     static JCheckBox getCheckBox(String text, boolean selected) {
  75         JCheckBox checkBox = new JCheckBox(text);
  76         checkBox.setSelected(selected);
  77         checkBox.setSize(new Dimension(width, height));
  78         return checkBox;
  79     }
  80 
  81     static Image getImage(JComponent component) {
  82         final BufferedImage image = new BufferedImage(
  83                 scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
  84         final Graphics g = image.getGraphics();
  85         ((Graphics2D) g).scale(scale, scale);
  86         component.paint(g);
  87         g.dispose();
  88 
  89         return image;
  90     }
  91 }