Re: [la-ruby] Java, Ruby, JRuby, JRubify some Java?

From: user 1.
Sent on: Wednesday, September 23, 2009 4:39 PM
JRuby People,

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.get­DefaultToolkit().get­ScreenSize()
rect = java.awt.Rectangle.n­ew(tk)
screencapture = java.awt.Robot.new()­.createScreenCapture­(rect)
my_file = java.io.File.new("sc­reencapture.jpg")
javax.imageio.ImageI­O.write(screencaptur­e, "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/23/09, jay <[address removed]> wrote:
> It's essentially a line for line mapping. You just have to learn the jruby
> syntax for importing java classes which you can find here.
> http://kenai.com/...­
>
> let me know if you run into any specific problems and I'll help out.
>
> Jay
>
> On Wed, Sep 23, 2009 at 8:24 AM, Audrey A Lee <[address removed]>
> 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.c...­
>>
>> I figure any class (even if it is a Java class) named "Robot" deserves
>> my attention.
>>
>> So I ran this query:
>> ?- http://www.google...­
>>
>> And this page looks good:
>> ?- http://www.rgagno...­
>>
>> 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().createScreen­Capture(
>> ? ? ? ? ? new Rectangle(Toolkit.ge­tDefaultToolkit().ge­tScreenSize()) );
>>
>> ? ? // Save as JPEG
>> ? ? File file = new File("screencapture.­jpg");
>> ? ? ImageIO.write(screen­capture, "jpg", file);
>>
>> ? ? // Save as PNG
>> ? ? // File file = new File("screencapture.­png");
>> ? ? // ImageIO.write(screen­capture, "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
>>
>>
>>
>>
>> --
>> Please Note: If you hit "REPLY", your message will be sent to everyone on
>> this mailing list ([address removed])
>> http://www.meetup...­
>> This message was sent by Audrey A Lee ([address removed]) from
>> The Los Angeles Ruby/Rails Meetup Group.
>> To learn more about Audrey A Lee, visit his/her member profile:
>> http://www.meetup...­
>> To unsubscribe or to update your mailing list settings, click here:
>> http://www.meetup...­
>> Meetup Support: [address removed]
>> 632 Broadway, New York, NY 10012 USA
>>
>
>
>
>
>
> --
> Please Note: If you hit "REPLY", your message will be sent to everyone on
> this mailing list ([address removed])
> This message was sent by jay ([address removed]) from The Los Angeles
> Ruby/Rails Meetup Group.
> To learn more about jay, visit his/her member profile
> To unsubscribe or to update your mailing list settings, click here
>
> Meetup Support: [address removed]
> 632 Broadway, New York, NY 10012 USA

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy