简体   繁体   中英

How do I make squares be on the same level as each other?

This is what my output is :

This is how I want it to be :

public void drawSquare(Graphics g, int x, int y, int size)
{
    g.drawRect(x,y,size,size);
    if(size >2 && x < 1001 && y <= 200)
    {
        x+= 10+size;     
       // y = I know I'm supposed to modify y, but I'm lost on how to make it level out...
        size = size*3/4;
        drawSquare(g,x,y,size);
    }
}   

Help would be appreciated.

The problem is that the y-axis points downward, therefore the tops of the squares are aligned. Instead, to keep the bottoms aligned, consider that the y coordinate for each square must be adjusted by the size difference to the previous square. What I mean is that if you look at any two squares, the square on the right needs to be moved downward by some amount, and that amount is equal to the difference in size between the two squares.

public void drawSquare(Graphics g, int x, int y, int size)
{
    g.drawRect(x,y,size,size);
    if(size >2 && x < 1001 && y <= 200)
    {
        x+= 10+size;     
        int newSize = size*3/4;
        y = y + size - newSize; // (size - newSize) is the difference
        size = newSize;
        drawSquare(g,x,y,size);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM