I have a GUI class Gui:
public class Gui extends JFrame implements Runnable
{
private JPanel outer, inner;
private JLabel[][] labels = new JLabel[22][12];
private Color[][] defaultMap, map;
Thread t;
private int row, col;
private Color color;
public Gui()
{
Container content = getContentPane();
content.setLayout(new BorderLayout());
setBackground(Color.BLACK);
setSize(1000, 1000);
setLocation(300, 0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
defaultMap = createMap();
draw(defaultMap);
}
public Color[][] createMap()
{
Color[][] map = new Color[22][12];
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 12; j++)
{
map[i][j] = Color.WHITE;
}
}
for (int i = 0; i < 22; i++)
{
map[i][0] = Color.GRAY;
map[i][11] = Color.GRAY;
}
for (int i = 0; i < 12; i++)
{
map[0][i] = Color.GRAY;
map[21][i] = Color.GRAY;
}
return map;
}
public void draw(Color[][] map)
{
outer = new JPanel();
outer.setLayout(new BorderLayout());
outer.setBackground(Color.WHITE);
outer.setPreferredSize(new Dimension());
inner = new JPanel();
inner.setLayout(new GridLayout(22, 12, 2, 2));
inner.setBackground(Color.BLACK);
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 12; j++)
{
labels[i][j] = new JLabel();
JLabel label = labels[i][j];
label.setPreferredSize(new Dimension(20, 20));
label.setBackground(map[i][j]);
label.setOpaque(true);
inner.add(label);
}
}
add(outer);
add(inner);
pack();
}
public void move(int row, int col, Color color)
{
System.out.println(row+","+col);
map = defaultMap;
map[row][col] = color;
t = new Thread(this);
t.start();
}
@Override
public void run()
{
draw(map);
}
}
Which is called from my main class like so:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
gui = new Gui();
gui.setVisible(true);
gui.move(2,5,Color.GREEN);
Thread.sleep(1000);
gui.move(3,5,Color.GREEN);
Thread.sleep(1000);
gui.move(4,5,Color.GREEN);
} catch (InterruptedException ex)
{
Logger.getLogger(Tetris.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
So the weird stuff is happening when the gui.move() function is called. You can ignore the rest or use it if it helps. But each time, a green block should be "added" to the gui at 2,5; 3,5; and 4,5; 1 second after the other.
The issue:
The Gui remains black for a time then immediately repaints/updates with the proper grid and colors with the first two blocks correctly colored green but it's missing the last block at 4,5. Again, the initial "defaultMap" should be painted immediately, but it is not, the JFrame is black until everything is painted together at once minus the last green block. Then each green block should be painted on 1 second after the other.
The interesting part is that the System.out.println() bit in the move method in Gui prints out the row and col as I'd expect it to... They come out about one second after the other in the terminal. So that tells me that something is going right. But I'm not sure what's going on with the Gui...
EDIT: slight difference in story. upon closer examination, I noticed that the last green block does appear for a second as soon as the entire map is painted but immediately "disappears" repainted white.