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