I feel silly because this was such an easy question.
I fiddled around in jirb for 15 minutes and learned a bit.
It turns out I only needed to write 4 lines of code:
tk = java.awt.Toolkit.getDefaultToolkit().getScreenSize()
rect = java.awt.Rectangle.new(tk)
screencapture = java.awt.Robot.new().createScreenCapture(rect)
my_file = java.io.File.new("screencapture.jpg")
javax.imageio.ImageIO.write(screencapture, "jpg", my_file)
My biceps are bulging though because I just squashed 24 lines of Java
into 4 lines of JRuby.
I think this is a good way for me to write Java from now on.
Bring it on.
--Audrey
On 9/22/09, Audrey A Lee <audrey.l...@gmail.com> wrote:
>
> Hello JRuby People,
>
> I'm not quite ready to JRubyify yet but,
> I'm working on a mini-project which requires that I screen-capture a
> portion of my x-display on a linux box.
>
> It looks like I can use a class in Java named "Robot" to do this:
> - http://java.sun.com/javase/6/docs/api/java/awt/Robot.html
>
> I figure any class (even if it is a Java class) named "Robot" deserves
> my attention.
>
> So I ran this query:
> - http://www.google.com/search?q=java+robot+screencapture
>
> And this page looks good:
> - http://www.rgagnon.com/javadetails/java-0489.html
>
> I see this example:
>
> import java.awt.AWTException;
> import java.awt.Robot;
> import java.awt.Rectangle;
> import java.awt.Toolkit;
> import java.awt.image.BufferedImage;
> import java.io.*;
> import javax.imageio.ImageIO;
>
> class ScreenCapture {
> public static void main(String args[]) throws
> AWTException, IOException {
> // capture the whole screen
> BufferedImage screencapture = new Robot().createScreenCapture(
> new Rectangle(Toolkit.getDefaultToolkit().getScreenSize
> ()) );
>
> // Save as JPEG
> File file = new File("screencapture.jpg");
> ImageIO.write(screencapture, "jpg", file);
>
> // Save as PNG
> // File file = new File("screencapture.png");
> // ImageIO.write(screencapture, "png", file);
> }
> }
>
> My question:
> Is it possible to transform the above Java-syntax into Ruby-syntax
> which could be interpreted by JRuby?
>
> Or I could ask it this way:
> How do I transform the above Java-syntax into JRuby-syntax?
>
> --Audrey
>
>