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

65
LINES

< > BotCompany Repo | #1006526 // class ScreenLens - show screen lens

JavaX fragment (include)

1  
sclass ScreenLens {
2  
  double sleep = 0; // 0.2;
3  
  int w = 300, h = 300;
4  
  ImageSurface is;
5  
  volatile BufferedImage image, imagePP; // pp = postProcessed
6  
  bool keepAwayFromBorder = true, makeBot = false;
7  
  bool showFPS = true;
8  
  S botName = "Screen Lens.";
9  
  Android3 bot; // exporting our functionality as bot to other VMs
10  
  bool pack = true;
11  
  new PerSecondCounter fps;
12  
  javax.swing.Timer timer;
13  
  
14  
  O onImageChange;
15  
  O postProcess; // func(BufferedImage) => BufferedImage
16  
  
17  
  ScreenLens start() {
18  
    swingAndWait(r {
19  
      is = alwaysOnTop(showImage(new RGBImage(w, h, Color.white)));
20  
      timer = installTimer(is, toMS(sleep), r {
21  
        Pt m = new Pt(mouseLocation());
22  
        Rect screen = screenRect();
23  
        if (keepAwayFromBorder) {
24  
          m = new Pt(min(m.x, screen.x2()-w/2), min(m.y, screen.y2()-h/2));
25  
          m = new Pt(max(m.x, w/2), max(m.y, h/2));
26  
        }
27  
        Rect r = intersectRects(screen, rectAround(m, w, h));
28  
        image = shootScreen2(r);
29  
        imagePP = callPostProcessor(postProcess, image);
30  
        is.setImage(imagePP);
31  
        if (pack) {
32  
          packFrame(lens.is);
33  
          moveToTopRightCorner(getFrame(lens.is));
34  
        }
35  
        pack = false;
36  
        fps.inc();
37  
        if (showFPS)
38  
          setFrameTitle(is, "Screen Lens (" + iround(fps.get()) + " fps)");
39  
        pcallF(onImageChange);
40  
      });
41  
    });
42  
    addSizeChooser();
43  
    if (makeBot) bot = makeBot(botName, this);
44  
    this;
45  
  }
46  
  
47  
  void addSizeChooser() {
48  
    final JTextField tfSize = jTextField(w);
49  
    addToWindow(is, jRightAlignedLine(jlabel("Size:"), jMinWidth(50, tfSize), jlabel("pixels")));
50  
    onUpdate(tfSize, r {
51  
      w = h = max(1, parseIntOpt(getTextTrim(tfSize)));
52  
      pack = true;
53  
    });
54  
  }
55  
  
56  
  S answer(S s) {
57  
    new Matches m;
58  
    if "get image"
59  
      ret image == null ? "null" : structure(new RGBImage(image));
60  
    if "get post processed image"
61  
      ret imagePP == null ? "null" : structure(new RGBImage(imagePP));
62  
    if "running" ret yn(timer.isRunning());
63  
    null;
64  
  }
65  
}

Author comment

Began life as a copy of #1006525

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1006526
Snippet name: class ScreenLens - show screen lens
Eternal ID of this version: #1006526/3
Text MD5: cd46dab3a9204059e24f4a20273eb132
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-11-27 23:13:10
Source code size: 2091 bytes / 65 lines
Pitched / IR pitched: No / No
Views / Downloads: 528 / 1139
Version history: 2 change(s)
Referenced in: [show references]