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

2017/1/6

A problem drawing any type of shape

Hack2TheFuture Hack2TheFuture

2017/1/6

#
I've copy and pasted code that people swear works to no avail. I'm just trying to draw a rectangle in MyWorld class. public void drawScoreboard(){ GreenfootImage img = new GreenfootImage(200,200); img.drawRect(0,0,100,100); } I put this method in act() and in the constructor and nothing happens. I've tried fill, drawline and the rest of them.
danpost danpost

2017/1/6

#
Hack2TheFuture wrote...
I've tried fill, drawline and the rest of them.
You are creating an image and drawing a rectangle on it. But, that image is never set to the actor. Add the final statement:
1
setImage(img);
davmac davmac

2017/1/6

#
Welcome to the site. Please make sure you use code tags to format code correctly (see the instructions here). You code creates an image and draws a rectangle in it, but the image is not made visible anywhere. In the world class it is possible to draw on the current background:
1
2
GreenfootImage img = getBackground();
img.drawRect(0,0,100,100);
However, for a scoreboard or similar you might want to use an actor, so that you can easily remove it. In that case, from an actor:
1
2
3
GreenfootImage img = new GreenfootImage(200,200);
img.drawRect(0,0,100,100);
setImage(img);
danpost danpost

2017/1/6

#
I missed that it was in a World subclass. The 'setImage' will not work there. For drawing on the world background, you will need to first adjust the drawing color using something like the following:
1
getBackground().setColor(new java.awt.Color(128, 128, 128));
before any visible change will be seen (drawing white on white would be pointless). However, using an Actor object to display a scoreboard, as davmac suggests, is the way to go.
Hack2TheFuture Hack2TheFuture

2017/1/6

#
danpost wrote...
I missed that it was in a World subclass. The 'setImage' will not work there. For drawing on the world background, you will need to first adjust the drawing color using something like the following:
1
getBackground().setColor(new java.awt.Color(128, 128, 128));
before any visible change will be seen (drawing white on white would be pointless). However, using an Actor object to display a scoreboard, as davmac suggests, is the way to go.
Can you explain why setImage() isn't functional in the World class?
Nosson1459 Nosson1459

2017/1/6

#
Hack2TheFuture wrote...
Can you explain why setImage() isn't functional in the World class?
There is no setImage() method in the World class to use in it's subclasses. The above code is for drawing onto the image but to do setImage in a subclass of World you'll have to use "setBackground(java.lang.String filename)" (It's a little different than setImage from Actor).
You need to login to post a reply.