Results 1 to 11 of 11

Thread: repaint every

  1. #1
    3xpr1ment is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default repaint every

    Hi all

    I want a frame to repaint a JFrame every x msecs. I put a thread.sleep(x) but this makes the whole thread sleep for this time. This causes a mouseMotionListener not to take input while the thread is sleeping. I need the coordinates of the listener to be in the calculation of the next cycle so the method above is not appropriate here. Is there a way to say to the repaint method to wait x time without making the whole thread to sleep?

    Thank you for your help

    Java Code:
    class MyDrawPanel extends JPanel { 
      public static Plegma plegmaObject = new Plegma();
      
      
      public void paintComponent(Graphics g) { 
        
        for (int i=1; i<MainClass.columns; i++){
          for (int j=1; j<MainClass.rows; j++){
            if (plegmaObject.plegma[i][j].isOn == 1){
              g.setColor(Color.orange);
              g.fillRect(plegmaObject.plegma[i][j].x,plegmaObject.plegma[i][j].y, Automaton.size, Automaton.size);  
            }
            else{
              g.setColor(Color.white);
              g.fillRect(plegmaObject.plegma[i][j].x,plegmaObject.plegma[i][j].y, Automaton.size, Automaton.size);
            } 
          }
        }  
        plegmaObject.calcNextStep(); 
        repaint();
        try{
          Thread.sleep(500);
        }
        catch(InterruptedException ie){
        }
      }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If called in the Event Dispatch Thread, or EDT, Thread.sleep(...) will put the entire Swing app to sleep since the EDT is the only thread that Swing uses to do painting and user interaction. So use a Swing Timer instead as this won't put the EDT to sleep.

    Also, your use of a static variable above is a little suspect. I'd avoid using static anything unless you have a very good reason to do so.
    Last edited by Fubarable; 03-20-2010 at 01:02 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderator comment: Since this is a Swing topic, I'm moving this thread to the Swing forum.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default

    And never ever call repaint() from within a painting method override.

    db

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Java Code:
    new Thread(
        new Runnable() {
            public void run() {
                while (true) {
                    repaint();
                    try {
                        Thread.sleep(framerate);
                    }
                    catch (InterruptedException ie) {
                        System.err.println("Error: "+ie);
                        ie.printStackTrace();
                    }
                }
            }
        }
    ).start();
    I die a little on the inside...
    Every time I get shot.

  6. #6
    3xpr1ment is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    mmmm. I haven't read threads yet (I started learning 1 month ago) and seems essential for this. I will read threads and i will get back to this application.

    Thank you again for your help

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Supamagier View Post
    Java Code:
    new Thread(
        new Runnable() {
            public void run() {
                while (true) {
                    repaint();
                    try {
                        Thread.sleep(framerate);
                    }
                    catch (InterruptedException ie) {
                        System.err.println("Error: "+ie);
                        ie.printStackTrace();
                    }
                }
            }
        }
    ).start();
    I respectfully disagree with this solution as I believe that directly using a separate thread here is a bit of over-kill. Instead, and again, use a Swing Timer. While this also uses a separate thread, it does so behind the scenes, and it is built specifically so that timed events will be called on the EDT, thus eliminating many worries. It's also a lot easier to start, stop, and to detect if it's running.

    For e.g.:
    Java Code:
    new Timer(500, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        plegmaObject.calcNextStep();
        repaint();
      }
    }).start(); // code not compiled and not tested

    Note: ignore what I just stated if plegmaObject.calcNextStep(); is very CPU and time-intensive. If so, then this should be called from within a SwingWorker background thread.
    Last edited by Fubarable; 03-20-2010 at 05:28 PM.

  8. #8
    3xpr1ment is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    Ok. i got it working now. I just have two things unclear. I now have put the repaint method in the class i create the frame.

    Java Code:
    public class AutomataGUI implements MouseMotionListener {
    	
        public void show(){
        	
        	
            JFrame frame = new JFrame("Cellular Automata");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            MyDrawPanel panel = new MyDrawPanel();
            frame.setSize(754, 754);
            frame.setResizable(false);
            frame.addMouseMotionListener(this);
           
           
            frame.getContentPane().add(panel);
            frame.setVisible(true);
            
            while (true) {
            	panel.repaint();
    			try{
    				Thread.sleep(100);
    			}
    			catch(InterruptedException ie){
    			}
            }         
        }
    Is it the right place to put repaint()? And isnīt a bit strange that this doesnt interupt the MouseMotionListener methods.

    The second and most important:
    Also, your use of a static variable above is a little suspect. I'd avoid using static anything unless you have a very good reason to do so.
    How can i access the variables of an instance of a class (an object) from another class without declaring them static.

    Thank you again

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by 3xpr1ment View Post
    Ok. i got it working now. I just have two things unclear. I now have put the repaint method in the class i create the frame.

    Java Code:
    public class AutomataGUI implements MouseMotionListener {
    	
        public void show(){
        	
        	
            JFrame frame = new JFrame("Cellular Automata");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            MyDrawPanel panel = new MyDrawPanel();
            frame.setSize(754, 754);
            frame.setResizable(false);
            frame.addMouseMotionListener(this);
           
           
            frame.getContentPane().add(panel);
            frame.setVisible(true);
            
            while (true) {
            	panel.repaint();
    			try{
    				Thread.sleep(100);
    			}
    			catch(InterruptedException ie){
    			}
            }         
        }
    Is it the right place to put repaint()? And isnīt a bit strange that this doesnt interupt the MouseMotionListener methods.
    Again, use a Swing timer as what you're currently doing is at great risk of breaking.

    The second and most important:
    How can i access the variables of an instance of a class (an object) from another class without declaring them static.
    Sorry, but this doesn't make sense to me. I've had no problem using variables in other classes without using a static modifier. Please show me a simple case where use of an instance variable fails.

  10. #10
    3xpr1ment is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    public class One { //plegma
    	
    	public int x = 2;
    		
    }
    Java Code:
    public class Two {  
    			
    	public void changex(){
    		One.x = 5;
    	}
    }
    Java Code:
    public class MainClass {
    
        public static void main(String[] args) {
        	
        	Two two = new Two();
    		two.changex();
       	}
    	
    }
    This wont compile: Two.java:4: non-static variable x cannot be referenced from a static context
    One.x = 5;

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by 3xpr1ment View Post
    Java Code:
    public class One { //plegma
    	public int x = 2;
    }
    Java Code:
    public class Two {  
    	public void changex(){
    		One.x = 5;
    	}
    }
    Java Code:
    public class MainClass {
    
        public static void main(String[] args) {
        	Two two = new Two();
    		two.changex();
       	}
    }
    This wont compile: Two.java:4: non-static variable x cannot be referenced from a static context
    One.x = 5;
    The solution is not to use static but to use OOP techniques. For instance you should give the one class a way for two to change its state:
    Java Code:
    public class MainClass {
    
      public static void main(String[] args) {
    
        Two two = new Two();
        two.changex();
      }
    
    }
    
    class Two {
    
      public void changex() {
        One one = new One();
        one.setValue(5);
        System.out.println("one's value is: " + one.getValue());
      }
    }
    
    class One { //plegma
    
      public int x = 2;
    
      public void setValue(int i) {
        x = i;
    
      }
    
      public int getValue() {
        return x;
      }
    
    }

    If you don't have a good book on object oriented programming in Java, now's the time to get one. Much luck.

Similar Threads

  1. Repaint problem
    By citizenXL in forum New To Java
    Replies: 4
    Last Post: 10-28-2009, 03:02 PM
  2. Trying to do a simple repaint
    By IYIaster in forum New To Java
    Replies: 9
    Last Post: 10-14-2009, 10:30 PM
  3. Repaint() not working
    By Catkill in forum AWT / Swing
    Replies: 3
    Last Post: 09-09-2009, 10:51 PM
  4. repaint() problems
    By Emily1100125 in forum AWT / Swing
    Replies: 5
    Last Post: 02-03-2009, 04:11 PM
  5. Help with repaint() command
    By GeoffTK in forum New To Java
    Replies: 2
    Last Post: 11-26-2008, 04:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •