Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Building code blocks

From: Interactive Data Visualization with Processing

Video: Building code blocks

One of the tools you have for arranging your code and Processing is putting it into blocks. Now most dynamic drawings actually include two blocks every time and that is what's called the setup and the draw and you've seen me do those a number of times. In this sketch I am going to show you a few other kinds of blocks that you can use and how they work in Processing. I'll begin by putting a comment up here at the top with my sketch name. Then I am going to insert a color palette. Now I've decided to save a little bit a time and I've included a text file that has all the color palettes on it and I am going to be using this one right here, styleyou.

Building code blocks

One of the tools you have for arranging your code and Processing is putting it into blocks. Now most dynamic drawings actually include two blocks every time and that is what's called the setup and the draw and you've seen me do those a number of times. In this sketch I am going to show you a few other kinds of blocks that you can use and how they work in Processing. I'll begin by putting a comment up here at the top with my sketch name. Then I am going to insert a color palette. Now I've decided to save a little bit a time and I've included a text file that has all the color palettes on it and I am going to be using this one right here, styleyou.

I want to copy that and paste it right here into my sketch. Then I am going to define two variables that I'll be using in this the sketch. One is an incrementing variable and another one is of X coordinate variable and now that I've created these two global variables, I can go down to the first block of code that we will be using. Traditionally, that is void setup. And let me explain what's going on with this particular block. The void is the return type for the function, setup is the name of the function, and every function has a return type that is if it were to calculate a floating-point number, it would say float setup.

If it would were to create a Boolean value, it'd say Boolean setup. However the setup actually doesn't return a value at all, but you have to have something. You have to specify return type. So you say to void. It doesn't return anything. That explains a curious term and also the empty parentheses (), because every function needs to have a place where you could at least potentially put arguments. The setup doesn't have any arguments, but you do need the parentheses () there because it's a function. Instead, all the information we're going to do goes in the curly brackets that indicate a block of code in this case.

So what I am going to do is I am going to start inserting some code in here. This is where I put the size. This is where I can put the anti-aliasing to where I can put the stroke. Actually, I think for the stroke I'll use the palette and I'll use the 4th one and I can put the strokeWeight. In this case, I will use 5 pixels. So that completes a block of code. Now I didn't create any variables in there. If I had, they would have been local variables that applied only to that block of code or their values would've applied only to that.

Next I am going to go to the second standard block of code in most Processing sketches and that's the draw. So I go void draw, and again it has void because it's a function, but it doesn't return a value. So the void means there's no value to return. The open and close parentheses (), that's the space that's mandatory to allow parameters to be input, but we don't have a parameters for this, but we have to have that there as a marker. Then I do the curly brackets, the open, and then I am going to bump down to closing curly brackets. Then inside there I can put information about the background color and what I am going to do here for the background, I am going to do palette put, the 0 color.

Then I am going to fill in. Then I'm going to change to the letter i and that refers to the global variable that are defined up above. And the reason I put it up there, because I am going to be using that variable in more than one block of code. So it has to be outside of the code where things can get to it. Then I am going to draw a small rectangle. I am going to start it at x which I defined as 0 earlier up above, 75 pixels down, and then I'll make it 50 pixels wide and 50 pixels tall. Now if I'd simply go like this, I can hit draw, you can see I've got a pretty shocking rectangle there, but that's the basic idea.

Now what I want to show you is that Processing entry actually allows for several other blocks of code. I'm going to use two in this particular one. The one I'm going to use is what's called keyPressed. Now with keyPressed and with a similar one mousePressed, those can be used as functions within the draw block or they could be put as separate functions out here. They work little differently depending on this, too. But I am going to have this one out here. Again it's void, because it doesn't really return a dataType. I am going to put the curly brackets and what I can say is when I press the key, I want x, the location for the rectangle, to be incremented 3 pixels.

So each time I press the key x will be incremented 3 pixels. So I can press that press run and now I can just press a key, I'm pressing the Spacebar, and it's moving over. If I hold it, it moves faster. But I am going to add just one another block of code to show what can be done. I am going to void mousePressed, and again it doesn't have a dataReturnType, so I put void it doesn't have any arguments there. So I have the empty parentheses () and then I put the curly brackets where I am going to put my code.

What I am going to do is each time I press the mouse here, I am going to take the variable i, which I earlier used for the palette and I am going to have i take on a random value. I am into random (1, 5) from the palette. Now remember that the way random works is that the bottom limit is an inclusive so 1.00 is an option, but the top limit is exclusive. So it never goes to 5, it goes to 4.99 and when you take the random variable and you cast it into an integer, it just chops off the digits.

