For my program I am working with simple graphics and need to display a straight row of squares from left to right decreasing in size. I also have to program this recursively so that it makes the squares following the first one by itself. My problem right now is that it does not display any of the squares besides the first one. Here is my recursive method I created, and I am not sure where it is going wrong.

class Windows extends Frame
{
	
static Random dice = new Random();

	public void paint(Graphics g)
	{
		drawSquare(g,0,100,200);
	}
	
	public void drawSquare(Graphics g, int x, int y, int size)
	{
		if(size>=1)
		{
			switch(dice.nextInt(8))
			{
				case 0: g.setColor(Color.green);
				break;
				case 1: g.setColor(Color.orange);
				break;
				case 2: g.setColor(Color.red);
				break;
				case 3: g.setColor(Color.blue);
				break;
				case 4: g.setColor(Color.yellow);
				break;
				case 5: g.setColor(Color.cyan);
				break;
				case 6: g.setColor(Color.pink);
				break;
				case 7: g.setColor(Color.magenta);
				break;
			}
			g.fillRect(x,y,size,size);
			x=x+size+5;
			y=size*(1/4)+y;
			size=size*(3/4);
			drawSquare(g,x,y,size);
		}
	}
}

Your problem is with your 1/4 and 3/4. In Java, both of these evaluate to 0, not 0.25 and 0.75 as you might be expecting. Division of integers in Java (and many other programming languages) always evaluates to an integer, and the decimal part is simply dropped. So the line:

size=size*(3/4);

evaluates to 0, meaning that on your recursive call, size is not >= 1, so the second square is not drawn.

Try changing the two lines to this:

y=y+(size/4);
size=3*size/4;

That should fix your problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.