<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Processing</title>
	<atom:link href="http://catehuston.com/workshop/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://catehuston.com/workshop</link>
	<description></description>
	<lastBuildDate>Thu, 22 Apr 2010 15:51:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Creating an Applet</title>
		<link>http://catehuston.com/workshop/?p=3</link>
		<comments>http://catehuston.com/workshop/?p=3#comments</comments>
		<pubDate>Thu, 01 Apr 2010 12:00:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[beginner]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[creating an applet]]></category>

		<guid isPermaLink="false">http://catehuston.com/workshop/?p=3</guid>
		<description><![CDATA[Processing can be downloaded here. This section assumes that it is already working on your computer. Open up Processing and you should see the &#8220;editor&#8221;, which looks like this: If we press the &#8220;play&#8221; (or &#8220;run&#8221;) button on the top left, we&#8217;ll get a little applet like the one below. Easy, right? Think of that [...]]]></description>
			<content:encoded><![CDATA[<p><em>Processing can be downloaded <a href="http://processing.org/download/">here</a>. This section assumes that it is already working on your computer.<br />
</em></p>
<p>Open up Processing and you should see the &#8220;editor&#8221;, which looks like this:</p>
<div id="attachment_15" class="wp-caption aligncenter" style="width: 260px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/editor-pic.png"><img class="size-medium wp-image-15" title="editor pic" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/editor-pic-250x300.png" alt="editor" width="250" height="300" /></a><p class="wp-caption-text">The Processing Editor</p></div>
<p>If we press the &#8220;play&#8221; (or &#8220;run&#8221;) button on the top left, we&#8217;ll get a little applet like the one below.</p>
<div id="attachment_16" class="wp-caption aligncenter" style="width: 137px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/first-applet.png"><img class="size-full wp-image-16" title="first applet" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/first-applet.png" alt="first applet" width="127" height="151" /></a><p class="wp-caption-text">Your First Applet</p></div>
<p>Easy, right? Think of that as our canvas. Now we can create.</p>
]]></content:encoded>
			<wfw:commentRss>http://catehuston.com/workshop/?feed=rss2&#038;p=3</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Size and Background</title>
		<link>http://catehuston.com/workshop/?p=4</link>
		<comments>http://catehuston.com/workshop/?p=4#comments</comments>
		<pubDate>Thu, 01 Apr 2010 11:30:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[beginner]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[background]]></category>
		<category><![CDATA[size]]></category>

		<guid isPermaLink="false">http://catehuston.com/workshop/?p=4</guid>
		<description><![CDATA[So we have out applet, but it&#8217;s a little dull so far &#8211; just a grey box. Let&#8217;s make it bigger, and more colorful. In the editor, add the following code: void setup() { size(500, 500); } This is the setup() method. The brackets group the code &#8211; in this section, everything you add should [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption aligncenter" style="width: 510px"><a href="http://www.flickr.com/photos/rebeccacowser/3913497067/"><img title="Lollipop Brush" src="http://farm3.static.flickr.com/2460/3913497067_6bf406ec14.jpg" alt="Lollipop Brush" width="500" height="332" /></a><p class="wp-caption-text">Credit: flickr / Toni_Chacheres</p></div>
<p>So we have out applet, but it&#8217;s a little dull so far &#8211; just a grey box.</p>
<p>Let&#8217;s make it bigger, and more colorful. In the editor, add the following code:</p>
<blockquote>
<pre>void setup() {
    size(500, 500);
}</pre>
</blockquote>
<p>This is the setup() method. The brackets group the code &#8211; in this section, everything you add should go <em>inside</em> the brackets.</p>
<p>Now you should have something that looks like this!</p>
<div id="attachment_18" class="wp-caption aligncenter" style="width: 297px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/big-applet.png"><img class="size-medium wp-image-18" title="big applet" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/big-applet-287x300.png" alt="big applet" width="287" height="300" /></a><p class="wp-caption-text">After changing the size</p></div>
<p>However, the grey is a little dull. So let&#8217;s change the background color! Add the following (inside the brackets) to your setup() method:</p>
<blockquote>
<pre>background(0);</pre>
</blockquote>
<p>Then try this:</p>
<blockquote>
<pre>background(255);</pre>
</blockquote>
<p>What happens? What if we choose a value in between 0 and 255?</p>
<p>The number in the brackets denotes the greyscale. But wouldn&#8217;t it be nice if we had something more colorful? Try this:</p>
<blockquote><p>background(236, 0 , 128);</p></blockquote>
<p>What does that do? Try changing the three values (between 0 and 255) and see what happens.</p>
<p>These are &#8220;RGB&#8221; values &#8211; Red, Blue and Green. It&#8217;s like mixing colors. The first value determines how much red, the second blue, and the third green.</p>
]]></content:encoded>
			<wfw:commentRss>http://catehuston.com/workshop/?feed=rss2&#038;p=4</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drawing Shapes</title>
		<link>http://catehuston.com/workshop/?p=5</link>
		<comments>http://catehuston.com/workshop/?p=5#comments</comments>
		<pubDate>Thu, 01 Apr 2010 11:00:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[beginner]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[drawing]]></category>
		<category><![CDATA[shapes]]></category>

		<guid isPermaLink="false">http://catehuston.com/workshop/?p=5</guid>
		<description><![CDATA[Add one more line to your setup() method (you&#8217;ll see why later): noLoop(); Now, we&#8217;re going to make a draw() method. Just like when we made the setup() method, we have the following: void draw() { } Now, let&#8217;s draw something! How about a square. rect(50, 50, 400, 400); You should have something that looks [...]]]></description>
			<content:encoded><![CDATA[<p>Add one more line to your setup() method (you&#8217;ll see why later):</p>
<blockquote>
<pre>noLoop();</pre>
</blockquote>
<p>Now, we&#8217;re going to make a draw() method. Just like when we made the setup() method, we have the following:</p>
<blockquote>
<pre>void draw() {

}</pre>
</blockquote>
<p>Now, let&#8217;s draw something! How about a square.</p>
<blockquote>
<pre>rect(50, 50, 400, 400);</pre>
</blockquote>
<p>You should have something that looks like this:</p>
<div id="attachment_21" class="wp-caption aligncenter" style="width: 509px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/draw-square.png"><img class="size-full wp-image-21" title="draw square" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/draw-square.png" alt="draw square" width="499" height="523" /></a><p class="wp-caption-text">Drawing a square</p></div>
<p>See how the square is white but has a black outline? We can change that really easily. Before the line you just added, insert this:</p>
<blockquote>
<pre>fill(255, 126, 0);
stroke(238);</pre>
</blockquote>
<p>Now your square should look like this:</p>
<div id="attachment_22" class="wp-caption aligncenter" style="width: 509px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/colorful-square.png"><img class="size-full wp-image-22" title="colorful square" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/colorful-square.png" alt="colorful square" width="499" height="521" /></a><p class="wp-caption-text">Colorful square</p></div>
<p>fill() and stroke() work like background() did &#8211; if we have one value in the brackets, it&#8217;s gray-scale. If we have two, it&#8217;s RGB. Trying playing until you&#8217;re happy with the colors! stroke() controls the color of the line, fill() controls the color inside.</p>
<p>What about the numbers we use for rect()? What do they do? Try changing those up as well.</p>
<p>We had four numbers for rect(), the first two are the x and y positions of the top left corner. The third is the width, and the fourth is the height.</p>
<p>What other shapes can we have?</p>
<ul>
<li>
<pre>point(x, y)</pre>
<p>(<a href="http://processing.org/reference/point_.html">detail</a>)</li>
<li>
<pre>quad(x1, y1, x2, y2, x3, y3, x4, y4)</pre>
<p>(<a href="http://processing.org/reference/quad_.html">detail</a>)</li>
<li>
<pre>triangle(x1, y1, x2, y2, x3, y3)</pre>
<p>(<a href="http://processing.org/reference/triangle_.html">detail</a>)</li>
<li>
<pre>arc(x, y, width, height, start, stop)</pre>
<p>(<a href="http://processing.org/reference/arc_.html">detail</a>)</li>
<li>
<pre>ellipse(x, y, width, height)</pre>
<p>(<a href="http://processing.org/reference/ellipse_.html">detail</a>)</li>
<li>
<pre>line(x1, y1, x2, y2)</pre>
<p>(<a href="http://processing.org/reference/line_.html">detail</a>)</li>
</ul>
<p>You don&#8217;t have to try all of them, but at least try line() and ellipse(). You&#8217;ll notice there&#8217;s something different about the ellipse &#8211; x and y are not the top left corner, but the center.</p>
]]></content:encoded>
			<wfw:commentRss>http://catehuston.com/workshop/?feed=rss2&#038;p=5</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Saving Things for Later</title>
		<link>http://catehuston.com/workshop/?p=6</link>
		<comments>http://catehuston.com/workshop/?p=6#comments</comments>
		<pubDate>Thu, 01 Apr 2010 10:30:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://catehuston.com/workshop/?p=6</guid>
		<description><![CDATA[So far, we&#8217;ve found ourselves using the same numbers again and again. Wouldn&#8217;t it be easier if we could save them for later? We can! And it&#8217;s easy. Above your setup() and draw() methods, add the following: int width = 500; int height = 500; int border = 50; Now, change the rect() line in [...]]]></description>
			<content:encoded><![CDATA[<p>So far, we&#8217;ve found ourselves using the same numbers again and again. Wouldn&#8217;t it be easier if we could save them for later?</p>
<p>We can! And it&#8217;s easy. Above your setup() and draw() methods, add the following:</p>
<blockquote>
<pre>int width = 500;
int height = 500;
int border = 50;</pre>
</blockquote>
<p>Now, change the rect() line in your draw() method so:</p>
<blockquote>
<pre>rect(border, border, width-2*border, height-2*border);</pre>
</blockquote>
<p>Now, what happens when we change width and height at the top?</p>
<p>We can also save things inside our function if we&#8217;re going to use them more than once. See here we are repeating &#8220;2*border&#8221; twice? How about we change that.</p>
<blockquote>
<pre>int b = 2*border;
rect(border, border, width-b, height-b);</pre>
</blockquote>
<p>This shouldn&#8217;t have changed the resulting applet at all, it&#8217;s just made it easier for us to do stuff because we don&#8217;t have to repeat so much. And, if we want to change it and say, make our applet wider than it is tall, we just have to change two things. First, the initial call to size(), and the width and height <strong>variables</strong> at the top to match.</p>
<blockquote>
<pre>int width = 750;
int height = 400;
int border = 50;

void setup() {
   size(750, 400);
   background(236, 0 , 128);
   noLoop();
}

void draw() {
  fill(255, 126, 0);
  stroke(238);
  int b = 2*border;
  rect(border, border, width-b, height-b);
}</pre>
</blockquote>
<p style="text-align: center;">
<div id="attachment_24" class="wp-caption aligncenter" style="width: 609px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/wide-view.png"><img class="size-full wp-image-24 " title="Wide Applet" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/wide-view.png" alt="Wide Applet" width="599" height="338" /></a><p class="wp-caption-text">Making it wider</p></div>
]]></content:encoded>
			<wfw:commentRss>http://catehuston.com/workshop/?feed=rss2&#038;p=6</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making the Image Change</title>
		<link>http://catehuston.com/workshop/?p=7</link>
		<comments>http://catehuston.com/workshop/?p=7#comments</comments>
		<pubDate>Thu, 01 Apr 2010 10:00:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[draw]]></category>
		<category><![CDATA[framerate]]></category>

		<guid isPermaLink="false">http://catehuston.com/workshop/?p=7</guid>
		<description><![CDATA[Try this: int width = 500; int height = 500; int x = 0; int y = 0; int s = 10; void setup() { size(500, 500); background(236, 0 , 128); frameRate(5); } void draw() { fill(255, 126, 0); stroke(238); rect(x*s, y*s, s, s); x++; y++; } What&#8217;s changed? Some extra variables at the top [...]]]></description>
			<content:encoded><![CDATA[<p>Try this:</p>
<blockquote>
<pre>int width = 500;
int height = 500;

int x = 0;
int y = 0;
int s = 10;

void setup() {
   size(500, 500);
   background(236, 0 , 128);
   frameRate(5);
}

void draw() {
  fill(255, 126, 0);
  stroke(238);

  rect(x*s, y*s, s, s);
  x++;
  y++;
}</pre>
</blockquote>
<p>What&#8217;s changed?</p>
<ol>
<li>Some extra variables at the top &#8211; x, y and size. We have them there so we can update them in our draw() method.</li>
<li>noLoop() is gone and has been replaced by frameRate(5). noLoop() means the draw method is called only once, frameRate() controls how many times a second it is called. Try changing the number and see what happens.</li>
<li>We drawing a smaller square &#8211; the size is specified by the variable s.</li>
<li>At the end of draw(), we change x and y so that we draw the <em>next</em> square in a different place. The ++ means increase the value by one.</li>
</ol>
<p>The variables at the top are the <strong>state</strong> of our applet. The state can change over time, so that the image changes too.</p>
<p>Try it!</p>
<div id="attachment_26" class="wp-caption aligncenter" style="width: 510px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/changing-the-image.png"><img class="size-full wp-image-26" title="changing the image" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/changing-the-image.png" alt="Changing the Image" width="500" height="523" /></a><p class="wp-caption-text">Changing the Image</p></div>
]]></content:encoded>
			<wfw:commentRss>http://catehuston.com/workshop/?feed=rss2&#038;p=7</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sometimes Do This, Sometimes Do That</title>
		<link>http://catehuston.com/workshop/?p=12</link>
		<comments>http://catehuston.com/workshop/?p=12#comments</comments>
		<pubDate>Thu, 01 Apr 2010 09:30:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[if statements]]></category>

		<guid isPermaLink="false">http://catehuston.com/workshop/?p=12</guid>
		<description><![CDATA[So we have our image changing, but all the squares are the same color. Let&#8217;s switch that up! If your friend was going to get you a soda, you might say &#8211; get me Cherry Coke if they have it, if not I&#8217;ll have a Dr Pepper. The way we do that is by using [...]]]></description>
			<content:encoded><![CDATA[<p>So we have our image changing, but all the squares are the same color. Let&#8217;s switch that up!</p>
<p>If your friend was going to get you a soda, you might say &#8211; get me Cherry Coke if they have it, if not I&#8217;ll have a Dr Pepper.</p>
<p>The way we do that is by using <strong>if-statements</strong>. So it&#8217;s like,</p>
<blockquote>
<pre>if(cherrycoke) {
    buy(cherrycoke);
}

else {
    buy(drpepper);
}
</pre>
</blockquote>
<p>We can compare numbers using these operators:</p>
<ul>
<li>
<pre>a &lt; b</pre>
<p>a is <strong>less than</strong> b</li>
<li>
<pre>a &lt;= b</pre>
<p>a is less <strong>than or equal to</strong> b</li>
<li>
<pre>a &gt; b</pre>
<p>a is <strong>greater than</strong> b</li>
<li>
<pre>a &gt;= b</pre>
<p>a is <strong>greater than or equal to</strong> b</li>
<li>
<pre>a == b</pre>
<p>a is <strong>equal to</strong> b</li>
</ul>
<p>So how about we change our code to fill the applet with squares, alternating in color.</p>
<blockquote>
<pre>  stroke(140, 9, 214);

  if (y%2 == 0) {
      if (x%2 == 0) {
        fill(255, 126, 0);
      }
      else {
        fill(238);
      }
  }
  else {
    if (x%2 == 0) {
        fill(238);
      }
      else {
        fill(255, 126, 0);
      }
  }</pre>
</blockquote>
<p>Using x%2 takes &#8220;mod 2&#8243; of x. <a href="http://en.wikipedia.org/wiki/Modular_arithmetic">Wikipedia has a full explanation</a>, but here we&#8217;re using it to determine whether x is even or odd.</p>
<p>So we&#8217;ve set the color. Now, we need to draw the square and update x and y. So:</p>
<blockquote>
<pre>rect(x*s, y*s, s, s);
x++;  

if (x &gt;= width/s) {
  y++;
  x = 0;
}</pre>
</blockquote>
<p>And now, it update until it looks like this:</p>
<div id="attachment_28" class="wp-caption aligncenter" style="width: 510px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/grid.png"><img class="size-full wp-image-28" title="grid" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/grid.png" alt="Grid" width="500" height="523" /></a><p class="wp-caption-text">Alternating Colors!</p></div>
<p>Finally, add the lines below: can you work out what they do?</p>
<blockquote>
<pre>  if (y &gt;= width/s) {
     noLoop();
  }</pre>
</blockquote>
<p>This says we&#8217;ve come to the end of our program, so we don&#8217;t need to call draw() again.</p>
]]></content:encoded>
			<wfw:commentRss>http://catehuston.com/workshop/?feed=rss2&#038;p=12</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Repeating Things</title>
		<link>http://catehuston.com/workshop/?p=8</link>
		<comments>http://catehuston.com/workshop/?p=8#comments</comments>
		<pubDate>Thu, 01 Apr 2010 09:00:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[loops]]></category>

		<guid isPermaLink="false">http://catehuston.com/workshop/?p=8</guid>
		<description><![CDATA[We repeat things using loops. There are multiple kinds of loops, but one of the most useful is the for-loop. We use this when we want to do something a certain number of times. It looks like this: for(int i = 0; i &#60; max; i++) { } Whatever goes in the brackets gets repeated [...]]]></description>
			<content:encoded><![CDATA[<p>We repeat things using <strong>loops</strong>. There are multiple kinds of loops, but one of the most useful is the <strong>for-loop</strong>. We use this when we want to do something a certain number of times. It looks like this:</p>
<blockquote>
<pre>for(int i = 0; i &lt; max; i++) {
}</pre>
</blockquote>
<p>Whatever goes in the brackets gets repeated <em>max</em> number of times.</p>
<p>We can use this do draw a circles inside one another like this:</p>
<div id="attachment_30" class="wp-caption aligncenter" style="width: 510px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/circles.png"><img class="size-full wp-image-30" title="circles" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/circles.png" alt="Repeating Circles" width="500" height="522" /></a><p class="wp-caption-text">Repeating Circles</p></div>
<p>We can create this using the code below:</p>
<blockquote>
<pre>int width = 500;
int height = 500;

void setup() {
   size(500, 500);
   background(236, 0 , 128);
   noLoop();
}

void draw() {
  stroke(140, 9, 214);
  int inc = 50;
  int radius = width - inc;
  int n = radius/inc;
  int x = width/2;
  int y = width/2;

 for(int i = 0; i &lt; n; i++) {
    if (i%2 == 0) {
      fill(255, 126, 0);
     }
     else {
       fill(238);
     }

     ellipse(x, y, radius, radius);
     radius = radius - inc;
  }
}</pre>
</blockquote>
<p>Let&#8217;s look at what&#8217;s happening here. We&#8217;re drawing the outermost circle first, this is because things we draw later are drawn over the top of things we drew before. So we start with the outermost circle and draw the rest of them inside it, getting progressively smaller.</p>
<p>We have a lot of numbers that we&#8217;re using again and again, but they all depend on the size of the applet (which is declared at the top &#8211; width and height) and the increment (inc) which is the space between each circle, so we can calculate them from that. Writing math in Processing is much the same as when you do your math homework &#8211; except you don&#8217;t need to work out the answer yourself!</p>
<p>The initial radius is the width less that space. The number of times we&#8217;ll repeat the loop (n) is the number of times we can reduce the radius by inc and still have a circle to draw. x and y are the centre point of the applet.</p>
<p>For each <em>iteration</em> of the loop, we calculate the color (as we did before), draw the circle, and reduce the radius for the next iteration.</p>
]]></content:encoded>
			<wfw:commentRss>http://catehuston.com/workshop/?feed=rss2&#038;p=8</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Responding to Mouse Clicks</title>
		<link>http://catehuston.com/workshop/?p=9</link>
		<comments>http://catehuston.com/workshop/?p=9#comments</comments>
		<pubDate>Thu, 01 Apr 2010 08:30:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[advanced]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[onclick]]></category>

		<guid isPermaLink="false">http://catehuston.com/workshop/?p=9</guid>
		<description><![CDATA[So far, our applets aren&#8217;t very interactive. But it&#8217;s really easy to add response to mouse clicks. Make a new applet with a plain background. Our setup() method will be the same as usual, and we&#8217;ll want the variables for width, height, and a third one &#8211; a radius. Make sure there&#8217;s no noLoop() &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>So far, our applets aren&#8217;t very <em>interactive</em>. But it&#8217;s really easy to add response to mouse clicks.</p>
<p>Make a new applet with a plain background. Our setup() method will be the same as usual, and we&#8217;ll want the variables for width, height, and a third one &#8211; a radius. Make sure there&#8217;s no noLoop() &#8211; that will prevent the applet from working the way we want it to.</p>
<p>Create the draw method() too, but don&#8217;t put anything between the { and }.</p>
<p>Now, we&#8217;ll make a third method, which tells the applet that we&#8217;re going to do something when the user clicks.</p>
<blockquote>
<pre>void mouseClicked() {
  stroke(238);
  fill(255, 126, 0);

  ellipse(mouseX, mouseY, radius, radius);
}</pre>
</blockquote>
<p>Easy, right? You can make something else happen if you like. Notice how we never declared mouseX and mouseY? Processing takes care of that for us. They hold the current X and Y position of the mouse, respectively.</p>
<div id="attachment_32" class="wp-caption aligncenter" style="width: 509px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/mouseclicks.png"><img class="size-full wp-image-32" title="mouseclicks" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/mouseclicks.png" alt="Responding to mouse clicks" width="499" height="522" /></a><p class="wp-caption-text">Responding to mouse clicks</p></div>
]]></content:encoded>
			<wfw:commentRss>http://catehuston.com/workshop/?feed=rss2&#038;p=9</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Responding to Keyboard Input</title>
		<link>http://catehuston.com/workshop/?p=10</link>
		<comments>http://catehuston.com/workshop/?p=10#comments</comments>
		<pubDate>Thu, 01 Apr 2010 08:00:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[advanced]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[keyboard input]]></category>

		<guid isPermaLink="false">http://catehuston.com/workshop/?p=10</guid>
		<description><![CDATA[Try the following code: int width = 500; int height = 500; int x; int y; int size = 20; void setup() { size(500, 500); background(236, 0 , 128); x = width/2; y = height/2; stroke(255, 126, 0); fill(238); rect(x, y, size, size); } void draw() { } void keyPressed() { if (key == CODED) [...]]]></description>
			<content:encoded><![CDATA[<p>Try the following code:</p>
<blockquote>
<pre>int width = 500;
int height = 500;
int x;
int y;
int size = 20;

void setup() {
   size(500, 500);
   background(236, 0 , 128);
   x = width/2;
   y = height/2;
   stroke(255, 126, 0);
   fill(238);
   rect(x, y, size, size);
}

void draw() {

}

void keyPressed() {
   if (key == CODED) {
     if (keyCode == UP) {
       y = y - size;
     }
     else if (keyCode == DOWN) {
       y = y + size;
     }
     else if (keyCode == RIGHT) {
        x = x + size;
     }
     else if (keyCode == LEFT) {
        x = x - size;
     }

     background(236, 0 , 128);
     stroke(255, 126, 0);
     fill(238);
     rect(x, y, size, size);
   }
}</pre>
</blockquote>
<p>So, what are we doing here? In order to respond to keyboard input we have to have a method keyPressed().</p>
<p>if (key == CODED) determines whether it&#8217;s a <em>special</em> key &#8211; up, down, left, right, shift, alt and control. We update our <strong>x</strong> and <strong>y</strong> variables, declared at the top, accordingly and redraw our square. Thus, our square moves about in response to what we&#8217;re doing on the keyboard. (<a href="http://processing.org/reference/keyCode.html">Full detail</a>).</p>
<p>Each time, we call background() again, what happens if you remove that line?</p>
<div id="attachment_34" class="wp-caption aligncenter" style="width: 507px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/Screen-shot-2010-04-05-at-8.28.04-PM.png"><img class="size-full wp-image-34" title="Responding to keyboard input" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/Screen-shot-2010-04-05-at-8.28.04-PM.png" alt="Responding to keyboard input" width="497" height="522" /></a><p class="wp-caption-text">Responding to keyboard input</p></div>
<p>There&#8217;s a potential problem here though, can you spot it? If we go too far left, right, up or down our square can leave the screen. Can you fix that?</p>
]]></content:encoded>
			<wfw:commentRss>http://catehuston.com/workshop/?feed=rss2&#038;p=10</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Saving Lots of Values</title>
		<link>http://catehuston.com/workshop/?p=11</link>
		<comments>http://catehuston.com/workshop/?p=11#comments</comments>
		<pubDate>Thu, 01 Apr 2010 07:30:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[advanced]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[arrays]]></category>

		<guid isPermaLink="false">http://catehuston.com/workshop/?p=11</guid>
		<description><![CDATA[Sometimes, we want to keep track of a lot of values, more than we can using individual variable names. We do this using arrays. Here, we&#8217;ll use two arrays to create a scrolling background effect. We declare an array like this: int[] name = new int[size]; Here, the &#8220;int&#8221; means we&#8217;re storing a whole number, [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, we want to keep track of a lot of values, more than we can using individual variable names.</p>
<p>We do this using <strong>arrays</strong>. Here, we&#8217;ll use two arrays to create a scrolling background effect.</p>
<p>We declare an array like this:</p>
<blockquote>
<pre>int[] name = new int[size];</pre>
</blockquote>
<p>Here, the &#8220;int&#8221; means we&#8217;re storing a whole number, and the &#8220;[]&#8221; mean it&#8217;s an array of them. &#8220;size&#8221; is how many we want to store.</p>
<p>We&#8217;re also going to introduce two other things here. The first is the <strong>comment</strong>. Comments are like notes we write for ourselves, Processing ignores them. Comments start with &#8220;//&#8221;.</p>
<p>The second is making random numbers. This is easy,</p>
<blockquote>
<pre>int randnum = (int) random(max);</pre>
</blockquote>
<p>Here, the &#8220;(int)&#8221; means we want a whole number back. The &#8220;max&#8221; in the brackets says we want all the numbers returned to be less than that value.</p>
<p>Here&#8217;s the whole program, with comments.</p>
<blockquote>
<pre>int width = 500;
int height = 500;
int n = 200; // number of items
int diff = 2; // how much the points should move

int[] pts_x;
int[] pts_y;

void setup() {
   size(500,500);

   // create the two arrays
   pts_x = new int[n];
   pts_y = new int[n];

   // fill them with random numbers
   for(int i = 0; i &lt; n; i++) {
      pts_x[i] = (int) random(width);
      pts_y[i] = (int) random(height);
   }
}

void draw() {
   background(64);
   stroke(238);

   for(int i = 0; i &lt; n; i++) {
      point(pts_x[i], pts_y[i]);

      // move each point down the screen
      pts_y[i] = pts_y[i] + diff;

      // when one leaves the screen, make a new ptsdop
      if (pts_y[i] &gt; height) {
        pts_y[i] = 0;
        pts_x[i] = (int) random(width);
     }
   }
}</pre>
</blockquote>
<p>What does it look like? What if we have more (or fewer) points? What if the difference is bigger, or smaller?</p>
<div id="attachment_15" class="wp-caption aligncenter" style="width: 509px"><a href="http://catehuston.com/workshop/wp-content/uploads/2010/04/moving-background.png"><img class="aligncenter size-full wp-image-36" title="moving background" src="http://catehuston.com/workshop/wp-content/uploads/2010/04/moving-background.png" alt="" width="499" height="522" /></a><p class="wp-caption-text">Moving Background</p></div>
<p>Experiment! Can you make the points change color too?</p>
]]></content:encoded>
			<wfw:commentRss>http://catehuston.com/workshop/?feed=rss2&#038;p=11</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

