This site requires JavaScript, please enable it in your browser!
Greenfoot back
vtn2@calvin.edu
vtn2@calvin.edu wrote ...

2014/6/25

Setting the pen size for drawing on the background

vtn2@calvin.edu vtn2@calvin.edu

2014/6/25

#
I am trying to add functionality so that you can have an Actor draw on the background as it moves. The basic functionality works great, but now I am trying to allow the user to set the pensize to be something > 1 pixel wide. To do this, I've discovered the setStroke() method for awtImages. I've implemented code like this in my Actor:
1
2
3
4
5
6
public void setPenSize(int size)
{
    penSize = size;
    java.awt.Graphics2D g = getWorld().getBackground().getAwtImage().createGraphics();
    g.setStroke(new java.awt.BasicStroke(size));
}
However, calling this function seems to do nothing. I'm wondering if anyone knows why this does not work or knows more about how to make it work. I have a feeling I'm getting a new copy of the awt image that underlies the background and then I am making changes to that copy, but I can't figure out how to verify if that's what is happening. Thanks! Vic
danpost danpost

2014/6/25

#
You could just draw, or fill, an oval of the 'penSize' onto the background image. Something like the following:
1
2
3
4
5
6
7
8
9
Color drawColor = Color.black;
int penSize = 3;
 
private void layInk()
{
    GreenfootImage bg = getWorld().getBackground();
    bg.setColor(drawColor);
    bg.fillOval(getX()-penSize/2, getY()-penSize/2, penSize, penSize);
}
Then your method above would be:
1
2
3
4
public void setPenSize(int size)
{
    penSize = size;
}
vtn2@calvin.edu vtn2@calvin.edu

2014/6/25

#
Thanks for your suggestion. But, I'd really like to use the setStroke() that seems to be available and just exactly what I want. I'm just really curious why the code I wrote does not work.
danpost danpost

2014/6/26

#
I believe you still need to create the shape (using 'createStrokedShape') and then render it (using 'draw').
vtn2@calvin.edu vtn2@calvin.edu

2014/6/28

#
No, none of this is working. but, I did get it to work by changing the greenfoot source code. Thanks anyway.
You need to login to post a reply.