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

2021/5/18

For Loop drawPolygon

ronald ronald

2021/5/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 600, 1);
        setBackground(Background());
    }
    
    public GreenfootImage Background()
    {
        GreenfootImage background = new GreenfootImage(getWidth(), getHeight());
        background.setColor(Color.BLACK);
        
        for (int i = 0; i < 9; i++)
        {           
            int [] x = {getWidth()/2, getWidth(), getWidth()*0};
            int [] y = {getHeight()*0, 150+getHeight()/2, 150+getHeight()/2};
            background.drawPolygon(x, y, 3);
            background.rotate(180);
        }
        
        return background;       
    }
}
Hello I do not understand too much for loop normally if i <9; He should have eight triangles while there are two triangles with rotate (180) Thank you for your help
danpost danpost

2021/5/18

#
Actually, you are drawing 9 triangles. However, by rotating 180 between each drawing, all odd numbered ones are drawn completely on top of each other and likewise for all even ones. Use i<8 and rotate 45.
ronald ronald

2021/5/18

#
All triangles are poorly trained I also want to show you a drawing screenshot capture how to do ? thank you
danpost danpost

2021/5/18

#
ronald wrote...
All triangles are poorly trained
Rotating the image is not best for precise drawing. Calculate the point locations for each triangle and draw each on an non-rotated image.
ronald ronald

2021/5/18

#
ok thank you
You need to login to post a reply.