190 likes | 355 Views
GreenfootImage. Part of the greenfoot.* library. Actor has some useful methods. Since Actor defines them, then any child has them (Hero, Virus, VirusPatrol). setImage & setLocation.
E N D
GreenfootImage Part of the greenfoot.* library
Actor has some useful methods • Since Actor defines them, then any child has them (Hero, Virus, VirusPatrol).
setImage & setLocation • When a class issues a setImage( image ) command (i.e. calls the Actor method setImage), it creates a new picture for that class. • That’s how VirusPatrol became a skull in Lab5. • When a class issues a setLocation( x, y ) command (i.e. calls the Actor method setLocation), it moves the icon to a new location • So a class has some control over it’s own picture
But we need an “image” to do that GreenfootImage myImage = new GreenfootImage( “skull.png “); • Like an instance variable declaration. • No different than “ int x = 0; ” • Except it copies a CLASS into myImage, not an int into x. • Must use “new” for object creation.
INSTANTIATION! • Creates a copy of a class. • The copy is called an OBJECT • The copy is also called an INSTANCE of the CLASS. • The act of creating the class is called INSTANTIATION. • We can instantiate any class, as many times as we want.
Big deal. Is that it? • No. GreenfootImage has it’s own methods that Actor can use.... • Classes (and the objects created from them) bring all their capabilities to the program that creates the copy.
First, some Background.... • In addition to ints, booleans, doubles, variables can also be Strings. Upper case S. • Strings don’t hold numbers or true/false, but human-readable text. • Ex.: “hello” “how are you” “please enter your name” “our next exam is Monday Oct. 18” “14.6” note: Not a number. Just characters.
import javax.awt.*; • Like importing Greenfoot. • Gives us COLORS to use whenever a color is needed. • Stated as Color.BLACK; • Color.GREEN; • Color.RED; • Many more
GrrenfootImage method setColor( ) GreenfootImage skullImage = new GreenfootImage("skull.png"); public void act() { setImage( skullImage ); setLocation( 100, 100 ); skullImage.setColor(Color.BLACK);
Note how you call an Object’s method Objectname . Methodname( );
The Objects name should ALWAYS be stated • Calling another objects method: skullImage.setColor( Color.BLACK ); • Calling your OWN method setImage( skullImage ); should really be: this.setImage( skullImage ); • Calling your PARENT’S methods should be super.Act( )
GreenfootImage method fill( ) GreenfootImage skullImage = new GreenfootImage("skull.png"); public void act() { this.setImage( skullImage ); this.setLocation( 100, 100 ); skullImage.setColor(Color.BLACK); skullImage.fill( );
Draw a String GreenfootImage skullImage = new GreenfootImage("skull.png"); public void act() { this.setImage( skullImage ); this.setLocation( 100, 100 ); skullImage.setColor(Color.BLACK); skullImage.fill(); skullImage.setColor(Color.WHITE); skullImage.drawString( "hello", 8, 20 );
Remember the World class? addObject( new Virus( ), 10, 20 ); The same as: Virus bug = new Virus( ); addObject ( bug, 10, 20 );
Writing to the screen public void writeToScreen( String text ) { GreenfootImage myImage = new GreenfootImage(100, 50); setImage( myImage ); setLocation( 100, 100 ); myImage.setColor(Color.BLACK); myImage.fill(); myImage.setColor(Color.WHITE); myImage.drawString( text, 8, 20); }