Hi, could some one look over my code I by myself dont get where the flaw is. It should paint 1 cube at a time but now it runs all the code through and paints all at once.

package net.viped;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

class Tetris extends JPanel {

    private int[][] occupied = new int[10][20];

    public Tetris() {
        addKeyListener(new ListenKeys());
        setFocusable(true);
        initGame();

    }

    public void initGame() {
        for (int i = 0; i < occupied.length; i++) {
            for (int j = 0; j < 20; j++) {
                occupied[i][j] = 0;
            }
        }

    }

    public void paint(Graphics g) {
        for (int i = 0; i < occupied.length; i++) {
            for (int j = 0; j < occupied[0].length; j++) {
                if (occupied[i][j] == 1) {
                    g.setColor(Color.black);
                    g.fillRect(i*24, j*24, 24, 24);
                    g.setColor(Color.red);
                    g.fillRect(i*24+1, j*24+1, 22, 22);
                } else {

                    g.setColor(Color.black);
                    g.fillRect(i*24, j*24, 24, 24);
                }
            }
        }
    }

    public void testPaints() {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 20; j++) {
                System.out.println(i + " " + j);
                occupied[i][j] = 1;
                try {   

                    Thread.sleep(10); 
                    repaint();

                } catch (Exception e) {}
            }
        }


    }

    public class ListenKeys extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_R) {
                testPaints();
            }
        }
    }


    public static void main(String[] args) {
        JFrame window = new JFrame();
        Tetris tetris = new Tetris();
        window.setSize(800, 600);   
        window.add(tetris);
        window.setVisible(true);
        window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
    }
}

window.pack to main method and it is working. Sorry for a unnecessary post.

I doubt very much that your key listener is working properly. Unless you have other code changes it will still have the "updates eveything at once at the end" symptom.

Everything to do with Swing (eg painting the screen, running actionPerformed methods) happens on a single thread - the "Event Dispatch Thread", or "EDT", or "Swing thread".
That means that once your KeyAdaptor method starts NOTHING else will happen in Swing, including no screen updates, until your method finishes. You can update stuff and loop and sleep as much as you like, but none of that will affect what's on the screen until your method has terminated.
If you want to do stuff in steps over a period of time the this is the right way:
Start a javax.swing.Timer. Every time the timer fires you can update whatever needs updating and return. Inbetween the timer firings Swing will be free to update the screen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.