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

2024/3/2

Draw Polygons on Image Background

UmbranDrake UmbranDrake

2024/3/2

#
I would like to draw on top of an image background (a .png file); however, I do not see the polygon despite not getting any warnings or errors. Below, in the MyWorld(), works but I do not have the file background.
Private GreenfootImage bg = new GreenfootImage(getWidth(), getHeight());
//World is 700x365
setBackground(bg);
bg.setColor(Color.GREEN);
bg.fill();
bg.setColor(Color.RED);
int[] xPts = {600,700,700};
int[] yPts = {200,180,235};
bg.drawPolygon(xPts, yPts, 3);
For adding the .png as the background, I have...
GreenfootImage bg = new GreenfootImage(getWidth(),getHeight());
setBackground("images/nPlayBG.png");
bg.fill();
bg.setColor(Color.RED);
int[] xPts = {600,700,700};
int[] yPts = {200,180,235};
bg.drawPolygon(xPts, yPts, 3);
Now, I have the file background but I cannot see any drawn polygons. Thoughts?
nccb nccb

2024/3/2

#
You can only set one image as background. In the second code, you are creating an image named bg, then setting background to be a new image loaded from the file. You then manipulate bg, but bg is not your background image; it's nowhere on screen so you can't see it. Either construct bg by loading from the image file, or use setBackground then get bg from getBackground rather than making your own image.
UmbranDrake UmbranDrake

2024/3/2

#
nccb wrote...
Either construct bg by loading from the image file, or use setBackground then get bg from getBackground rather than making your own image.
So it would look something like the code below?
setBackground("images/nPlayBG.png");
GreenfootImage bg = getBackground();
Note: Code above is not included in the scenario--only to check understanding of your suggestion!
nccb nccb

2024/3/3

#
Yes, that's right.
UmbranDrake UmbranDrake

2024/3/3

#
Thank you!!
You need to login to post a reply.