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

131
LINES

< > BotCompany Repo | #1003836 // Screenshot Maker v2

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 9620K of libraries. Click here for Pure Java version (1493L/10K/37K).

1  
!759
2  
3  
lib 1003153 // magic.jar
4  
5  
import ma.ma02.*;
6  
7  
import drjava.util.*;
8  
import net.luaos.tb.remote.ServerConnection;
9  
import net.luaos.tb.remote.Snippet;
10  
import net.luaos.tb.remote.SnippetTypes;
11  
import net.luaos.tb.tb07.PasswordUtil;
12  
13  
// That ImageSurface has the selector & the nice zoom stuff
14  
import prophecy.common.image.ImageSurface;
15  
import prophecy.common.image.RGBImage;
16  
17  
p-substance {
18  
  JFrame frame = showFrame();
19  
  hideConsole();
20  
  ScreenshotPanel screenshotPanel = new ScreenshotPanel(frame);
21  
  setFrameContents(frame, screenshotPanel);
22  
}
23  
24  
sclass ScreenshotPanel extends JPanel {
25  
  private JButton btnUpload;
26  
  private JFrame frame;
27  
  private int defaultDelay = 3000;
28  
  private ImageSurface imageSurface;
29  
  private int delayWithFrame = 3000;
30  
31  
  public ScreenshotPanel(JFrame frame) {
32  
    this.frame = frame;
33  
    getContentPane().setLayout(new LetterLayout("B", "I", "I").setBorder(10));
34  
35  
    imageSurface = new ImageSurface() {
36  
      public void setSelection(Rectangle r) {
37  
        super.setSelection(r);
38  
        btnUpload.setText(r != null ? "Upload screenshot (selected area)" : "Upload screenshot");
39  
      }
40  
    };
41  
42  
    JButton btnShoot = new JButton(new AbstractAction("Shoot!") {
43  
      public void actionPerformed(ActionEvent actionEvent) {
44  
        shoot();
45  
      }
46  
    });
47  
48  
    btnUpload = new JButton(new AbstractAction("Upload screenshot") {
49  
      public void actionPerformed(ActionEvent actionEvent) {
50  
        try {
51  
          String user = TBDefaultUser.getInAWTContext();
52  
          user = TBDefaultUser.askUserNameAWT(user);
53  
          if (user == null) return;
54  
          char[] pass = PasswordUtil.readPasswordFromFileOrAWT(user);
55  
          ServerConnection serverConnection = new ServerConnection();
56  
          if (!serverConnection.login(user, pass)) {
57  
            JOptionPane.showMessageDialog(null, "Could not log in");
58  
            return;
59  
          }
60  
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
61  
          BufferedImage image = imageSurface.getImage();
62  
          if (imageSurface.getSelection() != null)
63  
            image = new RGBImage(image).clip(imageSurface.getSelection()).getBufferedImage();
64  
          ImageIO.write(image, "png", outputStream);
65  
          String title = "A screenshot";
66  
          title = JOptionPane.showInputDialog(null, "Please enter screenshot title", title);
67  
          if (title == null) return;
68  
          Snippet snippet = new Snippet(title, "", SnippetTypes.SN_IMAGE);
69  
          snippet.isPublic = true;
70  
          snippet.setBlobData(outputStream.toByteArray(), "png");
71  
          try {
72  
            long id = serverConnection.createSnippet(snippet, true);
73  
            String msg = "Screen shot uploaded as #" + id + " (http://tinybrain.de/" + id + ")!";
74  
            JOptionPane.showMessageDialog(null, msg);
75  
          } catch (Exception e) {
76  
            Errors.popup(e);
77  
          }
78  
        } catch (Throwable e) {
79  
          Errors.popup(e);
80  
        }
81  
      }
82  
    });
83  
    btnUpload.setEnabled(false);
84  
    getContentPane().add("B", new Line(btnShoot, btnUpload));
85  
    getContentPane().add("I", GUIUtil.withTitle("Screenshot:", new JScrollPane(imageSurface)));
86  
87  
  }
88  
89  
  public void shoot() {
90  
    hideFrame();
91  
    SwingTimerHelper.runOnce(getDelay(), new Runnable() {
92  
      @Override
93  
      public void run() {
94  
        try {
95  
          BufferedImage image;
96  
          try {
97  
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
98  
            Rectangle screenRectangle = new Rectangle(screenSize);
99  
            Robot robot = new Robot();
100  
            image = robot.createScreenCapture(screenRectangle);
101  
          } finally {
102  
            showFrame();
103  
          }
104  
          imageSurface.setImage(image);
105  
          imageSurface.zoomToDisplaySize();
106  
          btnUpload.setEnabled(true);
107  
        } catch (Throwable e) {
108  
          Errors.popup(e);
109  
        }
110  
      }
111  
    });
112  
  }
113  
114  
  private int getDelay() {
115  
    return frame != null ? delayWithFrame : defaultDelay;
116  
  }
117  
118  
  private void hideFrame() {
119  
    if (frame != null)
120  
      frame.setVisible(false);
121  
  }
122  
123  
  private void showFrame() {
124  
    if (frame != null)
125  
      frame.setVisible(true);
126  
  }
127  
128  
  private JPanel getContentPane() {
129  
    return this;
130  
  }
131  
}

Author comment

Began life as a copy of #1003164

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003836
Snippet name: Screenshot Maker v2
Eternal ID of this version: #1003836/1
Text MD5: 59acfc59428ecb4fb5477507fbceea58
Transpilation MD5: 551812194e9b2495211b4c95c1654601
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-28 17:25:27
Source code size: 4325 bytes / 131 lines
Pitched / IR pitched: No / No
Views / Downloads: 536 / 691
Referenced in: [show references]