Skip to content

Commit

Permalink
update chapter 18
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Feb 25, 2014
1 parent bb6d2b3 commit e516627
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 169 deletions.
18 changes: 11 additions & 7 deletions chapter_18/03_creating_object_from_a_text_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 16 additions & 13 deletions chapter_18/04_loading_and_saving_data_to_text_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,46 @@ 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
end

# 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
3 changes: 2 additions & 1 deletion chapter_18/05_parsing_yahoos_xml_weather_feed_manually.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def mouse_pressed
# A WeatherGrabber class
#
class WeatherGrabber
include Processing::Proxy
attr_reader :temperature, :weather, :zip
attr_writer :zip

Expand All @@ -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, "");
Expand Down
90 changes: 0 additions & 90 deletions chapter_18/07_loading_a_url_with_simpleml.rb

This file was deleted.

36 changes: 0 additions & 36 deletions chapter_18/08_loading_xml_with_simpleml.rb

This file was deleted.

39 changes: 19 additions & 20 deletions chapter_18/09_using_processings_xml_library.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -47,27 +42,31 @@ 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
end

# 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
4 changes: 2 additions & 2 deletions chapter_18/11_yahoo_search_visualization.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down

0 comments on commit e516627

Please sign in to comment.