Search code examples
javaappletawtpaintrepaint

Program Doesn't Repaint?


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();
            }
        }
    }

}

Solution

  • You need to include the line

    super.paint( g );
    

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