Skip to content

Instantly share code, notes, and snippets.

@monkstone
Last active March 7, 2017 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkstone/0e1aca31136beeeba56b6af5224fd116 to your computer and use it in GitHub Desktop.
Save monkstone/0e1aca31136beeeba56b6af5224fd116 to your computer and use it in GitHub Desktop.
The power of ruby to be concise
# frozen_string_literal: false
if RUBY_PLATFORM == 'java'
require 'WordCram.jar'
require 'jsoup-1.7.2.jar'
require 'cue.language.jar'
wc = %w(WordAngler WordColorer WordCram WordFonter WordPlacer WordSkipReason)
sh = %w(Colorers ImageShaper Word ShapeBasedPlacer)
WC = wc.concat(sh).freeze
WC.each do |klass|
java_import "wordcram.#{klass}"
end
end
require 'ruby_wordcram'
def settings
size(800, 600)
end
def setup
sketch_title 'US Male and Female First Names'
background 255
names = File.readlines(data_path('names.txt')).map do |line|
name, frequency, sex = line.split
col = 'f' == sex ? color('#f36d91') : color('#476dd5')
Word.new(name, frequency.to_f).set_color(col)
end
WordCram.new(self)
.from_words(names.to_java(Java::Wordcram::Word)) # cast to java array
.with_font(create_font(data_path('MINYN___.TTF'), 1))
.sized_by_weight(12, 60)
.draw_all
end
require 'ruby_wordcram'
attr_reader :names, :wc, :reset
def settings
size(800, 600)
end
def setup
sketch_title 'US Male and Female First Names Reset'
@names = File.readlines(data_path('names.txt')).map do |line|
name, frequency, sex = line.split
col = 'f' == sex ? color('#f36d91') : color('#476dd5')
Word.new(name, frequency.to_f).set_color(col)
end
@reset = true
make_word_cram
end
def make_word_cram
# NB: see cast to java array
@wc = WordCram.new(self)
.from_words(names.to_java(Java::Wordcram::Word))
.with_font(create_font(data_path('MINYN___.TTF'), 1))
.sized_by_weight(12, 60)
end
def draw
background 255 if reset
@reset = false
if wc.has_more
wc.draw_next
else
puts 'done'
no_loop
end
end
def mouse_clicked
make_word_cram
@reset = true
loop
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment