Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

44
LINES

< > BotCompany Repo | #1003551 // imageIcon - make ImageIcon from image ID or BufferedImage

JavaX fragment (include)

1  
static int imageIcon_cacheSize = 10;
2  
sbool imageIcon_verbose;
3  
static Map<S, ImageIcon> imageIcon_cache;
4  
static Lock imageIcon_lock = lock();
5  
static new ThreadLocal<Bool> imageIcon_fixGIF;
6  
7  
// not going through BufferedImage preserves animations
8  
static ImageIcon imageIcon(S imageID) ctex {
9  
  if (imageID == null) null;
10  
  lock imageIcon_lock;
11  
  if (imageIcon_cache == null)
12  
    imageIcon_cache = new MRUCache(imageIcon_cacheSize);
13  
  imageID = fsI(imageID);
14  
  ImageIcon ii = imageIcon_cache.get(imageID);
15  
  if (ii == null) {
16  
    if (imageIcon_verbose) print("Loading image icon: " + imageID);
17  
    File f = loadBinarySnippet(imageID);
18  
    ifndef DontFixGIFs
19  
      Bool b = imageIcon_fixGIF!;
20  
      if (!isFalse(b))
21  
        ii = new ImageIcon(loadBufferedImageFixingGIFs(f));
22  
      else
23  
    endifndef
24  
    ii = new ImageIcon(f.toURI().toURL());
25  
  } else
26  
    imageIcon_cache.remove(imageID); // move to front of cache on access
27  
  imageIcon_cache.put(imageID, ii);
28  
  ret ii;
29  
}
30  
31  
// doesn't fix GIFs
32  
static ImageIcon imageIcon(File f) ctex {
33  
  ret new ImageIcon(f.toURI().toURL());
34  
}
35  
36  
static ImageIcon imageIcon(Image img) {
37  
  ret img == null ?: new ImageIcon(img);
38  
}
39  
40  
ifclass RGBImage
41  
  static ImageIcon imageIcon(RGBImage img) {
42  
    ret imageIcon(img.getBufferedImage());
43  
  }
44  
endif

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003551
Snippet name: imageIcon - make ImageIcon from image ID or BufferedImage
Eternal ID of this version: #1003551/16
Text MD5: a892d616213a47ef53002d4d7e61eda0
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-12 21:09:50
Source code size: 1317 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 775 / 1067
Version history: 15 change(s)
Referenced in: [show references]