-
Bug
-
Resolution: Fixed
-
P2
-
9
-
b161
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8176996 | 10 | Alexandr Scherbatiy | P2 | Resolved | Fixed | b03 |
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);
}
}
---------------
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);
}
}
---------------
- backported by
-
JDK-8176996 Window size is not updated after setting location to display with different DPI
-
- Resolved
-
- relates to
-
JDK-8177137 8175293 breaks Windows build on VS 2010
-
- Resolved
-
-
JDK-8178911 Building of awt_Window.cpp fails on Windows with VS2010
-
- Closed
-
-
JDK-8147440 HiDPI (Windows): Swing components have incorrect sizes after changing display resolution
-
- Resolved
-