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

Sunday, 21 April 2013

More Explorations in Calling The Processing Register Method

Here we explore using "java_method" as an alternative to the "java_send", which may be inefficient, and in this case we call it twice, once to "register" the "pre" method and once to register the "draw" method so some progress there.


Sketch:
# A simple demonstration of vanilla processing # 'reflection' methods 
# in ruby-processing see register_method.rb for the guts...

require_relative 'register_method'

def setup
  size 200, 200
  RegisterMethod.new self
  no_loop
end

def draw
  fill(0, 0, 200)
  ellipse(120, 120, 60, 60)
end

Class with methods to "register" with the processing sketch (applet whatever), note the register method requires a java class Object hence the need for the "become_java!" magic
require 'jruby/core_ext' # required to allow become_java!

# This class demonstrates how to use 'reflection' methods in ruby-processing
# NB: the class must become a java object to get registered. This is an
# advanced feature in vanilla processing, mainly used by libraries.
class RegisterMethod
  attr_reader :parent

  def initialize(parent)
    @parent = parent
    register = parent.java_method :registerMethod, [java.lang.String, java.lang.Object]
    register.call(:draw, self)
    register.call(:pre, self)
  end

  def pre
    puts 'before draw'
    parent.background(100)
  end

  def draw
    puts 'at begin draw...'
    parent.fill(200, 100)
    parent.ellipse(100, 100, 60, 60)
  end
  # putting become_java! here works OK
  become_java!
end

No comments:

Post a Comment

Followers

Blog Archive

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