2

I am new to Java, so I am making a simple program that outputs different randomly colored pixels in a window, and continually updates. The program will output the original array of pixels, but it doesn't continually change. Possible problems I expect:

  • The repaint() isn't correct.
  • The array is not correctly generating new random numbers

Here is the code:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Renderer extends Applet {

    static int[][] tilemap;
    static int rows, columns;

    @Override
    public void init() {
        setSize(800, 480);
        setBackground(Color.BLACK);
        createTilemap();

    }

    @Override
    public void paint(Graphics g) {

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                // int mod_i = 16 * i;
                // int mod_j = 16 * j;
                switch (tilemap[i][j]) {
                case 0:
                    g.setColor(Color.RED);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;
                case 1:
                    g.setColor(Color.BLUE);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;
                case 2:
                    g.setColor(Color.YELLOW);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;
                case 3:
                    g.setColor(Color.WHITE);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;
                case 4:
                    g.setColor(Color.GREEN);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;
                case 5:
                    g.setColor(Color.CYAN);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;
                case 6:
                    g.setColor(Color.MAGENTA);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;
                case 7:
                    g.setColor(Color.ORANGE);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;
                case 8:
                    g.setColor(Color.PINK);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;
                case 9:
                    g.setColor(Color.DARK_GRAY);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;
                case 10:
                    g.setColor(Color.BLACK);
                    // g.fillRect(mod_i, mod_j, 16, 16);
                    g.fillRect(i, j, 16, 16);
                    break;

                }

            }

        }

    }

    public static void createTilemap() {

        tilemap = new int[800][480];
        rows = tilemap.length;
        columns = tilemap[1].length;

        Random r = new Random();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                tilemap[i][j] = r.nextInt(11);
            }
        }
    }

    public void run() {
        char condition = 'y';

        while (condition != 'n') {
            createTilemap();
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
  • Why do you want to create a `16 x 16` flashing series of random colors? Without a pause? – Elliott Frisch Nov 29 '13 at 02:24
  • 2
    You should `javax.swing.Timer` instead of `Thread.sleep()`. See [This answered question](http://stackoverflow.com/questions/7816585/program-freezes-during-thread-sleep-and-with-timer) – Paul Samsotha Nov 29 '13 at 02:25
  • Not quite related to your question, I know, but how do you expect `condition` ever to be anything other than `'y'`? – Dawood ibn Kareem Nov 29 '13 at 02:43
  • Also, if you put your `Color` objects in an array, that whole switch/case statement will be reduced to just two lines. – Dawood ibn Kareem Nov 29 '13 at 02:46
  • 1
    1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Nov 29 '13 at 03:00

1 Answers1

2

You need to include the line

super.paint( g );

in your paint method, otherwise your display won't actually change.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110