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

Thursday, 18 July 2013

A simple ruby-processing paint application

#
# Painting Application in Ruby-Processing
# 
# Click and drag the mouse to paint. 
#

load_library :control_panel

attr_reader :panel, :red, :alpha, :green, :erase, :brush, :blue, :sz

def setup
  size(640, 360)
  color_mode RGB, 1.0
  background(0.8)
  control_panel do |c|
    c.slider    :red, 0.0..1.0, 1.0
    c.slider    :green, 0.0..1.0, 0.0
    c.slider    :blue, 0.0..1.0, 0.0
    c.slider    :alpha,  0.0..1.0, 0.9
    c.menu(:brush, ['big', 'medium', 'fine', 'pencil'], 'medium') {|m| load_menu_item(m) }
    c.checkbox  :erase
    c.button    :print_color
    c.button    :clear!
    @panel = c
  end
end

def draw
  no_stroke
  erase ? fill(0.8) : fill(red, green, blue, alpha)
  panel.set_visible true if self.visible?
  if mouse_pressed?
    ellipse(mouse_x, mouse_y, sz, sz)
  end
end

def clear!
  background(0.8)
end

def load_menu_item m
  chosen = brush[m]
  @sz = {'big' => 20, 'medium' => 10, 'fine' => 5, 'pencil' => 2}[chosen]
  return chosen
end

def print_color
  puts "red=#{(red*255).to_i}, blue=#{(blue*255).to_i}, green=#{(green*255).to_i}, alpha=#{(alpha*255).to_i}"
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