So you're going to get 1, 2, 3, or 4 from this. Now I can run this. I'll save it and I'll run it. If I hold down the Space key, it'll move over and if I hit the mouse, the color of the box changes. And it's a silly kind of interaction, but it lets you know that these different blocks of code function in a modular sense, and especially if you want to be able to use the same variable in different codes, and it needs to be declared as a global variable so more than one block can get at it.

And that's how we can organize some of the code in these sketches.

Show transcript

This video is part of

Image for Interactive Data Visualization with Processing
Interactive Data Visualization with Processing

72 video lessons · 13962 viewers

Barton Poulson
Author

 
Expand all | Collapse all
  1. 3m 16s
    1. Welcome
      58s
    2. What you should know
      1m 22s
    3. Using the exercise files
      56s
  2. 11m 51s
    1. Overview of data visualization
      11m 51s
  3. 11m 53s
    1. Installing Processing
      3m 38s
    2. Overview of Processing
      4m 5s
    3. Exploring libraries
      4m 10s
  4. 1h 1m
    1. Basic setup
      7m 31s
    2. Drawing points
      4m 37s
    3. Drawing lines
      5m 6s
    4. Drawing ellipses and circles
      5m 24s
    5. Drawing arcs
      6m 54s
    6. Drawing rectangles and squares
      4m 58s
    7. Drawing quadrangles
      3m 25s
    8. Drawing triangles
      2m 55s
    9. Drawing polygons
      3m 37s
    10. Drawing simple curves
      4m 54s
    11. Drawing complex curves
      6m 46s
    12. Drawing Bézier curves
      5m 38s
  5. 54m 3s
    1. Introduction to variables
      10m 44s
    2. Understanding variable scope
      6m 53s
    3. Modifying variables
      9m 8s
    4. Creating arrays
      9m 53s
    5. Modifying arrays
      6m 37s
    6. Creating strings
      7m 3s
    7. Modifying strings
      3m 45s
  6. 1h 2m
    1. Incorporating randomness
      7m 59s
    2. Using Perlin noise
      4m 24s
    3. Shuffling with Java
      3m 30s
    4. Specifying line attributes
      8m 2s
    5. Changing placement modes
      5m 45s
    6. Understanding color attributes and functions
      4m 16s
    7. Exploring color spaces
      7m 44s
    8. Using color palettes
      7m 5s
    9. Transforming the grid
      8m 38s
    10. Exploring the attribute matrix
      5m 33s
  7. 52m 7s
    1. Building code blocks
      5m 57s
    2. Writing a while loop
      3m 52s
    3. Using for loops
      5m 35s
    4. Creating conditionals
      14m 50s
    5. Working with easing
      10m 51s
    6. Creating spirals
      11m 2s
  8. 18m 55s
    1. Mouse tracking
      3m 54s
    2. Hovering and clicking
      11m 16s
    3. Understanding keyboard interaction
      3m 45s
  9. 27m 32s
    1. Specifying fonts
      6m 43s
    2. Using images
      5m 51s
    3. Playing a video loop
      6m 20s
    4. Exporting video
      3m 47s
    5. Adding sound
      4m 51s
  10. 20m 49s
    1. Creating functions
      11m 48s
    2. Creating classes and objects
      9m 1s
  11. 31m 10s
    1. Using embedded data
      5m 26s
    2. Working with appended text data
      6m 4s
    3. Working with appended tabular data
      10m 26s
    4. Reading XML data
      9m 14s
  12. 48m 17s
    1. Generating dot plots
      11m 11s
    2. Building scatter plots
      10m 0s
    3. Making line plots
      9m 55s
    4. Creating bar charts
      9m 12s
    5. Checking out examples of maps, hierarchies, and networks
      7m 59s
  13. 20m 57s
    1. Introducing some principles of 2D design
      13m 44s
    2. Understanding color theory
      7m 13s
  14. 24m 46s
    1. Interacting with zooming, rotating, and sliding
      6m 26s
    2. Implementing slicing
      6m 47s
    3. Using rollovers
      5m 58s
    4. Introducing the GUI libraries
      5m 35s
  15. 10m 35s
    1. Sharing via OpenProcessing and other sites
      3m 19s
    2. Saving as a desktop application
      2m 42s
    3. Saving as JavaScript
      1m 47s
    4. Saving as an Android application
      2m 47s
  16. 2m 38s
    1. Where to go from here
      2m 38s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Interactive Data Visualization with Processing.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.