The size of this image will be the same as the size of the image on the file. GreenfootImage img = new GreenfootImage( "crab.png" ) įorm 2 - creating a new GreenfootImage object from another GreenfootImage object GreenfootImage( GreenfootImage image )In this form, the GreenfootImage object will be a copy of another GreenfootImage object. GreenfootImage image1 = new GreenfootImage( "crab.png" ) GreenfootImage image2 = new GreenfootImage( image1 ) įorm 3 - creating an image with Java code GreenfootImage( int width, int height )In this form, the constructor creates a GreenfootImage object that has dimensions width by height. GreenfootImage img = new GreenfootImage( 100, 30 ) This image is transparent, by default, until you draw something onto it. You can clear the image (remove all the graphics from the image): Regardless of which constructor you used to create the GreenfootImage object, you may use any of the methods in the GreenfootImage class to manipulate the image. This statement draws the String "Hello World" onto the image starting 5 pixels from the left side of the image and 25 pixels from the top of the image. Note that the bottom of the first letter will be written 25 pixels from the top of the image.Ĭhanges the current drawing colour (which is like dipping a paint brush into the can of red paint. there's red paint on the brush but nothing changes on the paper). Note that to use this method you must import the java.awt package, which contains the Color class. See the Color class' documentation for more information about the Color class and its methods. Img.setFont( new Font("Comic Sans MS", Font.PLAIN, 24) Ĭhnages the current font. so that the next text drawn onto the image will use this new font. More information about the Font class can be found in the Java Class documentation.Ĭreating the GreenfootImage object is one thing, but the image must be assigned to an object before it becomes visible: Again, the Font class must be imported (its also in the java.awt package). Sets the object's current image to the GreenfootImage stored in variable img.Īssigns a reference to the object's current image to the variable img. You'd do this if you want to make changes to the current image of an object.Ī complete example - a label that looks like a gaugeĪ gauge keeps track of some score. It is really just a filled-in rectangle inside an open rectangle. GreenfootImage img = new GreenfootImage( 100, 10 ) The fillRect method draws the filled in rectangle while the drawRect draws the open rectangle. Img.drawRect(0,0,99,9) // draws the open rectangle as a border around the image. The first two are the coordinates of the upper left-hand corner of the rectangle to be drawn.The drawRect and fillRect methods require 4 parameters: Score += points // add points to current score Img.fillRect(1,1,score+2,9) // draws the filled rectangle inside the open rectangle. Remember that everything is zero-indexed, so the upper left-hand corner of the image has coordinates (0,0).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |