From 6c9e11ef99fdcb0faf53ddcbcbed36774fe8ad01 Mon Sep 17 00:00:00 2001 From: Shahzaib Ibrahim Date: Wed, 10 Dec 2025 13:30:23 +0100 Subject: [PATCH] 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 --- .../org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java | 4 ++-- .../src/org/eclipse/swt/snippets/Snippet361.java | 4 ++-- .../src/org/eclipse/swt/snippets/Snippet367.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java index 1fe40e833f8..25d505a822f 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/imageanalyzer/ImageAnalyzer.java @@ -1166,6 +1166,7 @@ void menuPrint() { if (image == null) return; try { + final int DOTS_PER_INCH = 96; // Ask the user to specify the printer. PrintDialog dialog = new PrintDialog(shell, SWT.NONE); if (printerData != null) dialog.setPrinterData(printerData); @@ -1173,9 +1174,8 @@ void menuPrint() { if (printerData == null) return; Printer printer = new Printer(printerData); - Point screenDPI = display.getDPI(); Point printerDPI = printer.getDPI(); - int scaleFactor = printerDPI.x / screenDPI.x; + int scaleFactor = printerDPI.x / DOTS_PER_INCH; Rectangle trim = printer.computeTrim(0, 0, 0, 0); if (printer.startJob(currentName)) { if (printer.startPage()) { diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet361.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet361.java index 56ef65dbeaa..3423008bc82 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet361.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet361.java @@ -146,6 +146,7 @@ public void getName(AccessibleEvent e) { } private static void performPrintAction(final Display display, final Shell shell) { + final int DOTS_PER_INCH = 96; Rectangle r = composite.getBounds(); Point p = shell.toDisplay(r.x, r.y); org.eclipse.swt.graphics.Image snapshotImage @@ -159,9 +160,8 @@ private static void performPrintAction(final Display display, final Shell shell) data = dialog.open(); if (data != null) { Printer printer = new Printer(data); - Point screenDPI = display.getDPI(); Point printerDPI = printer.getDPI(); - int scaleFactor = printerDPI.x / screenDPI.x; + int scaleFactor = printerDPI.x / DOTS_PER_INCH; Rectangle trim = printer.computeTrim(0, 0, 0, 0); if (printer.startJob("Print Image")) { ImageData imageData = snapshotImage.getImageData(); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java index 54b53dc1d39..8e5b7c2f7d7 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet367.java @@ -185,7 +185,7 @@ public int getGcStyle() { createSeparator(shell); - new Label (shell, SWT.NONE).setText ("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")"); + new Label (shell, SWT.NONE).setText ("5. 50x50 box"); Label box= new Label (shell, SWT.NONE); box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW)); box.setLayoutData (new GridData (50, 50));