Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Wednesday 9 July 2014

Exploring PApplet with jruby

There is a lot of clever stuff that goes on in jruby, that allows you to treat java objects as if they were ruby. Recently I had been interested in refactoring the ruby-processings library loader, but that may be premature since changes seem to be afoot with the jruby class loader?

Anyway what is the current situation?
You can simply 'require a jar' to add the jar contents to your CLASSPATH, you can then start using the java classes as if they were ruby objects see PApplet example below:-
require 'java'
require '/home/tux/processing-2.2.1/core/library/core.jar'
app = Java::ProcessingCore::PApplet.new
puts "inspect: #{app.inspect}"
app.set_size(200, 200)
puts "\nto_s: #{app.to_s}"
# print the default renderer
puts "\nrenderer: #{app.sketch_renderer}"
# prints a list of methods including '_size'
puts "\nmethods:\n #{ app.methods.reject{ |method| /_size/ !~ method.to_s} }"


Output:-

inspect: #<Java::ProcessingCore::PApplet:0x4656c38e>

to_s: processing.core.PApplet[panel0,0,0,200x200,invalid,layout=java.awt.FlowLayout]

renderer: processing.core.PGraphicsJava2D

methods:
 [:text_size, :unregister_size, :register_size, :preferred_size_set?, :preferred_size=, :maximum_size=, :set_size, :is_minimum_size_set, :preferred_size, :set_preferred_size, :is_minimum_size_set?, :is_maximum_size_set?, :preferred_size_set, :set_maximum_size, :get_size, :minimum_size_set, :maximum_size_set?, :minimum_size=, :get_maximum_size, :get_minimum_size, :is_preferred_size_set?, :maximum_size, :minimum_size_set?, :maximum_size_set, :set_minimum_size, :is_preferred_size_set, :minimum_size, :get_preferred_size, :is_maximum_size_set]


Alternatively you can make use of the global CLASSPATH directly:-
require 'java'
$CLASSPATH << '/home/tux/processing-2.2.1/core/library/core.jar'
app = Java::ProcessingCore::PApplet.new
puts "inspect: #{app.inspect}"
app.set_size(200, 200)
puts "\nto_s: #{app.to_s}"
# print the default renderer
puts "\nrenderer: #{app.sketch_renderer}"
# prints a list of methods including '_size'
puts "\nmethods:\n #{ app.methods.reject{ |method| /_size/ !~ method.to_s} }"

There is yet another way to do it using classloader directly:-

require 'java'
F = java.io.File
class_loader = JRuby.runtime.jruby_class_loader
class_loader.add_url(F.new('/home/tux/processing-2.2.1/core/library/core.jar').to_url)
app = Java::ProcessingCore::PApplet.new
puts "inspect: #{app.inspect}"
app.set_size(200, 200)
puts "\nto_s: #{app.to_s}"
# print the default renderer
puts "\nrenderer: #{app.sketch_renderer}"
# prints a list of methods including '_size'
puts "\nmethods:\n #{ app.methods.reject{ |method| /_size/ !~ method.to_s} }"



In ruby-processing exported sketches, the jars are currently added to the java CLASSPATH (ie on the java side) this may not be necessary anymore (no Applets), the current export process can mess with nested libraries (on the plus side you only need specify a directory to include the contained jars).
java -cp ".:../lib/ruby/*:../lib/*:../lib/library/*" org.jruby.Main ../lib/ruby-processing/runners/run.rb drawolver.rb

No comments:

Post a Comment

Followers

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2