Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.

Slides:



Advertisements
Similar presentations
Written by: Dr. JJ Shepherd
Advertisements

Games and Simulations O-O Programming in Java The Walker School
Chapter 2 - The First Program: Little Crab
Program: Little Crab Mr Gano.
Greenfoot Asteroids Mrs. C. Furman August 16, 2010.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Structure of a C program
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
CMT Programming Software Applications
Games and Simulations O-O Programming in Java The Walker School
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CSE 113 Introduction to Computer Programming Lecture slides for Week 4 Monday, September 19 th, 2011 Instructor: Scott Settembre.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The Java Programming Language
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CPS120: Introduction to Computer Science Decision Making in Programs.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Java Translation Program Structure
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Copyright Curt Hill Variables What are they? Why do we need them?
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 4 - Finishing the Crab Game
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter # 2 Part 2 Programs And data
Chapter 4 - Finishing the Crab Game
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Yanal Alahmad Java Workshop Yanal Alahmad
Documentation Need to have documentation in all programs
Variables and Primative Types
Chapter 3 Assignment Statement
IDENTIFIERS CSC 111.
Building Java Programs Chapter 2
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Organizing Memory in Java
Unit-1 Introduction to Java
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Methods, Data and Data Types
Tutorial 10: Programming with javascript
Primitive Types and Expressions
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Variables and Constants
Section 6 Primitive Data Types
Presentation transcript:

Animation Mrs. C. Furman

Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png

Greenfoot images  We create new images by using the GreenfootImages class.  new GreenfootImage(“crab2.png”);  This file must be saved in the scenario’s images folder.

How Do Actors Get Images?  Images are assigned to classes when they are created.  Every object created from the class will receive a copy of the image.  This image can be changed  Each actor can decide to change it’s image.

Exercise  Check the documentation of the Actor class. There are two methods that allow us to change an actor’s image.  What are they called, and what are their parameters?  What do they return?

this.setImage (new GreenfootImage (“crab2.png”));  We can use the above method to set the image to something new.  Two things are happening…  A new GreenfootImage is created  This new image is being set as the image for the actor.

Setting image in Two Lines  It is better for us to separate the previous statement into 2.  This way we can better switch between the 2 images.

Storing Created Images  We will need to store the 2 images that we will be switching between  We will need variables.

Variables  They are named memory locations.  They are used to store values or objects.  We will be creating a type of variable called an instance variable, or field  Instance Variables are a bit of memory that belongs to an object. As long as the object exists and can be referenced, the instance variables exist.

Identifiers  When we name variables we have to follow these rules.  Choose names that: 1.Start with a letter 2.Contain only letters, numbers, underscores or the $ symbol 3.Can not be a keyword

Variable Identifiers  Variable names (identifiers) should clearly describe what we are storing. Use image1, rather than x.

Access Specifiers  There are 3 kinds of access specifiers, we will discuss 2 of them  public – other objects have access to these value and methods  private – only this class has access to the values and methods.

Instance Variables  For the most part, we will make our instance variables private.  Our methods up until this point have been public

Data Types  We also need to know data types when declaring a variable  There are 4 primitive types you need to know: int, double, boolean, char  Everything else is stored in an object.

int  stores positive and negative whole numbers and 0

double  stores real numbers (decimal numbers and integers)

boolean  true or false

char  a single character (letter, digit, symbol)  char literal – a character enclosed in a single quote.  char variables can be assign char literals or numbers

Storing our Image  Our image is not a primitive, it is a class  The type of our instance data is GreenfootImage  So…

What’s the line?  private GreenfootImage image2;  Before we add this to Crab, right click on Crab and select inspect  This will tell us what variables are already in the Crab object.  What variables do you find?

Look at Crab Code  Do you see WALKING_SPEED declared in Crab?  Where is it?  Why is it showing up in Crab?

Where do we put instance variables???  Instance variables are always listed first in a class.  Right after the class header and before the Constructors and methods.  This is a convention that Java programmers follow

Add Instance Variables to Crab  Above the act method add: private GreenfootImage image1; private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image2;  Recompile and right click to inspect the class again. Are these new variables added?

Null  What are image1 and image2 set to?  Null – means that the variables have been declared, but NO objects have been created.  We will need to create GreenfootImage objects to be store in image1 and image2

Assignments  = This is the assignment operator in Java  The variable goes on the LEFT always, and the value being assigned goes on the RIGHT.  This should not be confused with == which we use to compare two things to see if they are equal

Assignments  variable = expression;  image1 = new GreenfootImage (“crab.png”);  image2 = new GreenfootImage (“crab2.png”);  We need to put this code into a Constructor method of Crab  Constructors main job is to give values to instance data.

Create a Constructor  Constructors have the same name as the class  Constructors have NO return type

Above Act and After instance Data write… public Crab() { image1 = new GreenfootImage (“crab.png”); image1 = new GreenfootImage (“crab.png”); image2 = new GreenfootImage(“crab2.png”); image2 = new GreenfootImage(“crab2.png”); this.setImage (image1); this.setImage (image1);}

Compile and Inspect  Inspect Crab again  What are the value of image1 and image2?

if..else statements  We have looked at if statements..  if (something is true) { do something } { do something } What if the something is false???

if.. else  if..else provides us with something to do if the condition is false. if (condition) { execute this if condition is true } else { execute this if condition is false }

What Do we want to do?? if (the current image is image 1) { change the image to image2 } else { change the image to image1 } The above is pseudocode.

.equals( )  When we want to see if 2 objects are the same, we must use.equals()  this.getImage ().equals (image1) Current Imageimage1 variable

GreenfootImgage getImage()  This method returns the current image if (this.getImage().equals(image1)) { this.setImage(image2); this.setImage(image2);}else{ this.setImage (image1); this.setImage (image1);}

method toggleImage  Write a new method in Crab called toggleImage()  What is the return type of this method?  The body should be our if.. else statement  Be sure to call this.toggleImage() in the act() method.  Compile and run… what happens?

toogleImage method public void toggleImage() { if (this.getImage().equals(image1)) if (this.getImage().equals(image1)) { this.setImage(image2); this.setImage(image2); } else else { this.setImage (image1); this.setImage (image1); }