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)

static int imageIcon_cacheSize = 10;
sbool imageIcon_verbose;
static Map<S, ImageIcon> imageIcon_cache;
static Lock imageIcon_lock = lock();
static new ThreadLocal<Bool> imageIcon_fixGIF;

// not going through BufferedImage preserves animations
static ImageIcon imageIcon(S imageID) ctex {
  if (imageID == null) null;
  lock imageIcon_lock;
  if (imageIcon_cache == null)
    imageIcon_cache = new MRUCache(imageIcon_cacheSize);
  imageID = fsI(imageID);
  ImageIcon ii = imageIcon_cache.get(imageID);
  if (ii == null) {
    if (imageIcon_verbose) print("Loading image icon: " + imageID);
    File f = loadBinarySnippet(imageID);
    ifndef DontFixGIFs
      Bool b = imageIcon_fixGIF!;
      if (!isFalse(b))
        ii = new ImageIcon(loadBufferedImageFixingGIFs(f));
      else
    endifndef
    ii = new ImageIcon(f.toURI().toURL());
  } else
    imageIcon_cache.remove(imageID); // move to front of cache on access
  imageIcon_cache.put(imageID, ii);
  ret ii;
}

// doesn't fix GIFs
static ImageIcon imageIcon(File f) ctex {
  ret new ImageIcon(f.toURI().toURL());
}

static ImageIcon imageIcon(Image img) {
  ret new ImageIcon(img);
}

ifclass RGBImage
  static ImageIcon imageIcon(RGBImage img) {
    ret imageIcon(img.getBufferedImage());
  }
endif

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1003551
Snippet name: imageIcon - make ImageIcon from image ID or BufferedImage
Eternal ID of this version: #1003551/15
Text MD5: 375afab21676342eab52419069078453
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-12 02:17:05
Source code size: 1302 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 435 / 691
Version history: 14 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1003551 & http://1003551.tinybrain.de