How can I completely fill new drawn shapes over old shapes

Viewed 27

I am trying to write a program in Java that draws trees filling the screen. I have variables set to randomly place a tree on the x-axis and move down 10 steps each time. The problem I am encountering is when the new trees are drawn they overlap the old trees so the fill does not fill the tree. So my question is how can I fully fill a drawn shape filling over what is underneath. I have tried messing with the color I'm using but I just don't know what to do. I am brand new to Java but I have a fair bit of experience in Javascript. Here is my code so far:

public class Turtle extends TurtleGraphicsWindow
{
    
    public Turtle()
    { 
        super(650, 550);
    }

    static void PrintTree() {
        Turtle t = new Turtle();
        t.speed(100);
        
        for (int i = 0; i < 30; i++) { //to move the trees down
            //randomizer
            int min = -300;
            int max = 300;
            int random_int = (int)Math.floor(Math.random()*(max-min+1)+min);
            var tWx = random_int;

            int tWy = -10*i+150;
                //trunk
                t.pu();
                t.setpensize(3);
        
                t.setxy( (tWx-25) , (tWy-100) );
                t.setpc(8); //brown
                t.pd();
                t.fd(50);
                t.setx(t.xcor()+50);
                t.bk(50);
                t.setx(t.xcor()-50);
                t.pu();
                t.setxy(t.xcor()+5,t.ycor()+5);
                t.setpc(9); //tan
                t.fill();

                //tree
                t.setxy((tWx+100),(tWy-50));
                t.setpencolor(31);
                t.pd();
                var tx = 50;
                t.setx(t.xcor()-200);
                t.setxy(t.xcor()+tx,t.ycor()+(tx+10));
                t.setx(t.xcor()-(tx-15));
                t.setxy(t.xcor()+(tx-5),t.ycor()+(tx+5));
                t.setx(t.xcor()-(tx-20));
                t.setxy(t.xcor()+(tx-10),t.ycor()+(tx));
                t.setx(t.xcor()-(tx-30));
                t.setxy(t.xcor()+(tx-15),t.ycor()+(tx-5));
                t.setx(t.xcor()-(tx-35));
                t.setxy(t.xcor()+(tx-20),t.ycor()+(tx-10));
            //right side of tree
                t.setxy(t.xcor()+(tx-20),t.ycor()-(tx-10));
                t.setx(t.xcor()-(tx-35));
                t.setxy(t.xcor()+(tx-15),t.ycor()-(tx-5));
                t.setx(t.xcor()-(tx-30));
                t.setxy(t.xcor()+(tx-10),t.ycor()-(tx));
                t.setx(t.xcor()-(tx-20));
                t.setxy(t.xcor()+(tx-5),t.ycor()-(tx+5));
                t.setx(t.xcor()-(tx-15));
                t.setxy(t.xcor()+tx,t.ycor()-(tx+10));

                t.pu();
                t.setpc(FOREST);
                t.setxy(t.xcor()-10,t.ycor()+5);
                t.fill();
        }
    }

    public static void main(String[] args)
    {   
        PrintTree();
    }
}

This is what it has been looking like now: enter image description here

https://i.stack.imgur.com/ipwhN.png

0 Answers
Related