From 683a86563df41f4b7de98430ee9b696f2ed9a9c5 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 10 Jan 2023 22:17:59 -0500 Subject: [PATCH] fix class cast by pulling image data separately (resolves #624) --- core/src/processing/awt/PGraphicsJava2D.java | 14 +++++++++++++- core/todo.txt | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/core/src/processing/awt/PGraphicsJava2D.java b/core/src/processing/awt/PGraphicsJava2D.java index 92b10cb33..ee41fe052 100644 --- a/core/src/processing/awt/PGraphicsJava2D.java +++ b/core/src/processing/awt/PGraphicsJava2D.java @@ -3007,7 +3007,19 @@ public class PGraphicsJava2D extends PGraphics { public void copy(PImage src, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh) { - g2.drawImage((Image) src.getNative(), + Image nativeImage; + if (src instanceof PGraphicsJava2D) { + // if it's a Java2D drawing surface, use its backing image + nativeImage = ((PGraphicsJava2D) src).image; + } else if (src.getNative() instanceof Image) { // may be null + // if it's something else that has a java.awt.Image as its native backing + nativeImage = (Image) src.getNative(); + } else { + // much slower: grab the pixels with get() and create a native image + nativeImage = (Image) src.get(sx, sy, sw, sh).getNative(); + } + + g2.drawImage(nativeImage, dx, dy, dx + dw, dy + dh, sx, sy, sx + sw, sy + sh, null); } diff --git a/core/todo.txt b/core/todo.txt index 6f3c91eff..6bd367514 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -1,4 +1,8 @@ 1290 (4.1.2?) +X Fix ClassCastException with copy() and PGraphicsJava2D +X https://github.com/processing/processing4/issues/624 + + _ show an error when calling clear() on the main drawing surface _ https://github.com/processing/processing4/issues/627