This site requires JavaScript, please enable it in your browser!
Greenfoot back
K_wow
K_wow wrote ...

2013/6/25

Problem with drawing onto an image

K_wow K_wow

2013/6/25

#
I have been experimenting with using the "setColorAt" method to draw onto one of my images. Here is my code to draw onto the image:
GreenfootImage image = getImage();
image.setColor(Color.BLACK);
for (int currentPixel = getX() - 5; currentPixel < getX() + 5; currentPixel++)
{
        image.setColorAt(currentPixel, getY(), Color.BLACK);
}
Although When I try to move past 48 on the X or Y axis, I come up with this error: java.lang.IndexOutOfBoundsException: Y is out of bounds. It was: 48 and it should have been smaller than: 48 at greenfoot.GreenfootImage.setRGBAt(GreenfootImage.java:601) at greenfoot.GreenfootImage.setColorAt(GreenfootImage.java:543) at Player1.act(Player1.java:101) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) java.lang.IndexOutOfBoundsException: Y is out of bounds. It was: 48 and it should have been smaller than: 48 at greenfoot.GreenfootImage.setRGBAt(GreenfootImage.java:601) at greenfoot.GreenfootImage.setColorAt(GreenfootImage.java:543) at Player1.act(Player1.java:101) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) java.lang.IndexOutOfBoundsException: X is out of bounds. It was: 48 and it should have been smaller than: 48 at greenfoot.GreenfootImage.setRGBAt(GreenfootImage.java:597) at greenfoot.GreenfootImage.setColorAt(GreenfootImage.java:543) at Player1.act(Player1.java:101) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) java.lang.IndexOutOfBoundsException: X is out of bounds. It was: 48 and it should have been smaller than: 48 at greenfoot.GreenfootImage.setRGBAt(GreenfootImage.java:597) at greenfoot.GreenfootImage.setColorAt(GreenfootImage.java:543) at Player1.act(Player1.java:101) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) Does anyone know how to fix this?
davmac davmac

2013/6/25

#
getX() returns the actor position relative to the world; it has nothing to do with the co-ordinates of the actor's own image - so, your code above looks strange. Are you trying to draw on the actor's image or on the world's image? In fact, can you explain exactly what you wanted the code above to do?
davmac davmac

2013/6/25

#
(To explain why you are getting the error: your actor's image is 48x48 pixels in size, so you can't draw past 48 on either axis).
K_wow K_wow

2013/6/25

#
davmac wrote...
getX() returns the actor position relative to the world; it has nothing to do with the co-ordinates of the actor's own image - so, your code above looks strange. Are you trying to draw on the actor's image or on the world's image? In fact, can you explain exactly what you wanted the code above to do?
I wanted the code to draw onto the player's image. I thought that was what would happen. It was just an experiment, but I would like to see it through. What code do I use to draw onto the player's image?
danpost danpost

2013/6/26

#
You were correct in using 'getImage' to get the image of the player. From there, you can use 'getWidth' and 'getHeight' on the image (i.e. 'getImage().getWidth()') to get the dimensions of the image. With that, you can 'setColorAt' any point on that image between 'getImage().setColorAt(0, 0, Color.BLACK)' to 'getImage().setColorAt(getImage().getWidth()-1, getImage().getHeight()-1, Color.BLACK)'.
K_wow K_wow

2013/6/26

#
danpost wrote...
You were correct in using 'getImage' to get the image of the player. From there, you can use 'getWidth' and 'getHeight' on the image (i.e. 'getImage().getWidth()') to get the dimensions of the image. With that, you can 'setColorAt' any point on that image between 'getImage().setColorAt(0, 0, Color.BLACK)' to 'getImage().setColorAt(getImage().getWidth()-1, getImage().getHeight()-1, Color.BLACK)'.
Thanks, it worked perfectly.
You need to login to post a reply.