Skip to content

Commit e516627

Browse files
author
monkstone
committed
update chapter 18
1 parent bb6d2b3 commit e516627

7 files changed

+50
-169
lines changed

chapter_18/03_creating_object_from_a_text_file.rb

+11-7
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,29 @@ def draw
3232
# A Class to describe a "Bubble"
3333
#
3434
class Bubble
35+
include Processing::Proxy
36+
37+
attr_reader :width, :height
3538
# The constructor initializes color and size
3639
# Location is filled randomly
3740
def initialize(r, g, diameter)
38-
@x, @y = $app.random($app.width), $app.height
41+
@width, @height = $app.width, $app.height
42+
@x, @y = rand(width), height
3943
@r, @g = r, g
4044
@diameter = diameter
4145
end
4246

4347
# Display the Bubble
4448
def display
45-
$app.stroke 0
46-
$app.fill @r, @g, 255, 150
47-
$app.ellipse @x, @y, @diameter, @diameter
49+
stroke 0
50+
fill @r, @g, 255, 150
51+
ellipse @x, @y, @diameter, @diameter
4852
end
4953

5054
# Move the bubble
5155
def drift
52-
@y += $app.random(-3, -0.1)
53-
@x += $app.random(-1, 1)
54-
@y = $app.height + @diameter * 2 if @y < -@diameter * 2
56+
@y += rand(-3 .. -0.1)
57+
@x += rand(-1 .. 1.0)
58+
@y = height + @diameter * 2 if @y < -@diameter * 2
5559
end
5660
end

chapter_18/04_loading_and_saving_data_to_text_file.rb

+16-13
Original file line numberDiff line numberDiff line change
@@ -56,43 +56,46 @@ def save_data
5656
# A Class to describe a "Bubble"
5757
#
5858
class Bubble
59-
attr_reader :r, :g, :diameter
59+
include Processing::Proxy
60+
61+
attr_reader :width, :height, :r, :g, :diameter
6062

6163
# The constructor initializes color and size
6264
# Location is filled randomly
6365
def initialize(r, g, diameter)
64-
@x = $app.random($app.width)
65-
@y = $app.height
66+
@width, @height = $app.width, $app.height
67+
@x = random(width)
68+
@y = height
6669
@r = r
6770
@g = g
6871
@diameter = diameter
6972
end
7073

7174
# True or False if point is inside circle
7275
def rollover(mx, my)
73-
$app.dist(mx, my, @x, @y) < diameter / 2
76+
dist(mx, my, @x, @y) < diameter / 2
7477
end
7578

7679
# Change Bubble variables
7780
def change
78-
@r = $app.constrain(@r + $app.random(-10, 10), 0, 255)
79-
@g = $app.constrain(@g + $app.random(-10, 10), 0, 255)
80-
@diameter = $app.constrain(@diameter + $app.random(-2, 4), 4, 72)
81+
@r = constrain(@r + rand(-10 .. 10), 0, 255)
82+
@g = constrain(@g + rand(-10 .. 10), 0, 255)
83+
@diameter = constrain(@diameter + rand(-2 .. 4), 4, 72)
8184
end
8285

8386
# Display the Bubble
8487
def display
85-
$app.stroke 0
86-
$app.fill @r, @g, 255, 150
87-
$app.ellipse @x, @y, @diameter, @diameter
88+
stroke 0
89+
fill @r, @g, 255, 150
90+
ellipse @x, @y, @diameter, @diameter
8891
end
8992

9093
# Move the bubble
9194
def drift
92-
@y += $app.random(-3, -0.1)
93-
@x += $app.random(-1, 1)
95+
@y += rand(-3 .. -0.1)
96+
@x += rand(-1 .. 1.0)
9497
if @y < -@diameter * 2
95-
@y = $app.height + @diameter * 2
98+
@y = height + @diameter * 2
9699
end
97100
end
98101
end

chapter_18/05_parsing_yahoos_xml_weather_feed_manually.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def mouse_pressed
4747
# A WeatherGrabber class
4848
#
4949
class WeatherGrabber
50+
include Processing::Proxy
5051
attr_reader :temperature, :weather, :zip
5152
attr_writer :zip
5253

@@ -61,7 +62,7 @@ def request_weather
6162
# Get all the HTML/XML source code into an array of strings
6263
# (each line is one element in the array)
6364
url = "http://xml.weather.yahoo.com/forecastrss?p=" + @zip
64-
lines = $app.load_strings(url)
65+
lines = load_strings(url)
6566

6667
# Turn array into one long String
6768
xml = lines.join # join(lines, "");

chapter_18/07_loading_a_url_with_simpleml.rb

-90
This file was deleted.

chapter_18/08_loading_xml_with_simpleml.rb

-36
This file was deleted.
+19-20
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
#
22
# Example 18-9: Using Processing's XML library
33
#
4-
import "processing.xml"
4+
55

66
def setup
77
size 200, 200
88
smooth
99
# Load an XML document
10-
xml = XMLElement.new(self, "bubbles.xml")
10+
xml = loadXML("bubbles.xml")
1111

1212
# Getting the total number of Bubble objects with getChildCount().
1313
totalBubbles = xml.get_child_count
1414
@bubbles = []
1515

1616
# Get all the child elements
17-
children = xml.get_children
17+
children = xml.get_children("bubble")
1818

1919
children.each do |child|
20-
# The diameter is child 0
21-
diameterElement = child.get_child(0)
22-
23-
# The diameter is the content of the first element while red and green are attributes of the second.
20+
diameterElement = child.get_child("diameter")
2421
diameter = diameterElement.get_content.to_i
25-
26-
# Color is child 1
27-
colorElement = child.get_child(1)
28-
r = colorElement.get_int_attribute("red")
29-
g = colorElement.get_int_attribute("green")
22+
colorElement = child.get_child("color")
23+
r = colorElement.get_int("red")
24+
g = colorElement.get_int("green")
3025

3126
# Make a new Bubble object with values from XML document
3227
@bubbles << Bubble.new(r, g, diameter)
@@ -47,27 +42,31 @@ def draw
4742
# A Bubble class
4843
#
4944
class Bubble
45+
include Processing::Proxy
46+
attr_reader :width, :height
47+
5048
def initialize(r, g, diameter)
51-
@x = $app.random($app.width)
52-
@y = $app.height
49+
@width, @height = $app.width, $app.height
50+
@x = random(width)
51+
@y = height
5352
@r = r
5453
@g = g
5554
@diameter = diameter
5655
end
5756

5857
# Display Bubble
5958
def display
60-
$app.stroke 0
61-
$app.fill @r, @g, 255, 150
62-
$app.ellipse @x, @y, @diameter, @diameter
59+
stroke 0
60+
fill @r, @g, 255, 150
61+
ellipse @x, @y, @diameter, @diameter
6362
end
6463

6564
# Bubble drifts upwards
6665
def drift
67-
@y += $app.random(-3, -0.1)
68-
@x += $app.random(-1, 1)
66+
@y += rand(-3 .. -0.1)
67+
@x += rand(-1 .. 1.0)
6968
if @y < -@diameter * 2
70-
@y = $app.height + @diameter * 2
69+
@y = height + @diameter * 2
7170
end
7271
end
7372
end

chapter_18/11_yahoo_search_visualization.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#
22
# Example 18-11: Yahoo search visualization
33
#
4-
load_library "pyahoo"
5-
import "pyahoo"
4+
load_library :pyahoo
5+
java_import "pyahoo"
66

77
# The names to search
88
NAMES = %w{ Aliki Cleopatra Penelope Daniel Peter }

0 commit comments

Comments
 (0)