IAT 800 Lecture 4. Sept 18, Fall 2006IAT 8002 Outline  Programming concepts –Methods –Classes  Talk about project 1  Reading: Read Chapters 1-4 of.

Slides:



Advertisements
Similar presentations
SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables.
Advertisements

Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
This terms course Last term we both worked on learning 2 things –Processing –The concepts of graphics etc. This term will focus more on the basic concepts.
A Quick Introduction to Processing
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Translate, Rotate, Matrix Pages Function Function Definition Calling a function Parameters Return type and return statement.
Goal: to rotate a figure around a central point
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
IAT 334 Java using Processing ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
IAT 800 Foundations of Computational Art and Design Lecture 2.
Sep 26, Fall 2006IAT 800 Lecture 6 Methods and Classes.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
, Fall 2006IAT 800 Lab 2: Polygons, Transformations, and Arrays.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Oct 17, Fall 2006IAT 800 ArrayList, Text, and more Image Manipulation.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
IAT 800 Lecture 8. Oct 13, Fall 2006IAT 8002 Outline  Programming concepts –BImage BFont –Creating XImage –Typography.
IAT 800 Lecture Oct, Fall 2006IAT 8002 Outline  Programming concepts –Super and this –Java SDK classes Lists Reading the Java docs –Programming.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Classes / Objects An introduction to object-oriented programming.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Getting Started With Alice By Ruthie Tucker under the direction of Prof. Susan Rodger Duke University, July
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
CIT 590 Intro to Programming First lecture on Java.
Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman.
Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
 Students will be able… › Identify reflections, rotations, and translations. › Graph transformations in the coordinate plane.
An Introduction to Alice (Short Version) – Extras! Yossra Hamid Under the Supervision of Professor Susan Rodger Duke University, June 2014 This is a continuation.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Objects. The Heart of the Matter - The "Black Box" Object-oriented software is all about objects. An object is a "black box" which receives and sends.
Advanced Stuff Learning by example: Responding to the mouse.
Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays Evan.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
An Introduction to Alice By Jenna Hayes under the direction of Professor Susan Rodger Duke University, June 2009 Updated June 2014 by Ellen Yuan.
LCC 6310 Computation as an Expressive Medium Lecture 6.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Computation as an Expressive Medium Lab 2: Polygons, Transformations, and Arrays (Oh My) Micah.
IAT 265 Images in Processing PImage. Jun 27, 2014IAT 2652 Outline  Programming concepts –Classes –PImage –PFont.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
IAT 800 Lecture 8 PImage and PFont. Oct 13, Fall 2009IAT 8002 Outline  Programming concepts –PImage –PFont.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/20/08.
Some of Chap 17.
Computer Graphics: Rocket, Java: Class
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
IAT 265 PImage and PFont.
Chapter 14, Translate & Rotate
Polygons, Transformations, and Arrays
LCC 6310 Computation as an Expressive Medium
Workshop for Programming And Systems Management Teachers
IAT 265 Lecture 2: Java Loops, Arrays Processing setup, draw, mouse Shapes Transformations, Push/Pop.
Building Java Programs
Translate, Rotate, Matrix
Lecture 6 Methods and Classes
Computation as an Expressive Medium
IAT 800 Foundations of Computational Art and Design
Lecture 6 Methods, Classes, Inheritance
Presentation transcript:

IAT 800 Lecture 4

Sept 18, Fall 2006IAT 8002 Outline  Programming concepts –Methods –Classes  Talk about project 1  Reading: Read Chapters 1-4 of Head First Java

Sept 18, Fall 2006IAT 8003 Drawing a rocket background(0); fill(255); triangle(10, 0, 0, 20, 20, 20); rectMode(CORNERS); rect(5, 20, 8, 23); rect(12, 20, 15, 23);

Sept 18, Fall 2006IAT 8004 Now I want to draw several rockets  Want several rockets in different locations on the screen  I could copy and paste the code –Need to adjust all the numbers for the new location  Or… define a method

Sept 18, Fall 2006IAT 8005 First method for drawing a rocket void drawRocket() { fill(255); triangle(10, 0, 0, 20, 20, 20); rectMode(CORNERS); rect(5, 20, 8, 23); rect(12, 20, 15, 23); } Gotcha! Once you start using methods, all code must be in methods (can’t just directly call drawRocket() at the top of the file)

Sept 18, Fall 2006IAT 8006 Didn’t seem to help much…  Still just draws a rocket at one fixed location  Need arguments that allow me to tell the program where I want the rocket! –Must figure out the relationship between the position and the location of the rest of the parts  Argument variables are available within the method, but not outside (method scope)

