From b0cd253dad0faa67261bf3e2d8929c5243b5ee96 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sun, 30 Oct 2022 18:38:00 -0400 Subject: [PATCH] fix a pixelDensity() issue w/ default display --- core/src/processing/awt/ShimAWT.java | 32 ++++++++++++++++++++++------ core/todo.txt | 8 +++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/core/src/processing/awt/ShimAWT.java b/core/src/processing/awt/ShimAWT.java index a8c64e1a9..f33529644 100644 --- a/core/src/processing/awt/ShimAWT.java +++ b/core/src/processing/awt/ShimAWT.java @@ -129,16 +129,34 @@ public class ShimAWT implements PConstants { private int displayDensityImpl(int display) { - if (display > 0 && display <= displayDevices.length) { - GraphicsConfiguration graphicsConfig = - displayDevices[display - 1].getDefaultConfiguration(); + GraphicsConfiguration graphicsConfig = null; + + if (display == -1) { // the default display + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + graphicsConfig = ge.getDefaultScreenDevice().getDefaultConfiguration(); + + } else if (display == SPAN) { + // walk through all displays, go with lowest common denominator + for (int i = 0; i < displayDevices.length; i++) { + if (displayDensityImpl(i) == 1) { + return 1; + } + } + return 2; // everyone is density 2 + + } else if (display <= displayDevices.length) { + graphicsConfig = displayDevices[display - 1].getDefaultConfiguration(); + } + + if (graphicsConfig == null) { + System.err.println("Display " + display + " does not exist, " + + "returning 1 for displayDensity(" + display + ")"); + return 1; // not the end of the world, so don't throw a RuntimeException + + } else { AffineTransform tx = graphicsConfig.getDefaultTransform(); return (int) Math.round(tx.getScaleX()); } - - System.err.println("Display " + display + " does not exist, " + - "returning 1 for displayDensity(" + display + ")"); - return 1; // not the end of the world, so don't throw a RuntimeException } diff --git a/core/todo.txt b/core/todo.txt index fa0605fde..941a3d1a7 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -3,8 +3,16 @@ X Updating PApplet to use Java 17 (switch statements, etc) X XxxList bug with random() if there are zero elements in the array X now throws ArrayIndexOutOfBoundsException X kinda want to return null, but later getting an NPE is more confusing +X can't do pixelDensity 2 with fullScreen and Java2D +X when using fullScreen(), shows error: +X "Display -1 does not exist, returning 1 for displayDensity(-1)" +X maybe https://github.com/processing/processing4/issues/487 +_ need alphabetical ordering for json serialization +_ use choice() instead of random() for list classes +_ add choice() as a PApplet method for int values of random()? + _ concurrent StringDict et al _ why no concurrent TreemMap? https://stackoverflow.com/a/17656453 _ https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListMap.html