Today I was trying to draw a simple rectangle in Greenfoot. Images can dynamically made with the GreenfootImage class. When I drew my rectangle today, it was missing its bottom and right sides!
My initial code looked something like this:
// code inside of Actor called Thing public Thing() { draw(); } public void act() { draw(); } public void draw() { GreenfootImage gi = new GreenfootImage(120, 120); gi.drawRect(10, 10, 120, 120); this.setImage(gi); }
This code will set the image of an Actor called Thing to what should be a Square.
It is safe to say that there is something missing from this picture. Two of the sides are missing on that Square, the bottom and the right.
At first, I thought it was a problem with my actor, but all it did was set its own image when constructed, so that wasn’t the case. So how do you fix it?
To fix this problem, here’s what I did. I change the draw method slightly, here’s the code.
public void draw() { GreenfootImage gi = new GreenfootImage(120+1, 120+1); gi.drawRect(10, 10, 120, 120); this.setImage(gi); }
Notice those +1s? Those increase the drawing space of the image to 121 square. A quick recompile and run reveals this solves the issue. Apparently, Greenfoot will not draw if it meets the end of the images alloted space.
Have fun with Greenfoot.