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

Creating classes and objects

From: Interactive Data Visualization with Processing

Video: Creating classes and objects

In the previous movie, we discussed how you could use custom functions, or blocks of code, to save time and to allow you to reuse code in later sketches. In this movie, I want to take that several steps further, by explaining to you classes and objects. Now, a function is usually something that you use in a static drawing, and every piece of information that you enter into it has to be of the same type, so all integer variables or all floats. In a class, however, you can throw in any kind of information you want, and more significantly, you can specify the operations that occur, the things that the drawing actually does with the class.

Creating classes and objects

In the previous movie, we discussed how you could use custom functions, or blocks of code, to save time and to allow you to reuse code in later sketches. In this movie, I want to take that several steps further, by explaining to you classes and objects. Now, a function is usually something that you use in a static drawing, and every piece of information that you enter into it has to be of the same type, so all integer variables or all floats. In a class, however, you can throw in any kind of information you want, and more significantly, you can specify the operations that occur, the things that the drawing actually does with the class.

Now, a class is a template. It's a general category. And then when you instantiate something in a class, it becomes an object. So, you have a template and the realization. That's--the class is the template and the object is the realization of that class in your particular sketch. In this example, I'm not going to type the code in front of you, because there's a fair amount of code. I am, however, going to open it up and walk you through it. So here, in the Exercise folder, in Exercise 09_02, I actually have two files here, and the reason for that is because I created a class and I've put it on a separate tab.

And when you have more than one tab, each of those tabs gets its own folder in Processing. I'm going to double-click on this one right here and maximize that one. I decided for this example to create a pie chart that actually had a fair amount of random generation to it, a couple of user-specified parameters, and that also, a pie chart that actually grew over time. What I'm doing in this case is I'm actually creating five pie charts. Because it's modular, you can do things more than once now. Five is actually really pushing it for coding it in by hand.

Normally, by this point, I would be using an array of objects, which is actually a very good way to go. But I just wanted to emphasize the structure here for this one. So, I created an object called PieGrow, because it's a pie chart that grows. And the first thing I needed to do is I needed to declare both the class and the name of the object. So, what I have at the top are five statements of the class PieGrow, and then I have five objects: the pie1, pie2, and so on. So, that's like declaring a variable, except because it's a class and an object, it's a much more elaborate thing.

Beneath that, I have my setup where I do a 600 x 200 window and turn on the anti-aliasing. And beneath that is where I actually specify parameters. These are the ones that the user gets to specify. So, I put down, for each one of them, I put pie1-- that's the name of the objects that I created above--then equals and then new. That's something you have to do with objects. And then I put the name of the class PieGrow, and then in parentheses, that's where I put the two user-specified parameters. The first one is the name of the pie chart, and I just put them A through E.

And then the second one is how many radians around do you want the pie to go? And remember, it starts at 3 o'clock. And one radian is over to 9 o'clock, and two radians is all the way around. So, for instance, I have one here at 0.75. That's going to be less than halfway. But the fourth one down is 1.95. That's going to be a nearly complete circle. Anyhow, the nice thing is I don't need a whole lot of code here. That's all I have in the setup. And then I come down to draw and I put the background in, and then for each of my pies, I just have two behaviors, or two functions that each one of them do, and that is they update--that's to change the calculations--and then the display it--that is actually draw it--and I do each one of those things in turn.

And the Processing sketch will cycle through this, up to 60 frames per second, and see how it can adapt. Now, I did this so you can see how clean this can be when you use objects that you really don't have a lot of code in the drawing here per se. Instead, I have a fair amount of code here under PieGrow. That's the name of the class. So, at the top, it says class--that's all lowercase-- and then PieGrow. Class names actually start with capital letters. And then in the curly brackets, I put down all the variables that go into my pie chart.

There is the x and the y that position it. There is the d for the diameter. There is the angle for the start. That's where the slice of the pie chart begins. And then I have a angle for the middle as it goes around, and then I have an angle at the end where it stops. And the speed is how fast I want it to move. And then I actually have RGB for color. And then I have the string for the title that the user provides. Next, you have something called a constructor. And again, this is kind of a funny bit of logic, but what it is is this is something that takes the arguments that the user's provided, and it puts them in here to initialize the variables.

What's curious is that it has to actually use temporary variables for the initialization of anything the users provide. Now, if it's not based on a user-provided parameter, he don't need to use the temp stuff. So, you see, for instance, at the beginning, I have three variables here that are based on random numbers. The diameter of the pie is a random number between 50 to 250, so that's the diameter in pixels, and then the X and the Y coordinates for the center of the pie, those are also random coordinates. I did diameter first because I want to keep this from going completely off the window.

This will keep them on a little bit. Then the angle for the start is always going to be 0, and then the angle for the middle is temporarily at 0. That's going to change. And then the angleEnd is provided by the user, and I put that in there and multiply it times pie, because everything has to be in units of pie, so that's what that's for. Then I created a variable called speed, which would be different for each one of the pies. It would be somewhere between 0.005 and 0.005, and that is the change in the angle, measured in radians, as the pie sort of unravels.

Then I have random components for the red, green, and blue components of the color, and then I have the title. And then I finish the constructor there-- that's the initial values--and then I describe what my two methods are: the update and the display. For update, I want it to slowly move around the circle. So, it starts the angleMid, which currently is set at 0, will add on a small amount, and it will go around and circle clockwise. However, I don't want it to go too far. I want it to stop when it gets to the end, and the easiest way to do that is to just use a constraint. Say that the Angle Middle can't be less than 0, because that's where it starts, and it can't go past the user-specified ending value.

Also, I've got this curious little block of code here, which is going to allow me to click on the pies and move them around, because they are randomly positioned and they almost always end up covering one another. This allows me to move them a little bit. And then finally, I have the code displayed that actually draws the pies with Processing's built-in arc function. I just turn off the stroke. I specify the fill with the random RGBs. For arc, you need the X and the Y for the center. You need the diameter, both in terms of width and height.

You need the starting point and the ending point, and that's what I've got in there. And then I've also decided to put some titles on with the user-provided title. I turn those black and move those slightly below and to the right of the center. Anyhow, let me show you how this one works. It's a little silly, but here we go! I press Run, and what I get are five pie charts that are completely overlapping each other, and growing at different rates. But you see that's not something I could do with just a function. In fact, I'll do it again, so you see that we get different every time--different colors and different positions and different sizes.

Then that little loop I showed to you them allows me to reposition them-- if I get the cursor within the radius of the circle, I can start moving stuff around. And I click, I can move it there. What's kind of fun is when I get in the radius of two circles, it moves to both of them. They get locked up temporarily. But the point here is I've prepared a sort of data visualization, even though pie charts are usually not something you want to work with; it's because the arc function was easy to deal with in this one.

But I was able to take just a small amount of user information of different kinds of variables-- in this case, I got a string and a float variable from the user--and then I was able to produce this thing that was dynamic and that evolved as it worked, and allowed me to move it around, too, in a way you couldn't do with the function. And so classes and objects are one of the huge, huge time savers and most important tools you will have in data visualization and your programming work, and I strongly encourage you to take the time to play with them and see how you can make them work in your own Processing sketches.

Show transcript

This video is part of

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

72 video lessons · 12136 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 preferencesfrom 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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.