Skip to content

Commit 72b4743

Browse files
committed
Added samples 9 and 10 for chapter 18
Added pyahoo library Updaed data-1.txt and data-2.txt to make the bubbles look better.
1 parent 8a6fef0 commit 72b4743

File tree

9 files changed

+355
-11
lines changed

9 files changed

+355
-11
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#
2+
# Example 18-9: Using Processing's XML library
3+
#
4+
import "processing.xml"
5+
6+
def setup
7+
size 200, 200
8+
smooth
9+
# Load an XML document
10+
xml = XMLElement.new(self, "bubbles.xml")
11+
12+
# Getting the total number of Bubble objects with getChildCount().
13+
totalBubbles = xml.get_child_count
14+
@bubbles = []
15+
16+
# Get all the child elements
17+
children = xml.get_children
18+
19+
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.
24+
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")
30+
31+
# Make a new Bubble object with values from XML document
32+
@bubbles << Bubble.new(r, g, diameter)
33+
end
34+
end
35+
36+
def draw
37+
background 255
38+
39+
# Display and move all bubbles
40+
@bubbles.each do |bubble|
41+
bubble.display
42+
bubble.drift
43+
end
44+
end
45+
46+
#
47+
# A Bubble class
48+
#
49+
class Bubble
50+
def initialize(r, g, diameter)
51+
@x = $app.random($app.width)
52+
@y = $app.height
53+
@r = r
54+
@g = g
55+
@diameter = diameter
56+
end
57+
58+
# XXX: Unused - probably best to remove instead
59+
# True or False if point is inside circle
60+
#def rollover(mx, my)
61+
# return $app.dist(mx, my, @x, @y) < @diameter / 2
62+
#end
63+
64+
65+
# XXX: Unused - probably best to remove instead
66+
# Change Bubble variables
67+
#def change
68+
# @r = $app.constrain(@r + $app.random(-10, 10), 0, 255)
69+
# @g = $app.constrain(@g + $app.random(-10, 10), 0, 255)
70+
# @diameter = $app.constrain(@diameter + $app.random(-2, 4), 4, 72)
71+
#end
72+
73+
# Display Bubble
74+
def display
75+
$app.stroke 0
76+
$app.fill @r, @g, 255, 150
77+
$app.ellipse @x, @y, @diameter, @diameter
78+
end
79+
80+
# Bubble drifts upwards
81+
def drift
82+
@y += $app.random(-3, -0.1)
83+
@x += $app.random(-1, 1)
84+
if @y < -@diameter * 2
85+
@y = $app.height + @diameter * 2
86+
end
87+
end
88+
end

chapter_18/10_a_yahoo_search.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#
2+
# Example 18-10: A Yahoo search
3+
#
4+
load_library "pyahoo"
5+
import "pyahoo"
6+
7+
def setup
8+
size 400, 400
9+
# Create a YahooSearch object.
10+
# You have to pass in the API key given to you by Yahoo.
11+
@yahoo = YahooSearch.new(self, "YOUR API KEY HERE")
12+
end
13+
14+
def draw
15+
background 255
16+
17+
# When a request is finished the available
18+
# flag is set to true - and we get a chance to read
19+
# the data returned by the request
20+
if @yahoo.available?
21+
# Get Titles and URLs
22+
titles = @yahoo.get_titles
23+
24+
# Search results arrive as an array of Strings.
25+
# You can also get the summaries with getSummaries().
26+
urls = @yahoo.get_urls
27+
28+
titles.each_with_index do |title, i|
29+
puts "__________"
30+
puts title
31+
puts urls[i]
32+
end
33+
34+
no_loop
35+
end
36+
end
37+
38+
def mouse_pressed
39+
# Search for a String. By default you will get back 10 results.
40+
# If you want more (or less), you can request a specific number by
41+
# saying: yahoo.search("processing.org", 30);
42+
@yahoo.search "processing.org"
43+
end
44+
45+
# XXX: There are still issues related to events from imported library
46+
# so we're not implementing this yet.
47+
#def search_event(yahoo)
48+
# # Get Titles and URLs
49+
# titles = yahoo.get_titles
50+
# # Search results arrive as an array of Strings.
51+
# # You can also get the summaries with getSummaries().
52+
# urls = yahoo.get_urls
53+
#
54+
# titles.each_with_index do |title, i|
55+
# puts "__________"
56+
# puts title
57+
# puts urls[i]
58+
# end
59+
#end

chapter_18/data/bubbles.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<bubbles>
3+
<bubble>
4+
<diameter>10</diameter>
5+
<color red="0" green="127" />
6+
</bubble>
7+
<bubble>
8+
<diameter>30</diameter>
9+
<color red="127" green="0" />
10+
</bubble>
11+
<bubble>
12+
<diameter>50</diameter>
13+
<color red="127" green="127" />
14+
</bubble>
15+
<bubble>
16+
<diameter>70</diameter>
17+
<color red="255" green="127" />
18+
</bubble>
19+
<bubble>
20+
<diameter>90</diameter>
21+
<color red="255" green="200" />
22+
</bubble>
23+
</bubbles>

chapter_18/data/data-1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
102,75,192,178,78,23,144,181,125,55
1+
10,75,19,78,15,23,44,80,55,35

