Skip to content

Commit 6c9e11e

Browse files
ShahzaibIbrahimakoch-yatta
authored andcommitted
Replacing usages of Display#getDPI in snippets
Having the scale factor being based on the screen DPI leads to unexpected result e.g. Image too big/small. Having a screen dpi independent factor leads to consistent results
1 parent f005865 commit 6c9e11e

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,16 +1166,16 @@ void menuPrint() {
11661166
if (image == null) return;
11671167

11681168
try {
1169+
final int DOTS_PER_INCH = 96;
11691170
// Ask the user to specify the printer.
11701171
PrintDialog dialog = new PrintDialog(shell, SWT.NONE);
11711172
if (printerData != null) dialog.setPrinterData(printerData);
11721173
printerData = dialog.open();
11731174
if (printerData == null) return;
11741175

11751176
Printer printer = new Printer(printerData);
1176-
Point screenDPI = display.getDPI();
11771177
Point printerDPI = printer.getDPI();
1178-
int scaleFactor = printerDPI.x / screenDPI.x;
1178+
int scaleFactor = printerDPI.x / DOTS_PER_INCH;
11791179
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
11801180
if (printer.startJob(currentName)) {
11811181
if (printer.startPage()) {

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet361.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public void getName(AccessibleEvent e) {
146146
}
147147

148148
private static void performPrintAction(final Display display, final Shell shell) {
149+
final int DOTS_PER_INCH = 96;
149150
Rectangle r = composite.getBounds();
150151
Point p = shell.toDisplay(r.x, r.y);
151152
org.eclipse.swt.graphics.Image snapshotImage
@@ -159,9 +160,8 @@ private static void performPrintAction(final Display display, final Shell shell)
159160
data = dialog.open();
160161
if (data != null) {
161162
Printer printer = new Printer(data);
162-
Point screenDPI = display.getDPI();
163163
Point printerDPI = printer.getDPI();
164-
int scaleFactor = printerDPI.x / screenDPI.x;
164+
int scaleFactor = printerDPI.x / DOTS_PER_INCH;
165165
Rectangle trim = printer.computeTrim(0, 0, 0, 0);
166166
if (printer.startJob("Print Image")) {
167167
ImageData imageData = snapshotImage.getImageData();

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public int getGcStyle() {
185185

186186
createSeparator(shell);
187187

188-
new Label (shell, SWT.NONE).setText ("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")");
188+
new Label (shell, SWT.NONE).setText ("5. 50x50 box");
189189
Label box= new Label (shell, SWT.NONE);
190190
box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
191191
box.setLayoutData (new GridData (50, 50));

0 commit comments

Comments
 (0)