I have the code
import java.awt.*;
import javax.swing.*;
public class MondrianPanel extends JPanel
{
public MondrianPanel()
{
setPreferredSize(new Dimension(200, 600));
}
public void paintComponent(Graphics g) {
for(int i = 0; i < 20; i++) {
paint(g);
}
}
public void paint(Graphics g)
{
Color c = new Color((int)Math.random()*255, (int)Math.random()*255, (int)Math.random()*255);
g.setColor(c);
g.fillRect((int)Math.random()*200, (int)Math.random()*600, (int)Math.random()*40, (int)Math.random()*40);
}
}
What I'm trying to get it to do is draw a bunch of randomly colored rectangles at random places on the screen. However, when I run it, I just get a gray box. I was reading this question Drawing multiple lines with Java Swing and I saw that you should have a single paintComponent that calls paint a bunch of times and I tried adapting my code to this, but it still doesn't work.