chapter_18/data/data-2.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
12.0, 39.0, 72.0
2-
18.0, 53.0, 69.0
3-
51.0, 24.0, 72.0
4-
0.0, 23.0, 72.0
5-
0.0, 25.0, 72.0
6-
8.0, 7.0, 72.0
7-
0.0, 0.0, 72.0
8-
14.0, 139.0, 72.0
9-
27.0, 106.0, 71.0
10-
103.0, 7.0, 72.0
1+
127, 255, 70
2+
127, 127, 60
3+
12, 100, 15
4+
255, 127, 90
5+
12, 9, 5
6+
0, 127, 40
7+
0, 255, 50
8+
127, 0, 25
9+
255, 1, 30
10+
23, 25, 35
2.44 KB
Binary file not shown.
113 KB
Binary file not shown.
4.61 KB
Binary file not shown.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/* Processing Yahoo! Search Library
2+
* Daniel Shiffman, 8/02/2007
3+
* Processing bridge to Yahoo! Search API
4+
* Requires the Yahoo! Search SDK from
5+
* http://developer.yahoo.com/download
6+
* code on Yahoo! API example.
7+
*/
8+
9+
package pyahoo;
10+
11+
import processing.core.*;
12+
13+
import java.io.IOException;
14+
import java.lang.reflect.*;
15+
import java.math.BigInteger;
16+
17+
import com.yahoo.search.SearchClient;
18+
import com.yahoo.search.SearchException;
19+
import com.yahoo.search.WebSearchRequest;
20+
import com.yahoo.search.WebSearchResult;
21+
import com.yahoo.search.WebSearchResults;
22+
23+
public class YahooSearch implements Runnable {
24+
25+
PApplet parent;
26+
String key;
27+
Method eventMethod;
28+
Thread runner;
29+
boolean available = false;
30+
String searchString;
31+
int numRequested = 10;
32+
SearchClient client;
33+
WebSearchResults results;
34+
boolean searching = false;
35+
36+
public YahooSearch(PApplet parent, String key) {
37+
client = new SearchClient(key); //Jm3V0PbV34GKpO58IjWbVvW26XjoKlrkriC2D4idXRBm8No3VDoCCjQLhBqsjJ9wRVI
38+
results = null;
39+
this.parent = parent;
40+
this.key = key;
41+
parent.registerDispose(this);
42+
try {
43+
eventMethod = parent.getClass().getMethod("searchEvent", new Class[] {
44+
YahooSearch.class }
45+
);
46+
}
47+
catch (Exception e) {
48+
//System.out.println("Hmmm, event method no go?");
49+
}
50+
}
51+
52+
public void search(String searchString_) {
53+
search(searchString_,10);
54+
}
55+
56+
public void search(String searchString_, int num) {
57+
if (!searching) {
58+
searching = true;
59+
numRequested = num;
60+
searchString = searchString_;
61+
runner = new Thread(this);
62+
runner.start();
63+
// This is maybe a bit hacky, but it'll make sure one search
64+
// doesn't overwrite another
65+
} else {
66+
YahooSearch ys = new YahooSearch(parent,key);
67+
ys.search(searchString_,num);
68+
}
69+
}
70+
71+
public boolean available() {
72+
return available;
73+
}
74+
75+
public WebSearchResults getResults() {
76+
return results;
77+
}
78+
79+
public String[] getTitles() {
80+
if (results == null) return null;
81+
WebSearchResult[] resultsArray = results.listResults();
82+
String[] titles = new String[resultsArray.length];
83+
for (int i = 0; i < titles.length; i++) {
84+
titles[i] = resultsArray[i].getTitle();
85+
}
86+
return titles;
87+
}
88+
89+
public String[] getUrls() {
90+
if (results == null) return null;
91+
WebSearchResult[] resultsArray = results.listResults();
92+
String[] urls = new String[resultsArray.length];
93+
for (int i = 0; i < urls.length; i++) {
94+
urls[i] = resultsArray[i].getUrl();
95+
}
96+
return urls;
97+
}
98+
99+
public String[] getSummaries() {
100+
if (results == null) return null;
101+
WebSearchResult[] resultsArray = results.listResults();
102+
String[] summaries = new String[resultsArray.length];
103+
for (int i = 0; i < summaries.length; i++) {
104+
summaries[i] = resultsArray[i].getSummary();
105+
}
106+
return summaries;
107+
}
108+
109+
public WebSearchResult[] getResultsArray() {
110+
if (results == null) return null;
111+
return results.listResults();
112+
}
113+
114+
public int getTotalResultsAvailable() {
115+
int total = 0;
116+
if (results != null) {
117+
BigInteger count = results.getTotalResultsAvailable();
118+
total = count.intValue();
119+
}
120+
return total;
121+
}
122+
123+
public void run() {
124+
try {
125+
available = false;
126+
WebSearchRequest request = new WebSearchRequest(searchString);
127+
System.out.println("Searching for " + searchString);
128+
request.setResults(numRequested);
129+
results = client.webSearch(request);
130+
available = true;
131+
if (eventMethod != null) {
132+
try {
133+
eventMethod.invoke(parent, new Object[] {
134+
this }
135+
);
136+
}
137+
catch (Exception e) {
138+
e.printStackTrace();
139+
eventMethod = null;
140+
}
141+
}
142+
searching = false;
143+
} catch (IOException e) {
144+
System.out.println("Error calling Yahoo! Search Service: " + e.toString());
145+
e.printStackTrace();
146+
} catch (SearchException e) {
147+
System.out.println("Error calling Yahoo! Search Service: " + e.toString());
148+
e.printStackTrace();
149+
}
150+
}
151+
152+
/**
153+
* Called by applets to stop.
154+
*/
155+
public void stop() {
156+
runner = null; // unwind the thread
157+
}
158+
159+
/**
160+
* Called by PApplet to shut down
161+
*/
162+
public void dispose() {
163+
stop();
164+
}
165+
166+
public String getSearchString() {
167+
return searchString;
168+
}
169+
170+
public int getNumberRequested() {
171+
return numRequested;
172+
}
173+
174+
}

0 commit comments

Comments
 (0)