Skip to content

Commit 02b13ab

Browse files
committed
implement setIconImage() for PSurfaceAWT
1 parent 755c442 commit 02b13ab

File tree

2 files changed

+73
-47
lines changed

2 files changed

+73
-47
lines changed

core/src/processing/awt/PSurfaceAWT.java

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ public void initFrame(final PApplet sketch) {/*, int backgroundColor,
442442
}
443443

444444
// Put the p5 logo in the Frame's corner to override the Java coffee cup.
445-
setIconImage(frame);
445+
setProcessingIcon(frame);
446446

447447
// For 0149, moving this code (up to the pack() method) before init().
448448
// For OpenGL (and perhaps other renderers in the future), a peer is
@@ -543,6 +543,73 @@ public void setResizable(boolean resizable) {
543543
}
544544

545545

546+
@Override
547+
public void setIcon(PImage image) {
548+
Image awtImage = (Image) image.getNative();
549+
550+
if (PApplet.platform != PConstants.MACOSX) {
551+
frame.setIconImage(awtImage);
552+
553+
} else {
554+
try {
555+
final String td = "processing.core.ThinkDifferent";
556+
Class<?> thinkDifferent =
557+
Thread.currentThread().getContextClassLoader().loadClass(td);
558+
Method method =
559+
thinkDifferent.getMethod("setIconImage", new Class[] { java.awt.Image.class });
560+
method.invoke(null, new Object[] { awtImage });
561+
} catch (Exception e) {
562+
e.printStackTrace(); // That's unfortunate
563+
}
564+
}
565+
}
566+
567+
568+
static ArrayList<Image> iconImages;
569+
570+
static protected void setProcessingIcon(Frame frame) {
571+
// On OS X, this only affects what shows up in the dock when minimized.
572+
// So replacing it is actually a step backwards. Brilliant.
573+
if (PApplet.platform != PConstants.MACOSX) {
574+
//Image image = Toolkit.getDefaultToolkit().createImage(ICON_IMAGE);
575+
//frame.setIconImage(image);
576+
try {
577+
if (iconImages == null) {
578+
iconImages = new ArrayList<Image>();
579+
final int[] sizes = { 16, 32, 48, 64, 128, 256, 512 };
580+
581+
for (int sz : sizes) {
582+
//URL url = getClass().getResource("/icon/icon-" + sz + ".png");
583+
URL url = PApplet.class.getResource("/icon/icon-" + sz + ".png");
584+
Image image = Toolkit.getDefaultToolkit().getImage(url);
585+
iconImages.add(image);
586+
//iconImages.add(Toolkit.getLibImage("icons/pde-" + sz + ".png", frame));
587+
}
588+
}
589+
frame.setIconImages(iconImages);
590+
591+
} catch (Exception e) { } // harmless; keep this to ourselves
592+
593+
} else {
594+
// On OS X, set this for AWT surfaces, which handles the dock image
595+
// as well as the cmd-tab image that's shown. Just one size, I guess.
596+
URL url = PApplet.class.getResource("/icon/icon-512.png");
597+
// Seems dangerous to have this in code instead of using reflection, no?
598+
//ThinkDifferent.setIconImage(Toolkit.getDefaultToolkit().getImage(url));
599+
try {
600+
final String td = "processing.core.ThinkDifferent";
601+
Class<?> thinkDifferent =
602+
Thread.currentThread().getContextClassLoader().loadClass(td);
603+
Method method =
604+
thinkDifferent.getMethod("setIconImage", new Class[] { java.awt.Image.class });
605+
method.invoke(null, new Object[] { Toolkit.getDefaultToolkit().getImage(url) });
606+
} catch (Exception e) {
607+
e.printStackTrace(); // That's unfortunate
608+
}
609+
}
610+
}
611+
612+
546613
@Override
547614
public void setVisible(boolean visible) {
548615
frame.setVisible(visible);
@@ -1011,52 +1078,6 @@ public void componentResized(ComponentEvent e) {
10111078
}
10121079

10131080

1014-
static ArrayList<Image> iconImages;
1015-
1016-
static protected void setIconImage(Frame frame) {
1017-
// On OS X, this only affects what shows up in the dock when minimized.
1018-
// So replacing it is actually a step backwards. Brilliant.
1019-
if (PApplet.platform != PConstants.MACOSX) {
1020-
//Image image = Toolkit.getDefaultToolkit().createImage(ICON_IMAGE);
1021-
//frame.setIconImage(image);
1022-
try {
1023-
if (iconImages == null) {
1024-
iconImages = new ArrayList<Image>();
1025-
final int[] sizes = { 16, 32, 48, 64, 128, 256, 512 };
1026-
1027-
for (int sz : sizes) {
1028-
//URL url = getClass().getResource("/icon/icon-" + sz + ".png");
1029-
URL url = PApplet.class.getResource("/icon/icon-" + sz + ".png");
1030-
Image image = Toolkit.getDefaultToolkit().getImage(url);
1031-
iconImages.add(image);
1032-
//iconImages.add(Toolkit.getLibImage("icons/pde-" + sz + ".png", frame));
1033-
}
1034-
}
1035-
frame.setIconImages(iconImages);
1036-
1037-
} catch (Exception e) { } // harmless; keep this to ourselves
1038-
1039-
} else {
1040-
// On OS X, set this for AWT surfaces, which handles the dock image
1041-
// as well as the cmd-tab image that's shown. Just one size, I guess.
1042-
URL url = PApplet.class.getResource("/icon/icon-512.png");
1043-
// Seems dangerous to have this in code instead of using reflection, no?
1044-
//ThinkDifferent.setIconImage(Toolkit.getDefaultToolkit().getImage(url));
1045-
try {
1046-
final String td = "processing.core.ThinkDifferent";
1047-
Class<?> thinkDifferent =
1048-
Thread.currentThread().getContextClassLoader().loadClass(td);
1049-
Method method =
1050-
thinkDifferent.getMethod("setIconImage", new Class[] { java.awt.Image.class });
1051-
method.invoke(null, new Object[] { Toolkit.getDefaultToolkit().getImage(url) });
1052-
} catch (Exception e) {
1053-
e.printStackTrace(); // That's unfortunate
1054-
}
1055-
1056-
}
1057-
}
1058-
1059-
10601081
// /**
10611082
// * (No longer in use) Use reflection to call
10621083
// * <code>com.apple.eawt.FullScreenUtilities.setWindowCanFullScreen(window, true);</code>

core/src/processing/core/ThinkDifferent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public void handleQuitRequestWith(QuitEvent event, QuitResponse response) {
7272

7373

7474
static public void setIconImage(Image image) {
75+
// When already set, is a sun.awt.image.MultiResolutionCachedImage on OS X
76+
// Image current = application.getDockIconImage();
77+
// System.out.println("current dock icon image is " + current);
78+
// System.out.println("changing to " + image);
79+
7580
application.setDockIconImage(image);
7681
}
7782

0 commit comments

Comments
 (0)