!759 lib 1003153 // magic.jar import ma.ma02.*; import drjava.util.*; import net.luaos.tb.remote.ServerConnection; import net.luaos.tb.remote.Snippet; import net.luaos.tb.remote.SnippetTypes; import net.luaos.tb.tb07.PasswordUtil; // That ImageSurface has the selector & the nice zoom stuff import prophecy.common.image.ImageSurface; import prophecy.common.image.RGBImage; p-substance { JFrame frame = showFrame(); hideConsole(); ScreenshotPanel screenshotPanel = new ScreenshotPanel(frame); setFrameContents(frame, screenshotPanel); } sclass ScreenshotPanel extends JPanel { private JButton btnUpload; private JFrame frame; private int defaultDelay = 3000; private ImageSurface imageSurface; private int delayWithFrame = 3000; public ScreenshotPanel(JFrame frame) { this.frame = frame; getContentPane().setLayout(new LetterLayout("B", "I", "I").setBorder(10)); imageSurface = new ImageSurface() { public void setSelection(Rectangle r) { super.setSelection(r); btnUpload.setText(r != null ? "Upload screenshot (selected area)" : "Upload screenshot"); } }; JButton btnShoot = new JButton(new AbstractAction("Shoot!") { public void actionPerformed(ActionEvent actionEvent) { shoot(); } }); btnUpload = new JButton(new AbstractAction("Upload screenshot") { public void actionPerformed(ActionEvent actionEvent) { try { String user = TBDefaultUser.getInAWTContext(); user = TBDefaultUser.askUserNameAWT(user); if (user == null) return; char[] pass = PasswordUtil.readPasswordFromFileOrAWT(user); ServerConnection serverConnection = new ServerConnection(); if (!serverConnection.login(user, pass)) { JOptionPane.showMessageDialog(null, "Could not log in"); return; } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedImage image = imageSurface.getImage(); if (imageSurface.getSelection() != null) image = new RGBImage(image).clip(imageSurface.getSelection()).getBufferedImage(); ImageIO.write(image, "png", outputStream); String title = "A screenshot"; title = JOptionPane.showInputDialog(null, "Please enter screenshot title", title); if (title == null) return; Snippet snippet = new Snippet(title, "", SnippetTypes.SN_IMAGE); snippet.isPublic = true; snippet.setBlobData(outputStream.toByteArray(), "png"); try { long id = serverConnection.createSnippet(snippet, true); String msg = "Screen shot uploaded as #" + id + " (http://tinybrain.de/" + id + ")!"; JOptionPane.showMessageDialog(null, msg); } catch (Exception e) { Errors.popup(e); } } catch (Throwable e) { Errors.popup(e); } } }); btnUpload.setEnabled(false); getContentPane().add("B", new Line(btnShoot, btnUpload)); getContentPane().add("I", GUIUtil.withTitle("Screenshot:", new JScrollPane(imageSurface))); } public void shoot() { hideFrame(); SwingTimerHelper.runOnce(getDelay(), new Runnable() { @Override public void run() { try { BufferedImage image; try { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle screenRectangle = new Rectangle(screenSize); Robot robot = new Robot(); image = robot.createScreenCapture(screenRectangle); } finally { showFrame(); } imageSurface.setImage(image); imageSurface.zoomToDisplaySize(); btnUpload.setEnabled(true); } catch (Throwable e) { Errors.popup(e); } } }); } private int getDelay() { return frame != null ? delayWithFrame : defaultDelay; } private void hideFrame() { if (frame != null) frame.setVisible(false); } private void showFrame() { if (frame != null) frame.setVisible(true); } private JPanel getContentPane() { return this; } }