0

I'm trying to make a drawing panel that draws little circles with numbers in it, going from 0 to 9. However, the for loop, for one reason or another, keeps skipping 0. I've tried setting i to -1, but that doesn't change anything. Conversely when I set the condition to i < 11, it draws all numbers from 1 to 10.

Here's the code so far:

`

// Draws boxed ovals using a while loop.
import java.awt.*;

public class DrawLoop 
{
    public static void main(String[] args) 
    {
        DrawingPanel panel = new DrawingPanel(502, 252);
        panel.setBackground(Color.CYAN);
        Graphics g = panel.getGraphics();
        
        String iValue = "";
        int sizeX = 50;  // size of boxes
        int sizeY = 25;
        int i;
        for (i = 0; i < 10; i++) { // start at i = 0 
            int cornerX = i*50;  // calculate upper left corner
            int cornerY = i*25;
            g.setColor(Color.WHITE);
            g.fillOval(cornerX + 5, cornerY + 5, sizeX-10, sizeY-10);
            g.setColor(Color.BLACK);
            g.drawRect(cornerX, cornerY, sizeX, sizeY);
            iValue = "" + i;
            g.drawString(iValue, cornerX - 29, cornerY - 8);
        }
    }
}

`

Tried to make a drawing panel that drew numbers from 0-9, loop skips 0 and only does 1-9.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • *g.drawString(iValue, cornerX - 29, cornerY - 8);* When `i` is zero, those values will be negative and thus off-screen. e.g. `cornerX` = 0 * 50 = 0 – g00se Oct 31 '22 at 16:41
  • Your x/y coordinates are incorrect. Read the API for the `drawString(...)` method. The coordinates should be bottom/left not top/left. Also, don't use `getGraphics()` to do custom painting. Read the Swing tutorial on [Custom Paintng](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for the proper way to do this. – camickr Oct 31 '22 at 16:42

0 Answers0