Here is the sketch code test_register.rb:-
# A simple demonstration of vanilla processing # 'reflection' methods # in ruby-processing see register_send.rb for the guts... require_relative 'register_send' def setup size 200, 200 RegisterSend.new self no_loop end def draw fill(0, 0, 200) ellipse(120, 120, 60, 60) endHere's where the voodoo gets done in register_send.rb (ruby class becomes a java class and registers methods with the processing $app):-
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 RegisterSend attr_reader :parent def initialize(parent) @parent = parent parent.java_send :registerMethod, [java.lang.String, java.lang.Object], :draw, self parent.java_send :registerMethod, [java.lang.String, java.lang.Object], :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! endHere is the resulting sketch and the console, shows output to console:-
Actually I'm a bit worried about all this voodoo, it's bad enough that processing is making so much use of this reflection crap, without me making it worse here. Also I can't seem to register either mouseEvent or keyEvent as yet (methods requiring parameters which should probably be a simple java object, that can get cast later to an event?).
No comments:
Post a Comment