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

113
LINES

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

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

Uses 108K of libraries. Click here for Pure Java version (12491L/91K).

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.

  visualize { ret _visualize(); }
  
  final private JComponent _visualize() {
    imageSurface = imageSurface(image);
    imageSurface.onNewImage = r newImageFromSurface;
    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 {
    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(); }
}

Author comment

Began life as a copy of #1016205

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1018873
Snippet name: DynImageSurface - module template showing an image
Eternal ID of this version: #1018873/27
Text MD5: 8a9424cdb455ba9fed94a16ab9993948
Transpilation MD5: 1068eb71ac6c23f41b9e4fcaf0294f6a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-09-10 14:02:06
Source code size: 3336 bytes / 113 lines
Pitched / IR pitched: No / No
Views / Downloads: 259 / 1317
Version history: 26 change(s)
Referenced in: [show references]

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