0

When I input all of the values then click generate, it works, but it doesn't work when I try it again.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class colRand {

    static JPanel[][] square;
    static JFrame colRand = new JFrame();
    static JPanel settings = new JPanel();
    static JPanel panel = new JPanel();

    public static void main(String[] args) {
        colRand.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        colRand.setLocationRelativeTo(null);
        colRand.setTitle("Color Randomizer");
        JTextArea dim = new JTextArea("Grid Dimensions");
        dim.setEditable(false);
        JTextField width = new JTextField("Width");
        JTextField height = new JTextField("Height");
        JCheckBox reds = new JCheckBox("reds");
        JCheckBox greens = new JCheckBox("greens");
        JCheckBox blues = new JCheckBox("blues");
        JButton generate = new JButton("Generate!");
        settings.add(dim);
        settings.add(width);
        settings.add(height);
        settings.add(reds);
        settings.add(greens);
        settings.add(blues);
        settings.add(generate);
        settings.setVisible(true);
        generate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int w = Integer.parseInt(width.getText());
                int h = Integer.parseInt(height.getText());
                boolean R = reds.isSelected();
                boolean G = greens.isSelected();
                boolean B = blues.isSelected();
                square = new JPanel[w][h];
                for(int i = 1; i < Integer.parseInt(width.getText()); i++) {
                    for(int j = 1; j < Integer.parseInt(height.getText()); j++) {
                        square[i][j] = new JPanel();
                        square[i][j].setBackground(Color.black);
                        panel.add(square[i][j]);
                        square[i][j].setVisible(true);
                    }
                }
                paint(w, h, R, G, B);
                colRand.setSize(w * 10, h * 10);
            }
        });
        panel.setBackground(Color.black);
        colRand.add(panel);
        colRand.add(settings, BorderLayout.SOUTH);
        colRand.pack();
        colRand.setVisible(true);
    }

    public static void paint(int w, int h, boolean reds, boolean greens, boolean blues) {
        for(int i = 1; i < w; i++) {
            for(int j = 1; j < h; j++) {
                square[i][j].setBackground(randColor(reds, greens, blues));
            }
        }
    }

    public static Color randColor(boolean reds, boolean greens, boolean blues) {
        int R, G, B;

        R = (int)(Math.random() * 255);
        G = (int)(Math.random() * 255);
        B = (int)(Math.random() * 255);

        if(reds == false) {
            R = 0;
        }
        if(greens == false) {
            G = 0;
        }
        if(blues == false) {
            B = 0;
        }
        return new Color(R, G, B);
    }
}

Please help me i've been struggeling for a long time.

Rob
  • 11,446
  • 7
  • 39
  • 57
JProg
  • 27
  • 3

3 Answers3

4

You program works, just that you didn't clear the panel from the previous run. Scroll down and you'll see it. Add panel.removeAll(); to your actionPerformed

0

You probably want to add:

panel.removeAll();

somewhere in actionPerformed to clear the previous panels. Also, when you paint, you should call JPanel#revalidate and JPanel#repaint so the panel will be actually repainted.

Community
  • 1
  • 1
kentaro
  • 1,249
  • 2
  • 13
  • 19
0

JPanel has a method called revalidate that will redraw the underlying drawing context for that control. After you draw, call revalidate.

Gabriel Reiser
  • 402
  • 5
  • 10