1 /*
   2  * Copyright (c) 2015, 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.BorderLayout;
  25 import java.awt.Color;
  26 import java.awt.Graphics;
  27 import java.awt.Rectangle;
  28 import java.awt.Robot;
  29 import java.awt.event.InputEvent;
  30 import javax.swing.JDesktopPane;
  31 import javax.swing.JFrame;
  32 import javax.swing.JInternalFrame;
  33 import javax.swing.JPanel;
  34 import javax.swing.SwingUtilities;
  35 import static sun.awt.OSInfo.*;
  36 
  37 /**
  38  * @test
  39  * @bug 8069348
  40  * @summary SunGraphics2D.copyArea() does not properly work for scaled graphics
  41  * @author Alexandr Scherbatiy
  42  * @modules java.desktop/sun.awt
  43  * @run main/othervm -Dsun.java2d.uiScale=2 bug8069348
  44  * @run main/othervm -Dsun.java2d.opengl=true -Dsun.java2d.uiScale=2 bug8069348
  45  * @run main/othervm -Dsun.java2d.d3d=true -Dsun.java2d.uiScale=2 bug8069348
  46  */
  47 public class bug8069348 {
  48 
  49     private static final int WIN_WIDTH = 500;
  50     private static final int WIN_HEIGHT = 500;
  51 
  52     private static final Color DESKTOPPANE_COLOR = Color.YELLOW;
  53     private static final Color FRAME_COLOR = Color.ORANGE;
  54 
  55     private static JFrame frame;
  56     private static JInternalFrame internalFrame;
  57 
  58     public static void main(String[] args) throws Exception {
  59 
  60         if (!isSupported()) {
  61             return;
  62         }
  63 
  64         try {
  65 
  66             SwingUtilities.invokeAndWait(bug8069348::createAndShowGUI);
  67 
  68             Robot robot = new Robot();
  69             robot.setAutoDelay(50);
  70             robot.waitForIdle();
  71 
  72             Rectangle screenBounds = getInternalFrameScreenBounds();
  73 
  74             int x = screenBounds.x + screenBounds.width / 2;
  75             int y = screenBounds.y + 10;
  76             int dx = screenBounds.width / 2;
  77             int dy = screenBounds.height / 2;
  78 
  79             robot.mouseMove(x, y);
  80             robot.waitForIdle();
  81 
  82             robot.mousePress(InputEvent.BUTTON1_MASK);
  83             robot.mouseMove(x + dx, y + dy);
  84             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  85             robot.waitForIdle();
  86 
  87             int cx = screenBounds.x + screenBounds.width + dx / 2;
  88             int cy = screenBounds.y + screenBounds.height + dy / 2;
  89 
  90             robot.mouseMove(cx, cy);
  91             if (!FRAME_COLOR.equals(robot.getPixelColor(cx, cy))) {
  92                 throw new RuntimeException("Internal frame is not correctly dragged!");
  93             }
  94         } finally {
  95             if (frame != null) {
  96                 frame.dispose();
  97             }
  98         }
  99     }
 100 
 101     private static boolean isSupported() {
 102         String d3d = System.getProperty("sun.java2d.d3d");
 103         return !Boolean.getBoolean(d3d) || getOSType() == OSType.WINDOWS;
 104     }
 105 
 106     private static Rectangle getInternalFrameScreenBounds() throws Exception {
 107         Rectangle[] points = new Rectangle[1];
 108         SwingUtilities.invokeAndWait(() -> {
 109             points[0] = new Rectangle(internalFrame.getLocationOnScreen(),
 110                     internalFrame.getSize());
 111         });
 112         return points[0];
 113     }
 114 
 115     private static void createAndShowGUI() {
 116 
 117         frame = new JFrame();
 118         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 119 
 120         JDesktopPane desktopPane = new JDesktopPane();
 121         desktopPane.setBackground(DESKTOPPANE_COLOR);
 122 
 123         internalFrame = new JInternalFrame("Test") {
 124 
 125             @Override
 126             public void paint(Graphics g) {
 127                 super.paint(g);
 128                 g.setColor(FRAME_COLOR);
 129                 g.fillRect(0, 0, getWidth(), getHeight());
 130             }
 131         };
 132         internalFrame.setSize(WIN_WIDTH / 3, WIN_HEIGHT / 3);
 133         internalFrame.setVisible(true);
 134         desktopPane.add(internalFrame);
 135 
 136         JPanel panel = new JPanel();
 137         panel.setLayout(new BorderLayout());
 138         panel.add(desktopPane, BorderLayout.CENTER);
 139         frame.add(panel);
 140         frame.setSize(WIN_WIDTH, WIN_HEIGHT);
 141         frame.setVisible(true);
 142         frame.requestFocus();
 143     }
 144 }