Hello
I've been playing around with 2D graphics in Java and I want to make a timed matrix generator using Thread.sleep() .
Simply put , I have a matrix of squares which is 20x20 blue squares and I want to fill each square in order at 10ms intervals.
I tried doing that using the following code but the repaint() doesn't work for some reason. It just waits until the whole matrix is updated and then it repaints .
Can someone tell me why it's not working?
You can see in the method "Generator" it updates the specific square with a boolean value , sleeps for 10 ms and then performs the repaint();
But for some reason the repaint() is not called until the whole thing is done.
Thanks in advance
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GUI extends JPanel implements MouseListener, MouseMotionListener { boolean[][] Matrix; public static void main(String[] args) throws InterruptedException { GUI gui = new GUI(); JFrame f = new JFrame("graphix"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Finishes program when Cross is pressed on window f.setSize(815,835); // Size of main window f.setVisible(true); // Has to be set true to be visible f.add(gui); // Adds instance of the user interface class } public GUI() throws InterruptedException { Matrix = new boolean[20][20]; generator(); } public void generator() throws InterruptedException{ for(int x=0; x < 20; x ++){ for(int y=0; y< 20; y++){ Matrix[x][y] = true; Thread.sleep(10); repaint(); } } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); this.setBackground(Color.BLACK); // Window Background g.setColor(new Color(0, 0, 255)); for (int x = 0; x < 20; x++) { for (int y = 0; y < 20; y++) { if (Matrix[x][y]) { g.fillRect(x * 40, y * 40, 38, 38); } } // end of for y } // end of for x }