For a school project, I need to create an applet that produces a 10 x 10 grid in which each cell will change color in accordance to what some threads are doing in the background. I have all of the rest figured out, but I don't have the slightest clue as to how to display this grid. This is the only example code we were given:
import java.awt.*;
import java.applet.Applet;
public class Array2 extends Applet {
private final ststic int LIMIT = 9;
private int[][] results;
public void init() {
int count = 1;
results = new int [LIMIT][LIMIT];
for (int i = 0; i < LIMIT; i++) {
for (int j = 0; j < LIMIT; j++) {
results[i][j] = count % 2;
count++;
}
}
}
public void paint (Graphics g) {
int xLoc = 25;
int yLoc = 25;
for (int i = 0; i < LIMIT; i++) {
for (int j = 0; j < LIMIT; j++) {
g.drawString(Integer.toString(results[i][j]), xLoc. yLoc);
xLoc += 20;
}
xLoc = 25;
yLoc += 20;
}
}
}
This ends up printing a blank 2 x 2 grid. This is easy enough to modify into a 10 x 10. However, what I DON'T know how to do is color the squares. Everything I've searched mentions using jPanels or jFrames or something, but this HAS to be an applet. I was just looking for some suggestions as to what I should look into for the coloring process, as this is literally all I have to go on.Thanks!
The applet draws with the class Graphics and passes you an instance in the paint
method. You can use Graphics
to do many cool things on the screen, so check its methods out! But to draw a colored square, first set the color using g.setColor(color)
and then use g.fillRect(xLoc, yLoc, size, size)
with xLoc and yLoc being the top-left coordinates of the square.