Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/swing/ImageIcon.java
41153 views
1
/*
2
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.swing;
27
28
import java.awt.Component;
29
import java.awt.Graphics;
30
import java.awt.IllegalComponentStateException;
31
import java.awt.Image;
32
import java.awt.MediaTracker;
33
import java.awt.Toolkit;
34
import java.awt.image.ColorModel;
35
import java.awt.image.ImageObserver;
36
import java.awt.image.MemoryImageSource;
37
import java.awt.image.PixelGrabber;
38
import java.beans.BeanProperty;
39
import java.beans.ConstructorProperties;
40
import java.beans.Transient;
41
import java.io.IOException;
42
import java.io.ObjectInputStream;
43
import java.io.ObjectOutputStream;
44
import java.io.Serial;
45
import java.io.Serializable;
46
import java.net.URL;
47
import java.security.AccessControlContext;
48
import java.security.AccessController;
49
import java.security.PrivilegedAction;
50
import java.security.ProtectionDomain;
51
import java.util.Locale;
52
53
import javax.accessibility.Accessible;
54
import javax.accessibility.AccessibleContext;
55
import javax.accessibility.AccessibleIcon;
56
import javax.accessibility.AccessibleRole;
57
import javax.accessibility.AccessibleState;
58
import javax.accessibility.AccessibleStateSet;
59
60
import sun.awt.AWTAccessor;
61
import sun.awt.AppContext;
62
63
/**
64
* An implementation of the Icon interface that paints Icons
65
* from Images. Images that are created from a URL, filename or byte array
66
* are preloaded using MediaTracker to monitor the loaded state
67
* of the image.
68
*
69
* <p>
70
* For further information and examples of using image icons, see
71
* <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html">How to Use Icons</a>
72
* in <em>The Java Tutorial.</em>
73
*
74
* <p>
75
* <strong>Warning:</strong>
76
* Serialized objects of this class will not be compatible with
77
* future Swing releases. The current serialization support is
78
* appropriate for short term storage or RMI between applications running
79
* the same version of Swing. As of 1.4, support for long term storage
80
* of all JavaBeans
81
* has been added to the <code>java.beans</code> package.
82
* Please see {@link java.beans.XMLEncoder}.
83
*
84
* @author Jeff Dinkins
85
* @author Lynn Monsanto
86
* @since 1.2
87
*/
88
@SuppressWarnings({"removal","serial"}) // Same-version serialization only
89
public class ImageIcon implements Icon, Serializable, Accessible {
90
/* Keep references to the filename and location so that
91
* alternate persistence schemes have the option to archive
92
* images symbolically rather than including the image data
93
* in the archive.
94
*/
95
private transient String filename;
96
private transient URL location;
97
98
transient Image image;
99
transient int loadStatus = 0;
100
ImageObserver imageObserver;
101
String description = null;
102
103
/**
104
* Do not use this shared component, which is used to track image loading.
105
* It is left for backward compatibility only.
106
* @deprecated since 1.8
107
*/
108
@Deprecated
109
protected static final Component component;
110
111
/**
112
* Do not use this shared media tracker, which is used to load images.
113
* It is left for backward compatibility only.
114
* @deprecated since 1.8
115
*/
116
@Deprecated
117
protected static final MediaTracker tracker;
118
119
static {
120
component = AccessController.doPrivileged(new PrivilegedAction<Component>() {
121
public Component run() {
122
try {
123
final Component component = createNoPermsComponent();
124
125
// 6482575 - clear the appContext field so as not to leak it
126
AWTAccessor.getComponentAccessor().
127
setAppContext(component, null);
128
129
return component;
130
} catch (Throwable e) {
131
// We don't care about component.
132
// So don't prevent class initialisation.
133
e.printStackTrace();
134
return null;
135
}
136
}
137
});
138
tracker = new MediaTracker(component);
139
}
140
141
private static Component createNoPermsComponent() {
142
// 7020198 - set acc field to no permissions and no subject
143
// Note, will have appContext set.
144
return AccessController.doPrivileged(
145
new PrivilegedAction<Component>() {
146
public Component run() {
147
return new Component() {
148
};
149
}
150
},
151
new AccessControlContext(new ProtectionDomain[]{
152
new ProtectionDomain(null, null)
153
})
154
);
155
}
156
157
/**
158
* Id used in loading images from MediaTracker.
159
*/
160
private static int mediaTrackerID;
161
162
private static final Object TRACKER_KEY = new StringBuilder("TRACKER_KEY");
163
164
int width = -1;
165
int height = -1;
166
167
/**
168
* Creates an ImageIcon from the specified file. The image will
169
* be preloaded by using MediaTracker to monitor the loading state
170
* of the image.
171
* @param filename the name of the file containing the image
172
* @param description a brief textual description of the image
173
* @see #ImageIcon(String)
174
*/
175
public ImageIcon(String filename, String description) {
176
image = Toolkit.getDefaultToolkit().getImage(filename);
177
if (image == null) {
178
return;
179
}
180
this.filename = filename;
181
this.description = description;
182
loadImage(image);
183
}
184
185
/**
186
* Creates an ImageIcon from the specified file. The image will
187
* be preloaded by using MediaTracker to monitor the loading state
188
* of the image. The specified String can be a file name or a
189
* file path. When specifying a path, use the Internet-standard
190
* forward-slash ("/") as a separator.
191
* (The string is converted to an URL, so the forward-slash works
192
* on all systems.)
193
* For example, specify:
194
* <pre>
195
* new ImageIcon("images/myImage.gif") </pre>
196
* The description is initialized to the <code>filename</code> string.
197
*
198
* @param filename a String specifying a filename or path
199
* @see #getDescription
200
*/
201
@ConstructorProperties({"description"})
202
public ImageIcon (String filename) {
203
this(filename, filename);
204
}
205
206
/**
207
* Creates an ImageIcon from the specified URL. The image will
208
* be preloaded by using MediaTracker to monitor the loaded state
209
* of the image.
210
* @param location the URL for the image
211
* @param description a brief textual description of the image
212
* @see #ImageIcon(String)
213
*/
214
public ImageIcon(URL location, String description) {
215
image = Toolkit.getDefaultToolkit().getImage(location);
216
if (image == null) {
217
return;
218
}
219
this.location = location;
220
this.description = description;
221
loadImage(image);
222
}
223
224
/**
225
* Creates an ImageIcon from the specified URL. The image will
226
* be preloaded by using MediaTracker to monitor the loaded state
227
* of the image.
228
* The icon's description is initialized to be
229
* a string representation of the URL.
230
* @param location the URL for the image
231
* @see #getDescription
232
*/
233
public ImageIcon (URL location) {
234
this(location, location.toExternalForm());
235
}
236
237
/**
238
* Creates an ImageIcon from the image.
239
* @param image the image
240
* @param description a brief textual description of the image
241
*/
242
public ImageIcon(Image image, String description) {
243
this(image);
244
this.description = description;
245
}
246
247
/**
248
* Creates an ImageIcon from an image object.
249
* If the image has a "comment" property that is a string,
250
* then the string is used as the description of this icon.
251
* @param image the image
252
* @see #getDescription
253
* @see java.awt.Image#getProperty
254
*/
255
public ImageIcon (Image image) {
256
this.image = image;
257
Object o = image.getProperty("comment", imageObserver);
258
if (o instanceof String) {
259
description = (String) o;
260
}
261
loadImage(image);
262
}
263
264
/**
265
* Creates an ImageIcon from an array of bytes which were
266
* read from an image file containing a supported image format,
267
* such as GIF, JPEG, or (as of 1.3) PNG.
268
* Normally this array is created
269
* by reading an image using Class.getResourceAsStream(), but
270
* the byte array may also be statically stored in a class.
271
*
272
* @param imageData an array of pixels in an image format supported
273
* by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
274
* @param description a brief textual description of the image
275
* @see java.awt.Toolkit#createImage
276
*/
277
public ImageIcon (byte[] imageData, String description) {
278
this.image = Toolkit.getDefaultToolkit().createImage(imageData);
279
if (image == null) {
280
return;
281
}
282
this.description = description;
283
loadImage(image);
284
}
285
286
/**
287
* Creates an ImageIcon from an array of bytes which were
288
* read from an image file containing a supported image format,
289
* such as GIF, JPEG, or (as of 1.3) PNG.
290
* Normally this array is created
291
* by reading an image using Class.getResourceAsStream(), but
292
* the byte array may also be statically stored in a class.
293
* If the resulting image has a "comment" property that is a string,
294
* then the string is used as the description of this icon.
295
*
296
* @param imageData an array of pixels in an image format supported by
297
* the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
298
* @see java.awt.Toolkit#createImage
299
* @see #getDescription
300
* @see java.awt.Image#getProperty
301
*/
302
public ImageIcon (byte[] imageData) {
303
this.image = Toolkit.getDefaultToolkit().createImage(imageData);
304
if (image == null) {
305
return;
306
}
307
Object o = image.getProperty("comment", imageObserver);
308
if (o instanceof String) {
309
description = (String) o;
310
}
311
loadImage(image);
312
}
313
314
/**
315
* Creates an uninitialized image icon.
316
*/
317
public ImageIcon() {
318
}
319
320
/**
321
* Loads the image, returning only when the image is loaded.
322
* @param image the image
323
*/
324
protected void loadImage(Image image) {
325
MediaTracker mTracker = getTracker();
326
synchronized(mTracker) {
327
int id = getNextID();
328
329
mTracker.addImage(image, id);
330
try {
331
mTracker.waitForID(id, 0);
332
} catch (InterruptedException e) {
333
System.out.println("INTERRUPTED while loading Image");
334
}
335
loadStatus = mTracker.statusID(id, false);
336
mTracker.removeImage(image, id);
337
338
width = image.getWidth(imageObserver);
339
height = image.getHeight(imageObserver);
340
}
341
}
342
343
/**
344
* Returns an ID to use with the MediaTracker in loading an image.
345
*/
346
private int getNextID() {
347
synchronized(getTracker()) {
348
return ++mediaTrackerID;
349
}
350
}
351
352
/**
353
* Returns the MediaTracker for the current AppContext, creating a new
354
* MediaTracker if necessary.
355
*/
356
private MediaTracker getTracker() {
357
Object trackerObj;
358
AppContext ac = AppContext.getAppContext();
359
// Opt: Only synchronize if trackerObj comes back null?
360
// If null, synchronize, re-check for null, and put new tracker
361
synchronized(ac) {
362
trackerObj = ac.get(TRACKER_KEY);
363
if (trackerObj == null) {
364
Component comp = new Component() {};
365
trackerObj = new MediaTracker(comp);
366
ac.put(TRACKER_KEY, trackerObj);
367
}
368
}
369
return (MediaTracker) trackerObj;
370
}
371
372
/**
373
* Returns the status of the image loading operation.
374
* @return the loading status as defined by java.awt.MediaTracker
375
* @see java.awt.MediaTracker#ABORTED
376
* @see java.awt.MediaTracker#ERRORED
377
* @see java.awt.MediaTracker#COMPLETE
378
*/
379
public int getImageLoadStatus() {
380
return loadStatus;
381
}
382
383
/**
384
* Returns this icon's <code>Image</code>.
385
* @return the <code>Image</code> object for this <code>ImageIcon</code>
386
*/
387
@Transient
388
public Image getImage() {
389
return image;
390
}
391
392
/**
393
* Sets the image displayed by this icon.
394
* @param image the image
395
*/
396
public void setImage(Image image) {
397
this.image = image;
398
loadImage(image);
399
}
400
401
/**
402
* Gets the description of the image. This is meant to be a brief
403
* textual description of the object. For example, it might be
404
* presented to a blind user to give an indication of the purpose
405
* of the image.
406
* The description may be null.
407
*
408
* @return a brief textual description of the image
409
*/
410
public String getDescription() {
411
return description;
412
}
413
414
/**
415
* Sets the description of the image. This is meant to be a brief
416
* textual description of the object. For example, it might be
417
* presented to a blind user to give an indication of the purpose
418
* of the image.
419
* @param description a brief textual description of the image
420
*/
421
public void setDescription(String description) {
422
this.description = description;
423
}
424
425
/**
426
* Paints the icon.
427
* The top-left corner of the icon is drawn at
428
* the point (<code>x</code>, <code>y</code>)
429
* in the coordinate space of the graphics context <code>g</code>.
430
* If this icon has no image observer,
431
* this method uses the <code>c</code> component
432
* as the observer.
433
*
434
* @param c the component to be used as the observer
435
* if this icon has no image observer
436
* @param g the graphics context
437
* @param x the X coordinate of the icon's top-left corner
438
* @param y the Y coordinate of the icon's top-left corner
439
*/
440
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
441
if(imageObserver == null) {
442
g.drawImage(image, x, y, c);
443
} else {
444
g.drawImage(image, x, y, imageObserver);
445
}
446
}
447
448
/**
449
* Gets the width of the icon.
450
*
451
* @return the width in pixels of this icon
452
*/
453
public int getIconWidth() {
454
return width;
455
}
456
457
/**
458
* Gets the height of the icon.
459
*
460
* @return the height in pixels of this icon
461
*/
462
public int getIconHeight() {
463
return height;
464
}
465
466
/**
467
* Sets the image observer for the image. Set this
468
* property if the ImageIcon contains an animated GIF, so
469
* the observer is notified to update its display.
470
* For example:
471
* <pre>
472
* icon = new ImageIcon(...)
473
* button.setIcon(icon);
474
* icon.setImageObserver(button);
475
* </pre>
476
*
477
* @param observer the image observer
478
*/
479
public void setImageObserver(ImageObserver observer) {
480
imageObserver = observer;
481
}
482
483
/**
484
* Returns the image observer for the image.
485
*
486
* @return the image observer, which may be null
487
*/
488
@Transient
489
public ImageObserver getImageObserver() {
490
return imageObserver;
491
}
492
493
/**
494
* Returns a string representation of this image.
495
*
496
* @return a string representing this image
497
*/
498
public String toString() {
499
if (description != null) {
500
return description;
501
}
502
return super.toString();
503
}
504
505
@Serial
506
private void readObject(ObjectInputStream s)
507
throws ClassNotFoundException, IOException
508
{
509
ObjectInputStream.GetField f = s.readFields();
510
511
imageObserver = (ImageObserver) f.get("imageObserver", null);
512
description = (String) f.get("description", null);
513
width = f.get("width", -1);
514
height = f.get("height", -1);
515
accessibleContext = (AccessibleImageIcon) f.get("accessibleContext", null);
516
517
int w = s.readInt();
518
int h = s.readInt();
519
int[] pixels = (int[])(s.readObject());
520
521
if (pixels == null && (w != -1 || h != -1)) {
522
throw new IllegalStateException("Inconsistent width and height"
523
+ " for null image [" + w + ", " + h + "]");
524
}
525
526
if (pixels != null && (w < 0 || h < 0)) {
527
throw new IllegalStateException("Inconsistent width and height"
528
+ " for image [" + w + ", " + h + "]");
529
}
530
531
if (w != getIconWidth() || h != getIconHeight()) {
532
throw new IllegalStateException("Inconsistent width and height"
533
+ " for image [" + w + ", " + h + "]");
534
}
535
536
if (pixels != null) {
537
Toolkit tk = Toolkit.getDefaultToolkit();
538
ColorModel cm = ColorModel.getRGBdefault();
539
image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w));
540
loadImage(image);
541
}
542
}
543
544
545
@Serial
546
private void writeObject(ObjectOutputStream s)
547
throws IOException
548
{
549
s.defaultWriteObject();
550
551
int w = getIconWidth();
552
int h = getIconHeight();
553
int[] pixels = image != null? new int[w * h] : null;
554
555
if (image != null) {
556
try {
557
PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
558
pg.grabPixels();
559
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
560
throw new IOException("failed to load image contents");
561
}
562
}
563
catch (InterruptedException e) {
564
throw new IOException("image load interrupted");
565
}
566
}
567
568
s.writeInt(w);
569
s.writeInt(h);
570
s.writeObject(pixels);
571
}
572
573
/**
574
* --- Accessibility Support ---
575
*/
576
577
private AccessibleImageIcon accessibleContext = null;
578
579
/**
580
* Gets the AccessibleContext associated with this ImageIcon.
581
* For image icons, the AccessibleContext takes the form of an
582
* AccessibleImageIcon.
583
* A new AccessibleImageIcon instance is created if necessary.
584
*
585
* @return an AccessibleImageIcon that serves as the
586
* AccessibleContext of this ImageIcon
587
* @since 1.3
588
*/
589
@BeanProperty(expert = true, description
590
= "The AccessibleContext associated with this ImageIcon.")
591
public AccessibleContext getAccessibleContext() {
592
if (accessibleContext == null) {
593
accessibleContext = new AccessibleImageIcon();
594
}
595
return accessibleContext;
596
}
597
598
/**
599
* This class implements accessibility support for the
600
* <code>ImageIcon</code> class. It provides an implementation of the
601
* Java Accessibility API appropriate to image icon user-interface
602
* elements.
603
* <p>
604
* <strong>Warning:</strong>
605
* Serialized objects of this class will not be compatible with
606
* future Swing releases. The current serialization support is
607
* appropriate for short term storage or RMI between applications running
608
* the same version of Swing. As of 1.4, support for long term storage
609
* of all JavaBeans
610
* has been added to the <code>java.beans</code> package.
611
* Please see {@link java.beans.XMLEncoder}.
612
* @since 1.3
613
*/
614
@SuppressWarnings("serial") // Same-version serialization only
615
protected class AccessibleImageIcon extends AccessibleContext
616
implements AccessibleIcon, Serializable {
617
618
/**
619
* Constructs an {@code AccessibleImageIcon}.
620
*/
621
protected AccessibleImageIcon() {}
622
623
/*
624
* AccessibleContest implementation -----------------
625
*/
626
627
/**
628
* Gets the role of this object.
629
*
630
* @return an instance of AccessibleRole describing the role of the
631
* object
632
* @see AccessibleRole
633
*/
634
public AccessibleRole getAccessibleRole() {
635
return AccessibleRole.ICON;
636
}
637
638
/**
639
* Gets the state of this object.
640
*
641
* @return an instance of AccessibleStateSet containing the current
642
* state set of the object
643
* @see AccessibleState
644
*/
645
public AccessibleStateSet getAccessibleStateSet() {
646
return null;
647
}
648
649
/**
650
* Gets the Accessible parent of this object. If the parent of this
651
* object implements Accessible, this method should simply return
652
* getParent().
653
*
654
* @return the Accessible parent of this object -- can be null if this
655
* object does not have an Accessible parent
656
*/
657
public Accessible getAccessibleParent() {
658
return null;
659
}
660
661
/**
662
* Gets the index of this object in its accessible parent.
663
*
664
* @return the index of this object in its parent; -1 if this
665
* object does not have an accessible parent.
666
* @see #getAccessibleParent
667
*/
668
public int getAccessibleIndexInParent() {
669
return -1;
670
}
671
672
/**
673
* Returns the number of accessible children in the object. If all
674
* of the children of this object implement Accessible, than this
675
* method should return the number of children of this object.
676
*
677
* @return the number of accessible children in the object.
678
*/
679
public int getAccessibleChildrenCount() {
680
return 0;
681
}
682
683
/**
684
* Returns the nth Accessible child of the object.
685
*
686
* @param i zero-based index of child
687
* @return the nth Accessible child of the object
688
*/
689
public Accessible getAccessibleChild(int i) {
690
return null;
691
}
692
693
/**
694
* Returns the locale of this object.
695
*
696
* @return the locale of this object
697
*/
698
public Locale getLocale() throws IllegalComponentStateException {
699
return null;
700
}
701
702
/*
703
* AccessibleIcon implementation -----------------
704
*/
705
706
/**
707
* Gets the description of the icon. This is meant to be a brief
708
* textual description of the object. For example, it might be
709
* presented to a blind user to give an indication of the purpose
710
* of the icon.
711
*
712
* @return the description of the icon
713
*/
714
public String getAccessibleIconDescription() {
715
return ImageIcon.this.getDescription();
716
}
717
718
/**
719
* Sets the description of the icon. This is meant to be a brief
720
* textual description of the object. For example, it might be
721
* presented to a blind user to give an indication of the purpose
722
* of the icon.
723
*
724
* @param description the description of the icon
725
*/
726
public void setAccessibleIconDescription(String description) {
727
ImageIcon.this.setDescription(description);
728
}
729
730
/**
731
* Gets the height of the icon.
732
*
733
* @return the height of the icon
734
*/
735
public int getAccessibleIconHeight() {
736
return ImageIcon.this.height;
737
}
738
739
/**
740
* Gets the width of the icon.
741
*
742
* @return the width of the icon
743
*/
744
public int getAccessibleIconWidth() {
745
return ImageIcon.this.width;
746
}
747
748
@Serial
749
private void readObject(ObjectInputStream s)
750
throws ClassNotFoundException, IOException
751
{
752
s.defaultReadObject();
753
}
754
755
@Serial
756
private void writeObject(ObjectOutputStream s)
757
throws IOException
758
{
759
s.defaultWriteObject();
760
}
761
} // AccessibleImageIcon
762
}
763
764