Sept 18, Fall 2006IAT 8007 DrawRocket() with arguments void drawRocket(int noseX, int noseY) { int bottomOfRocket = noseY + 20; int leftOfRocket = noseX - 10; int rightOfRocket = noseX + 10; fill(255); triangle(noseX, noseY, leftOfRocket, bottomOfRocket, rightOfRocket, bottomOfRocket); rectMode(CORNERS); rect(leftOfRocket + 5, bottomOfRocket, leftOfRocket + 8, bottomOfRocket + 3); rect(rightOfRocket - 8, bottomOfRocket, rightOfRocket - 5, bottomOfRocket + 3); }

Sept 18, Fall 2006IAT 8008 Now I’d like to specify rotation  Currently I specify the nose position and it draws a rocket that faces straight up  To rotate the rocket, I’d need to figure out the new position of the vertices –Too hard!  Instead of rotating the rocket, rotate the paper –As long as I’m rotating the paper, I may as well slide it around as well  To prepare for translating and rotating, first draw the rocket centered around the origin (0,0)

Sept 18, Fall 2006IAT 8009 Rocket centered around origin void drawRocket(int x, int y, float rot) { final int halfHeight = 10; final int halfWidth = 10; triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); } We’re purposely ignoring the arguments for now

Sept 18, Fall 2006IAT Add translation and rotation void drawRocket(int x, int y, float rot) { final int halfHeight = 10; final int halfWidth = 10; translate(x, y); rotate(rot); triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); } Let’s try drawing several rockets

Sept 18, Fall 2006IAT Works fine for one call…  But when we call it twice, it doesn’t seem to work –Looks like first call messes up subsequent calls  What happening is that, when you move the paper (translate, rotate) the paper stays moved  Subsequent paper moves (additional translates and rotates) build on top of the previous ones  What we want is to move the paper, draw, and move it back  We need pushMatrix() and popMatrix() –pushMatrix() remembers the previous paper position (previous transformation matrix) –popMatrix() restores the paper to the remembered position (restores the transformation matrix)

Sept 18, Fall 2006IAT Using pushMatrix() and popMatrix() void drawRocket(int x, int y, float rot) { final int halfHeight = 10; final int halfWidth = 10; pushMatrix(); translate(x, y); rotate(rot); triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); popMatrix(); }

Sept 18, Fall 2006IAT Classes  Java (Processing) is an object-oriented language  This means that parts of your program that you treat as conceptual things, become things (objects) in the program code  Objects are built from classes –Classes are the blueprint, objects are built from the blueprint –Objects are called instances of classes  Our rocket sure seems like a thing – lets turn it into a class

Sept 18, Fall 2006IAT Parts of a class  Classes define fields, constructors and methods  Fields are the variables that will appear inside every instance of the class –Each instance has it’s own values  Constructors are special methods that define how to build instances (generally, how to set the initial values of fields)  Methods are how you do things to instances

Sept 18, Fall 2006IAT Defining the rocket class class Rocket { // fields float rotation = 0; float xPos; float yPos; final int halfWidth = 10; final int halfHeight= 10; // constructor Rocket(int initialX, int initialY, float initialRot) { xPos = initialX; yPos = initialY; rotation = initialRot; }

Sept 18, Fall 2006IAT Using the class to create instances  Classes define a type  You can now declare variables of this type and initialize them using the constructor  Like arrays, the keyword new is used to tell Java to create a new object (hmm, so arrays must be objects…) Rocket r1, r2 ; void setup() { r1 = new Rocket(75, 10, 0); r2 = new Rocket(50, 50, PI/2); }  Nice, but my rockets don’t do anything (need methods!)

Sept 18, Fall 2006IAT Adding a draw routine to our Rocket void draw() { push(); translate(xPos, yPos); rotate(rotation); triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight); rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3); pop(); } Don’t need arguments because we use the fields But we could define additional arguments if we wanted to

Sept 18, Fall 2006IAT Calling methods on objects  You call methods on instances  Think of the method as something your asking the object to do  For example, we can now ask the rockets to draw themselves –r1.draw();  In general, to call a method, take the name of the variable holding the object + “.” + the method name –myObject.myMethod();

Sept 18, Fall 2006IAT What else do we want to do to the Rocket?  We may want to change the rotation –rotateClockwise(); –rotateCounterclockwise();  We may want to give the rocket a boost –fireThrusters();