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