diff --git a/chapter_18/03_creating_object_from_a_text_file.rb b/chapter_18/03_creating_object_from_a_text_file.rb index 0607e20..a6b6ecd 100644 --- a/chapter_18/03_creating_object_from_a_text_file.rb +++ b/chapter_18/03_creating_object_from_a_text_file.rb @@ -32,25 +32,29 @@ def draw # A Class to describe a "Bubble" # class Bubble + include Processing::Proxy + + attr_reader :width, :height # The constructor initializes color and size # Location is filled randomly def initialize(r, g, diameter) - @x, @y = $app.random($app.width), $app.height + @width, @height = $app.width, $app.height + @x, @y = rand(width), height @r, @g = r, g @diameter = diameter end # Display the Bubble def display - $app.stroke 0 - $app.fill @r, @g, 255, 150 - $app.ellipse @x, @y, @diameter, @diameter + stroke 0 + fill @r, @g, 255, 150 + ellipse @x, @y, @diameter, @diameter end # Move the bubble def drift - @y += $app.random(-3, -0.1) - @x += $app.random(-1, 1) - @y = $app.height + @diameter * 2 if @y < -@diameter * 2 + @y += rand(-3 .. -0.1) + @x += rand(-1 .. 1.0) + @y = height + @diameter * 2 if @y < -@diameter * 2 end end diff --git a/chapter_18/04_loading_and_saving_data_to_text_file.rb b/chapter_18/04_loading_and_saving_data_to_text_file.rb index dea2855..425e95a 100644 --- a/chapter_18/04_loading_and_saving_data_to_text_file.rb +++ b/chapter_18/04_loading_and_saving_data_to_text_file.rb @@ -56,13 +56,16 @@ def save_data # A Class to describe a "Bubble" # class Bubble - attr_reader :r, :g, :diameter + include Processing::Proxy + + attr_reader :width, :height, :r, :g, :diameter # The constructor initializes color and size # Location is filled randomly def initialize(r, g, diameter) - @x = $app.random($app.width) - @y = $app.height + @width, @height = $app.width, $app.height + @x = random(width) + @y = height @r = r @g = g @diameter = diameter @@ -70,29 +73,29 @@ def initialize(r, g, diameter) # True or False if point is inside circle def rollover(mx, my) - $app.dist(mx, my, @x, @y) < diameter / 2 + dist(mx, my, @x, @y) < diameter / 2 end # Change Bubble variables def change - @r = $app.constrain(@r + $app.random(-10, 10), 0, 255) - @g = $app.constrain(@g + $app.random(-10, 10), 0, 255) - @diameter = $app.constrain(@diameter + $app.random(-2, 4), 4, 72) + @r = constrain(@r + rand(-10 .. 10), 0, 255) + @g = constrain(@g + rand(-10 .. 10), 0, 255) + @diameter = constrain(@diameter + rand(-2 .. 4), 4, 72) end # Display the Bubble def display - $app.stroke 0 - $app.fill @r, @g, 255, 150 - $app.ellipse @x, @y, @diameter, @diameter + stroke 0 + fill @r, @g, 255, 150 + ellipse @x, @y, @diameter, @diameter end # Move the bubble def drift - @y += $app.random(-3, -0.1) - @x += $app.random(-1, 1) + @y += rand(-3 .. -0.1) + @x += rand(-1 .. 1.0) if @y < -@diameter * 2 - @y = $app.height + @diameter * 2 + @y = height + @diameter * 2 end end end diff --git a/chapter_18/05_parsing_yahoos_xml_weather_feed_manually.rb b/chapter_18/05_parsing_yahoos_xml_weather_feed_manually.rb index 5bc69a1..463867b 100644 --- a/chapter_18/05_parsing_yahoos_xml_weather_feed_manually.rb +++ b/chapter_18/05_parsing_yahoos_xml_weather_feed_manually.rb @@ -47,6 +47,7 @@ def mouse_pressed # A WeatherGrabber class # class WeatherGrabber + include Processing::Proxy attr_reader :temperature, :weather, :zip attr_writer :zip @@ -61,7 +62,7 @@ def request_weather # Get all the HTML/XML source code into an array of strings # (each line is one element in the array) url = "http://xml.weather.yahoo.com/forecastrss?p=" + @zip - lines = $app.load_strings(url) + lines = load_strings(url) # Turn array into one long String xml = lines.join # join(lines, ""); diff --git a/chapter_18/07_loading_a_url_with_simpleml.rb b/chapter_18/07_loading_a_url_with_simpleml.rb deleted file mode 100644 index 966d305..0000000 --- a/chapter_18/07_loading_a_url_with_simpleml.rb +++ /dev/null @@ -1,90 +0,0 @@ -# -# Example 18-7: Loading a URL with simpleML -# -load_library "simpleML" -import "simpleML" - -def setup - size 200, 200 - @html = "" # String to hold data from request - @counter = 0 # Counter to animate rectangle across window - @back = 255 # Background brightness - - # Create and make an asynchronous request using - # the Request object from the library - @request = HTMLRequest.new(self, "http://www.yahoo.com") - @request.makeRequest - @timer = Timer.new(5000) - @timer.start - background 0 -end - -def draw - background @back - - # A request is made every 5s. - # The data is not received here, however, this is only the request. - if @timer.finished? - @request.make_request - # XXX: was println("Making request!"); - puts "Making request!" - @timer.start - end - - # XXX: There are still issues related to events from imported library - # so we call the net_event method ourselves - # When a request is finished the data the available flag is set to true - # and we get a chance to read the data returned by the request - if @request.available? - net_event(@request) - end - - # Draw some lines with colors based on characters from data retrieved - width.times do |i| - if i < @html.length - c = @html[i] - stroke c, 150 - line i, 0, i, height - end - end - - # Animate rectangle and dim rectangle - fill 255 - noStroke - rect @counter, 0, 10, height - @counter = (@counter + 1) % width - @back = constrain(@back - 1, 0, 255) -end - -# When a request is finished the data is received in the netEvent() -# function which is automatically called whenever data is ready. -def net_event(ml) - @html = ml.readRawSource # Read the raw data - @back = 255 # Reset background - puts "Request completed!" # Print message -end - -# -# Timer Class from Chapter 10 -# -class Timer - def initialize(total_time) - @total_time = total_time - @running = false - end - - def start - @running = true - @saved_time = $app.millis - end - - def finished? - finished = $app.millis - @saved_time > @total_time - if @running && finished - @running = false - return true - else - return false - end - end -end diff --git a/chapter_18/08_loading_xml_with_simpleml.rb b/chapter_18/08_loading_xml_with_simpleml.rb deleted file mode 100644 index 3738988..0000000 --- a/chapter_18/08_loading_xml_with_simpleml.rb +++ /dev/null @@ -1,36 +0,0 @@ -# -# Example 18-8: Loading XML with simpleML -# -load_library "simpleML" -import "simpleML" - -def setup - size 200, 200 - - # Creating and starting the request - # An array of XML elements can be retrieved using getElementArray. - # This only works for elements with the same name that appear multiple times in the XML document. - @request = XMLRequest.new(self, "http://rss.news.yahoo.com/rss/topstories") - @request.makeRequest -end - -def draw - # XXX: There are still issues related to events from imported library - # so we call the net_event method ourselves - # When a request is finished the data the available flag is set to true - # and we get a chance to read the data returned by the request - if @request.available? - net_event(@request) - no_loop - end -end - -# When a request is finished the data is received in the netEvent() -# function which is automatically called whenever data is ready. -def net_event(ml) - # Retrieving an array of all XML elements inside " title* " tags - headlines = ml.getElementArray("title") - headlines.each do |headline| - puts headline # XXX: was println(headlines[i]); - end -end \ No newline at end of file diff --git a/chapter_18/09_using_processings_xml_library.rb b/chapter_18/09_using_processings_xml_library.rb index 1210e36..c7af6a9 100644 --- a/chapter_18/09_using_processings_xml_library.rb +++ b/chapter_18/09_using_processings_xml_library.rb @@ -1,32 +1,27 @@ # # Example 18-9: Using Processing's XML library # -import "processing.xml" + def setup size 200, 200 smooth # Load an XML document - xml = XMLElement.new(self, "bubbles.xml") + xml = loadXML("bubbles.xml") # Getting the total number of Bubble objects with getChildCount(). totalBubbles = xml.get_child_count @bubbles = [] # Get all the child elements - children = xml.get_children + children = xml.get_children("bubble") children.each do |child| - # The diameter is child 0 - diameterElement = child.get_child(0) - - # The diameter is the content of the first element while red and green are attributes of the second. + diameterElement = child.get_child("diameter") diameter = diameterElement.get_content.to_i - - # Color is child 1 - colorElement = child.get_child(1) - r = colorElement.get_int_attribute("red") - g = colorElement.get_int_attribute("green") + colorElement = child.get_child("color") + r = colorElement.get_int("red") + g = colorElement.get_int("green") # Make a new Bubble object with values from XML document @bubbles << Bubble.new(r, g, diameter) @@ -47,9 +42,13 @@ def draw # A Bubble class # class Bubble + include Processing::Proxy + attr_reader :width, :height + def initialize(r, g, diameter) - @x = $app.random($app.width) - @y = $app.height + @width, @height = $app.width, $app.height + @x = random(width) + @y = height @r = r @g = g @diameter = diameter @@ -57,17 +56,17 @@ def initialize(r, g, diameter) # Display Bubble def display - $app.stroke 0 - $app.fill @r, @g, 255, 150 - $app.ellipse @x, @y, @diameter, @diameter + stroke 0 + fill @r, @g, 255, 150 + ellipse @x, @y, @diameter, @diameter end # Bubble drifts upwards def drift - @y += $app.random(-3, -0.1) - @x += $app.random(-1, 1) + @y += rand(-3 .. -0.1) + @x += rand(-1 .. 1.0) if @y < -@diameter * 2 - @y = $app.height + @diameter * 2 + @y = height + @diameter * 2 end end end diff --git a/chapter_18/11_yahoo_search_visualization.rb b/chapter_18/11_yahoo_search_visualization.rb index 44da881..11394f7 100644 --- a/chapter_18/11_yahoo_search_visualization.rb +++ b/chapter_18/11_yahoo_search_visualization.rb @@ -1,8 +1,8 @@ # # Example 18-11: Yahoo search visualization # -load_library "pyahoo" -import "pyahoo" +load_library :pyahoo +java_import "pyahoo" # The names to search NAMES = %w{ Aliki Cleopatra Penelope Daniel Peter }