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