Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Timed matrix generation

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Timed matrix generation

    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
     
        }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Timed matrix generation

    Note that you are calling repaint() prior to the JFrame (and its contents) being visible. To accomplish the animation as I understand it, you need some type of threading or Timer. For instance, the java.swing.Timer will allow you to update the matrix and repaint at a certain time interval. A new Thread will allow you to do the same (although, you should understand Swing and concurrency should you take this route)

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,365
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Timed matrix generation

    After you call repaint() the code must give the JVM's EDT control so it can call the paintComponent() method to draw the new stuff.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Dec 2013
    Posts
    37
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Timed matrix generation

    I removed the generator() function and wrote "GUI.generator()" to after the declaration of the JFrame and it seems to be working as I intended now, thanks!
    I do not know about the swing timer ,multithreading or the EDT control and i'm planning to study all that in the holidays before my second semester starts .
    I just wanted to get this running to play with diffrent patterns while I study those topics so thanks for the help.

    Also I found instructions on how to make nanosecond delays using:
    long start = System.nanoTime();
                 while(start + 10000 >= System.nanoTime());

    Pretty fun stuff

Similar Threads

  1. Timed thread to poll servers
    By rockking8350 in forum Java Networking
    Replies: 16
    Last Post: April 7th, 2013, 07:41 PM
  2. Knapsack problem , check if matrix can fill list of smaller matrix list.
    By ofirattia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 12:20 PM
  3. TIMED JAVA APPLICATION
    By maingate in forum Java Theory & Questions
    Replies: 7
    Last Post: December 4th, 2012, 01:44 PM
  4. Replies: 6
    Last Post: October 3rd, 2012, 06:39 PM
  5. how to implement timed out event in java?!
    By migongotar in forum Java Theory & Questions
    Replies: 2
    Last Post: August 9th, 2011, 05:56 AM