help changing the RGB of a fillRect
I have written this program that makes a JPanel that creates random black and white pixels using a fillRect but now I would like to make random colors instead. How would I set the RGB values of an individual fillRect?
Code:
import java.util.Random;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Basic extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics = (Graphics2D) g;
graphics.setColor(Color.black);
Dimension size = getSize();
Random number = new Random();
int image[][];
int a = 0;
for (int x = 0; x < size.width; x++) {
for (int y = 0; y < size.height; y++) {
a = number.nextInt(2);
graphics.fillRect(x, y, a, a);
}
}
}
public static void main(String[] args) {
Basic basic = new Basic();
basic.setPreferredSize(new Dimension(400, 400));
JFrame frame = new JFrame("Basic");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(basic);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}