-1

The output of the code at the moment is the rectangle design and the first line of the array repeated. The wanted output is the rectangle design and the whole array rather than just the first line. enter image description here

public class design
{
public static void main (String[] args)
{
    JFrame window = new JFrame ("Game Screen");
    window.getContentPane ().add (new drawing ());
    window.setSize (500, 500);
    window.setVisible (true);
    window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
class drawing extends JComponent
{
public void paint (Graphics g)
{
    int[] [] word = {{5, 3, 0, 0, 7, 0, 0, 0, 0},
            {6, 0, 0, 1, 9, 5, 0, 0, 0},
            {0, 9, 8, 0, 0, 0, 0, 6, 0},
            {8, 0, 0, 0, 6, 0, 0, 0, 3},
            {4, 0, 0, 8, 0, 3, 0, 0, 1},
            {7, 0, 0, 0, 2, 0, 0, 0, 6},
            {0, 6, 0, 0, 0, 0, 2, 8, 0},
            {0, 0, 0, 4, 1, 9, 0, 0, 5},
            {0, 0, 0, 0, 9, 0, 0, 7, 9}};
    int r = 0;
    int c = 0;
    Graphics2D g2 = (Graphics2D) g;
    Rectangle rect;
    for (int x = 5 ; x < 450 ; x += 50)
    {
        for (int y = 5 ; y < 450 ; y += 50)
        {
            rect = new Rectangle (x, y, 50, 50);
            g2.draw (rect);
            g.drawString (Integer.toString (word [r] [c]), x + 25, y + 25);
        }
        c++;
        if (c == 9)
        {
            c = 0;
            r++;
        }
    }
    rect = new Rectangle (150, 5, 150, 450);
    g2.draw (rect);
    rect = new Rectangle (5, 150, 450, 150);
    g2.draw (rect);
}
}
Cindy Er
  • 11
  • 2

2 Answers2

0

Try to put r++; in the second for loop after calling g.drawString. r must also be set to 0 inside the first for loop and before entering the second one.

for (int x = 5 ; x < 450 ; x += 50){
    r = 0;
    for (int y = 5 ; y < 450 ; y += 50){
        rect = new Rectangle (x, y, 50, 50);
        g2.draw (rect);
        g.drawString (Integer.toString (word [r] [c]), x + 25, y + 25);
        r++;
    }
    c++;
}

It would be more readable (and logical in my opinion) to use only two variables for the loops, x and y in your case, and increment them only by one. You can use them to calculate the positions of your rectangles and place your numbers:

for (int x = 0 ; x < 9 ; x++){
    for (int y = 0 ; y < 9 ; y++){
        rect = new Rectangle (5+x*50, 5+y*50, 50, 50);
        g2.draw (rect);
        g.drawString (Integer.toString (word [y] [x]), 5+x*50 + 25, 5+y*50 + 25);
        r++;
    }
    c++;
}

Avoid using magic numbers, it's better to define constant variables and give them an appropriate name. You can then easily change their values, and your code is clearer.

What are "magic numbers" in computer programming?

0

The issue with your logic is that increment of c doesn't follow increment of y(y-axis). i.e. as per your logic c represents y axis in your 2-dimensional array, but you are incrementing it in the for loop of x axis. Solution to this is, move your logic of c increment in inner for loop

for (int x = 5; x < 450; x += 50) {
        for (int y = 5; y < 450; y += 50) {
            rect = new Rectangle(x, y, 50, 50);
            g2.draw(rect);
            g.drawString(Integer.toString(word[r][c]), x + 25, y + 25);
            c++;
        }
        if (c == 9) {
            c = 0;
            r++;
        }
    }

As your loop will only increment 9 times, moving of if condition along with c++ is not required.

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41