0

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!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Estol
  • 3
  • 2
  • Why not using g.setColor() method – Fady Saad Apr 08 '17 at 02:57
  • Start by having a look at the [2D Graphics trail](https://docs.oracle.com/javase/tutorial/2d/TOC.html) – MadProgrammer Apr 08 '17 at 04:46
  • 1
    [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free) – MadProgrammer Apr 08 '17 at 07:06
  • Something [like this](http://stackoverflow.com/questions/34036216/drawing-java-grid-using-swing/34036397#34036397) might be a start – MadProgrammer Apr 08 '17 at 07:09
  • [That's another way](http://stackoverflow.com/questions/14742069/java-how-to-paint-rectangles-on-mouseclick/14742199#14742199) and [that's another way](http://stackoverflow.com/questions/15891250/creating-a-multicolored-board/15891779#15891779) – MadProgrammer Apr 08 '17 at 07:11
  • 1) Please refer the teacher specifying 'applet' to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). See also the two links in one comment offered by @MadProgrammer. 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 3) The app. uses neither `JPanel` nor `JFrame` (which are Swing based, BTW). Don't add the tags. – Andrew Thompson Apr 09 '17 at 06:48
  • *"Everything I've searched mentions using jPanels or jFrames or something, but this HAS to be an applet."* A `JPanel` can be used in the Swing based [`JApplet`](http://docs.oracle.com/javase/8/docs/api/javax/swing/JApplet.html). – Andrew Thompson Apr 09 '17 at 06:50
  • Yeah, I've been finding that testing any changes is incredibly difficult, as Java security is so strict now and most web browsers don't have plugin support anymore. I've given these links a look, and I think it'll be a lot easier if I just use Swing. Thanks AndrewThompson and @MadProgrammer – Estol Apr 10 '17 at 19:47

2 Answers2

0

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.

Albert
  • 337
  • 1
  • 10
0

Albert provided me with the Graphics methods needed to finish this up as an applet. However after reading through the comments and links provided, it looks like I'll just be using Swing instead of AWT.

Estol
  • 3
  • 2