Search code examples
javaswingloopspaintcomponent

Having Paint loop trouble in java


For whatever the reason, my loops do not seem to be printing the colors correctly. I have my colors set up before my fills and draws but it isn't showing up the Color.white at all. It is showing white. To better explain what i want the picture to look like ill attach a photo. Thank for any help!

enter image description here

This is my ExampleGUI.java source code

import javax.swing.*;

public class ExampleGUI {
public static void main(String args []) {


    JFrame frame = new JFrame("Example Graphics");
    ExamplePanel panel = new ExamplePanel();

    frame.getContentPane().add(panel);
    frame.setDefaultCloseOperation(3);
    frame.pack();
    frame.setVisible(true);

}
}

This is my ExamplePanel.java source code

  import java.awt.*;

  import javax.swing.*;


public class ExamplePanel extends JPanel{

public ExamplePanel() {
    setPreferredSize(new Dimension (600, 600));
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);


    int x = 0;
    int x2 = 5;
    int y = 500;
    int y2 = 505;
    int w = 100;
    int w2 = 90;
    int h = 100;
    int h2 = 90;
    int i, j;

    for(j = 1; j < 7; j++) {
        x = 0;
        x2 = x + 5;

        for(i = 1; i < 7; i++) {
            if(i % 2 == 0) {
                g.setColor(Color.white);
                g.fillRect(x, y, w, h);
                g.setColor(Color.black);
                g.drawRect(x, y, w, h);
                g.setColor(Color.green);
                g.fillOval(x2, y2, w2, h2);
                g.setColor(Color.black);
                g.drawOval(x2, y2, w2, h2);

            }else if (i % 2 == 1 && j % 2 == 1)
                    g.setColor(Color.yellow);
                    g.fillRect(x, y, w, h);
                    g.setColor(Color.black);
                    g.drawRect(x, y, w, h);
                    g.setColor(Color.green);
                    g.fillOval(x2, y2, w2, h2);
                    g.setColor(Color.black);
                    g.drawOval(x2, y2, w2, h2);

            x = x + w;
            x2 = x2 + w2 + 10;


        }

        x = x + w;

        y = y - h;
        y2 = (y2 - h2) - 10;
    }


}
}

Solution

  • Lets start by removing the duplication, for example, instead of...

    if (i % 2 == 0) {
        g.setColor(Color.white);
        g.fillRect(x, y, w, h);
        g.setColor(Color.black);
        g.drawRect(x, y, w, h);
        g.setColor(Color.green);
        g.fillOval(x2, y2, w2, h2);
        g.setColor(Color.black);
        g.drawOval(x2, y2, w2, h2);
    
    } else if (i % 2 == 1 && j % 2 == 1) {
        g.setColor(Color.yellow);
    }
    g.fillRect(x, y, w, h);
    g.setColor(Color.black);
    g.drawRect(x, y, w, h);
    g.setColor(Color.green);
    g.fillOval(x2, y2, w2, h2);
    g.setColor(Color.black);
    g.drawOval(x2, y2, w2, h2);
    

    You really only need...

    if (i % 2 == 0) {
        g.setColor(Color.white);
    } else if (i % 2 == 1 && j % 2 == 1) {
        g.setColor(Color.yellow);
    }
    g.fillRect(x, y, w, h);
    g.setColor(Color.black);
    g.drawRect(x, y, w, h);
    g.setColor(Color.green);
    g.fillOval(x2, y2, w2, h2);
    g.setColor(Color.black);
    g.drawOval(x2, y2, w2, h2);
    

    Anything you did in the first if statement is getting wiped out by the other paint code outside of the if-else block...

    Next, your logic is slightly off, instead of...

    if (i % 2 == 0) {
        g.setColor(Color.white);
    } else if (i % 2 == 1 && j % 2 == 1) {
        g.setColor(Color.yellow);
    }
    

    You could use something more like...

    if ((i + j) % 2 == 0) {
        g.setColor(Color.white);
    } else {
        g.setColor(Color.yellow);
    }
    

    This will get you the checker board look you're after.

    Test

    Step closer. You will need another for-loop to generate the inner circles, try not to directly modify the values you are currenly relying on though ;)

    You could also use the if block to define the background and foreground colors you need to use, assign them to some variables and apply them within the paint section, this will give you more flexibility