1 / 17

GreenfootImage

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.

nishi
Télécharger la présentation

GreenfootImage

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GreenfootImage Part of the greenfoot.* library

  2. Actor has some useful methods • Since Actor defines them, then any child has them (Hero, Virus, VirusPatrol).

  3. 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

  4. 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.

  5. 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.

  6. Let’s try..

  7. 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.

  8. GreenfootImage

  9. 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.

  10. 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

  11. GrrenfootImage method setColor( ) GreenfootImage skullImage = new GreenfootImage("skull.png"); public void act() { setImage( skullImage ); setLocation( 100, 100 ); skullImage.setColor(Color.BLACK);

  12. Note how you call an Object’s method Objectname . Methodname( );

  13. 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( )

  14. 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( );

  15. 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 );

  16. Remember the World class? addObject( new Virus( ), 10, 20 ); The same as: Virus bug = new Virus( ); addObject ( bug, 10, 20 );

  17. 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); }

More Related