Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8175293

Window size is not updated after setting location to display with different DPI

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P2
    • 9
    • 9
    • client-libs

    Backports

      Description

        The issue is reproduced on Windows with 2 displays which have different DPI.

        Steps to reproduce:
        - Run the code below
        The frame appears. It shows the current display scale and the frame size.
        - Press the "Move to another screen" button

        The window location is set to another display.
        The window size is not updated according to the new display DPI scale.
        The window native size should be java size * uiScale.
        The window native size should be updated after moving the window to the another display with different DPI.

        ---------------
        import java.awt.Color;
        import java.awt.FlowLayout;
        import java.awt.Graphics;
        import java.awt.GraphicsConfiguration;
        import java.awt.GraphicsDevice;
        import java.awt.GraphicsEnvironment;
        import java.awt.Rectangle;
        import java.awt.event.WindowAdapter;
        import java.awt.event.WindowEvent;
        import java.awt.geom.AffineTransform;
        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.SwingUtilities;

        public class SimpleDPITest {

            public static void main(String[] args) {

                SwingUtilities.invokeLater(SimpleDPITest::creteAndShowGUI);
            }

            private static void creteAndShowGUI() {

                final JFrame frame = new JFrame() {

                    @Override
                    public void paint(Graphics g) {
                        super.paint(g);
                        g.setColor(Color.BLUE);
                        g.setFont(g.getFont().deriveFont(32f));
                        AffineTransform tx = getGraphicsConfiguration()
                                .getDefaultTransform();

                        g.drawString(String.format("Scale: [%2.1f, %2.1f]", tx.getScaleX(), tx.getScaleY()), 50, 200);
                        g.drawString(String.format("Size: [%d, %d]", getWidth(), getHeight()), 50, 250);
                    }
                };

                frame.setSize(400, 400);
                frame.addWindowListener(new WindowAdapter() {

                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });

                JButton button = new JButton("Move to another screen");
                button.addActionListener((e) -> {
                    GraphicsConfiguration config = frame.getGraphicsConfiguration();
                    GraphicsDevice device = config.getDevice();

                    GraphicsDevice[] devices = GraphicsEnvironment
                            .getLocalGraphicsEnvironment()
                            .getScreenDevices();

                    boolean found = false;
                    for (GraphicsDevice dev : devices) {
                        if (!dev.equals(device)) {
                            found = true;
                            Rectangle bounds = dev.getDefaultConfiguration().getBounds();
                            System.out.printf("bounds: %s\n", bounds);
                            AffineTransform tx = config.getDefaultTransform();
                            int x = (int) Math.round(bounds.x / tx.getScaleX()) + 15;
                            int y = (int) Math.round(bounds.y / tx.getScaleY()) + 15;
                            frame.setLocation(x, y);
                            break;
                        }
                    }

                    if (!found) {
                        System.out.println("Another display not found!");
                    }
                });

                frame.setLayout(new FlowLayout());

                frame.add(button);
                frame.setVisible(true);
            }
        }
        ---------------

        Attachments

          Issue Links

            Activity

              People

                alexsch Alexandr Scherbatiy
                alexsch Alexandr Scherbatiy
                Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: