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

121
LINES

< > BotCompany Repo | #1018873 // DynImageSurface - module showing an image

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (18213L/115K).

abstract sclass DynImageSurface extends DynModule {
  transient ImageSurface imageSurface;
  transient BufferedImage image;
  transient S imageID;
  transient bool useUpdateHack, zoomToWindowOnSetImage;
  transient O onNewImage;
  switchable bool autoSaveToDisk = autoSaveToDiskDefault(); // dev.
  switchable bool pixelate;

  visualize { ret _visualize(); }
  
  final private JComponent _visualize() {
    imageSurface = imageSurface(image);
    imageSurface.onNewImage = r newImageFromSurface;
    imageSurface_pixelated(imageSurface, pixelate);
    ret jscroll_center(imageSurface);
  }
  
  // call after visualize
  void enableDoubleBuffering {
    if (imageSurface != null) imageSurface.setDoubleBuffered(true);
  }

  JComponent visualizeWithDoubleBuffering() {  
    JComponent c = _visualize();
    enableDoubleBuffering();
    ret c;
  }
  
  // hold image during reload (not during reboot yet)
  
  O _getReloadData() { ret image; }
  void _setReloadData(BufferedImage image) { this.image = image; }
  
  start {
    dm_watchField pixelate(-> imageSurface_pixelated(imageSurface, pixelate));
    
    if (autoSaveToDisk)
      autoLoadNow();
  }
  
  void autoLoadNow enter {
    pcall {
      File f = autoSaveImageFile();
      if (fileExists(f)) {
        print("Auto-loading " + f);
        setImage(loadImage2(f), true);
      }
    }
  }

  void autoSaveNow enter {  
    pcall {
      File f = autoSaveImageFile();
      savePNG(f, image);
      infoBox("Auto-saved " + renderFileInfo(f));
    }
  }
  
  bool autoSaveToDiskDefault() { false; }
  
  enhanceFrame {
    internalFramePopupMenuItem(f, "Auto-save now", rThread autoSaveNow);
  }
  
  // API
  
  void setImage(MakesBufferedImage image) { imageID = null; setImage(image == null ? null : image.getBufferedImage()); }
  
  void newImageFromSurface {
    setImage(imageSurface.getImage());
  }
  
  void setImage(BufferedImage image, bool loadingFromAutoSave default false) {
    this.image = image; imageID = null;
    if (imageSurface != null) {
      temp tempSetField(imageSurface, onNewImage := null); // avoid recursion
      imageSurface.setImage(image);
      if (zoomToWindowOnSetImage) imageSurface.zoomToWindow();
      if (autoSaveToDisk && !loadingFromAutoSave)
        autoSaveNow();
    }
    vmBus_send('newImage, this);
    pcallF(onNewImage, image);
  }
  
  void setImage(S imageID) {
    if (sameSnippetID(this.imageID, imageID)) ret;
    setImage(loadImage2(imageID));
    this.imageID = imageID;
  }
  
  File autoSaveImageFile() {
    S id = dm_moduleID(me());
    ret javaxDataDir("Image Autosaves/" + id + "-autosave.png");
  }
  
  BufferedImage getImage() {
    if (useUpdateHack) {
      // HACKY: to get changes in ImageSurface (e.g. images drag&dropped in there directly)
      ImageSurface is = imageSurface;
      if (is != null && is.getImage() != image) setImage(is.getImage());
    }
    
    ret image;
  }
  bool hasImage() { ret image != null; }
  
  void setZoom(double zoom) { if (imageSurface != null) imageSurface.setZoom(zoom); }
  
  Rect getSelection() { ret imageSurface == null ? null : toRect(imageSurface.getSelection()); }
  
  void setSelection(Rect r) { if (imageSurface != null) imageSurface.setSelection(toRectangle(r)); }
  
  int imageWidth() { ret imageSurface.getImage().getWidth(); }
  int imageHeight() { ret imageSurface.getImage().getHeight(); }
  
  void pixelate() {
    setField(pixelate := true);
  }
}

Author comment

Began life as a copy of #1016205

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1018873
Snippet name: DynImageSurface - module showing an image
Eternal ID of this version: #1018873/32
Text MD5: 0dc93b3fa8b5eb4a9ab4ecd07f0b4940
Transpilation MD5: 167b95c936a83af5ff68510232830595
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-12-26 18:51:09
Source code size: 3567 bytes / 121 lines
Pitched / IR pitched: No / No
Views / Downloads: 563 / 3406
Version history: 31 change(s)
Referenced in: #1019849 - Loadable Utils
#1029927 - Loadable Utils v2 (old)
#1030952 - Loadable Utils for Gazelle BEA [LIVE, see #1030953, edited by #1030951]
#1031186 - Loadable Utils v5
#1032272 - Loadable Utils for Gazelle BEA [backup]
#1032708 - Secret BEA Utils [purposely different from regular utils]
#1033505 - Loadable Utils for Gazelle V [stable version]
#1033861 - Loadable Utils for Gazelle 22 [dev version]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)