Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
Like Tree3Likes

Thread: 'sleep' not working well

  1. #21
    rng
    rng is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: 'sleep' not working well

    Ok. Thanks. I tried following code. It works but again previous line is being erased. Lines are not adding on the screen, but only one line is remaining on screen.
    Java Code:
    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Insets;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.Timer; 
    import java.util.Random;
      
    public class Points extends JPanel  {
        int counter=0; 
        Timer tm; 
        int x,y; 
        Random r = new Random();
       
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
      
          Graphics2D g2d = (Graphics2D) g;
      
          g2d.setColor(Color.blue);
      
          Dimension size = getSize();
          Insets insets = getInsets();
      
          int w =  size.width - insets.left - insets.right;
          int h =  size.height - insets.top - insets.bottom;
          x = Math.abs(r.nextInt()) % w;
          y = Math.abs(r.nextInt()) % h;
          g2d.drawLine(x, y, x+5, y+5);
      }
      
      public static void main(String[] args) {
          JFrame frame = new JFrame("Points");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Points pts = new Points(); 
          pts.new DataStructure(); 
          frame.add(pts);
          frame.setSize(750, 600);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);  
      }
      private class DataStructure   implements ActionListener{
    	public DataStructure(){
    	    	tm = new Timer(500, this); 
    		tm.start(); 	
    		counter++; if (counter>5) System.exit(0); 
    	}
    	public void actionPerformed(ActionEvent e)  { repaint(); }
      }
    }
    And it is not exiting after 5 cycles. It just continues...

  2. #22
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: 'sleep' not working well

    You're not doing what I meant. When I say data structure, I simply mean a List, or a Set, or an array. The Timer will add things to be drawn to that data structure. The paintComponent method will simply draw everything in that data structure.

    Start with the data structure, forget about the Timer for a second. Create a List or an array that contains information to be drawn for multiple items. In your paintComponent method, simply iterate over your data structure and draw each thing. When you have that working, it should be easy to implement a Timer that adds to that data structure.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: 'sleep' not working well

    Or another option is to draw into a BufferedImage and call repaint making sure that the paintComponent method draws the BufferedImage.
    KevinWorkman likes this.

  4. #24
    rng
    rng is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: 'sleep' not working well

    As instructed by you, following is the code:
    Java Code:
    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Insets;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.Timer; 
    import java.util.Random;
      
    public class Points extends JPanel implements ActionListener {
        Timer tm; 
        int x,y; 
        Random r = new Random();
        int xpoint[], ypoint[];
        int WIDTH=750; int HEIGHT=600; 
       
       public Points(){
      	xpoint = new int[100];
      	ypoint = new int[100];
      	for (int i=0; i<100; i++){
    	      	xpoint[i] = Math.abs(r.nextInt()) % WIDTH;
    	      	ypoint[i] =  Math.abs(r.nextInt()) % HEIGHT;
    	}
     	tm = new Timer(500, this); 
    	tm.start(); 	
       }
       
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
          g2d.setColor(Color.blue);
          Dimension size = getSize();
          Insets insets = getInsets();
          WIDTH =  size.width - insets.left - insets.right;
          HEIGHT =  size.height - insets.top - insets.bottom;
          for (int i=0; i<100; i++)
          	  g2d.drawLine(xpoint[i], ypoint[i], xpoint[i]+10, ypoint[i]+10);
      }
    
      public void actionPerformed(ActionEvent e){
      }
      
      public static void main(String[] args) {
          JFrame frame = new JFrame("Points");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Points pts = new Points(); 
          frame.add(pts);
          frame.setSize(750, 600);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);  
      }
    }
    But it produces all the lines immediately and at the same time, no delay between drawing of lines. I suspect I have not put timer or not started timer at the right place.
    Last edited by rng; 01-19-2012 at 03:24 AM.

  5. #25
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: 'sleep' not working well

    Quote Originally Posted by rng View Post
    But it produces all the lines immediately and at the same time, no delay between drawing of lines. I suspect I have not put timer or not started timer at the right place.
    Yes, that is what is expected. Now, make it so your array starts with nothing in it. Then in your Timer, add one thing to it each step and call repaint. You might want to use a List and/or the Point class. Parallel arrays are usually a bad idea.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #26
    rng
    rng is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: 'sleep' not working well

    Will following work:
    Java Code:
    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Insets;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.Timer;
    import java.util.Random;
       
    public class Points extends JPanel implements ActionListener {
        Timer tm;
        int x,y;
        Random r = new Random();
        int xpoint[], ypoint[];
        int counter=0; 
        int WIDTH=750; int HEIGHT=600;
        
       public Points(){
        xpoint = new int[100];
        ypoint = new int[100];
        xpoint[counter] = Math.abs(r.nextInt()) % WIDTH;
        ypoint[counter] =  Math.abs(r.nextInt()) % HEIGHT;
        repaint(); 
        counter++; if (counter>100) System.exit(0); 
        tm = new Timer(500, this);
        tm.start();    
       }
        
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
          g2d.setColor(Color.blue);
          Dimension size = getSize();
          Insets insets = getInsets();
          WIDTH =  size.width - insets.left - insets.right;
          HEIGHT =  size.height - insets.top - insets.bottom;
          for (int i=0; i<100; i++)
              g2d.drawLine(xpoint[i], ypoint[i], xpoint[i]+10, ypoint[i]+10);
      }
     
      public void actionPerformed(ActionEvent e){
      }
       
      public static void main(String[] args) {
          JFrame frame = new JFrame("Points");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Points pts = new Points();
          frame.add(pts);
          frame.setSize(750, 600);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true); 
      }
    }
    "Then in your Timer, add one thing to it each step " : I can't understand how to add something in the timer apart from adding the object as 'this'. You will have to write a couple of lines of code please.
    Last edited by rng; 01-19-2012 at 09:50 AM.

  7. #27
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: 'sleep' not working well

    Quote Originally Posted by rng View Post
    You will have to write a couple of lines of code please.
    Poor choice of words. We don't "have" to do anything. We're all doing this voluntarily, for free. We've already told you exactly what to do, several times.

    You already know how to add stuff to your array. Only now, instead of adding everything at the beginning in a for loop, you want to add them one at a time, one for each time the Timer fires.

    Alternatively, you could fill the array at the beginning like you've already done, and simply keep track of an index that tells the paintComponent() method how many things to draw from the array. Then just increment that index in your Timer.
    Fubarable likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #28
    rng
    rng is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: 'sleep' not working well

    I think I am getting it now. Will post back when complete. Thanks.

  9. #29
    rng
    rng is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: 'sleep' not working well

    Following code is working, but after a few seconds, error message appears on the console:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 114
    	at Points.paintComponent(Points.java:57)
    	at javax.swing.JComponent.paint(JComponent.java:1029)
    	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5138)
    Java Code:
    //works well; 
    
    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Insets;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.Timer;
    import java.util.Random;
       
    public class Points extends JPanel implements ActionListener {
        Timer tm;
        int x,y;
        Random r = new Random();
        int xpoint[], ypoint[];
        int counter=0; 
        int WIDTH=750; int HEIGHT=600;
        
      public static void main(String[] args) {
          JFrame frame = new JFrame("Points");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(750, 600);
          frame.setResizable(false); 
          frame.setLocationRelativeTo(null);
          Points pts = new Points();
          frame.add(pts);
          frame.setVisible(true); 
      }
        
       public Points(){
    	    xpoint = new int[100];
    	    ypoint = new int[100];
    	    for (int i=0; i<100; i++){
    		    xpoint[i] = Math.abs(r.nextInt()) % WIDTH;
    		    ypoint[i] =  Math.abs(r.nextInt()) % HEIGHT;
    	    }
    	    tm = new Timer(100, this);
    	    tm.start();    
       }
        
       public void paintComponent(Graphics g) {
          Graphics2D g2d = (Graphics2D) g;
          g2d.setColor(Color.blue);
          Dimension size = getSize();
          Insets insets = getInsets();
          int newwidth =  size.width - insets.left - insets.right;
          int newheight =  size.height - insets.top - insets.bottom;
          if (newwidth != WIDTH || 	newheight != HEIGHT){
          	WIDTH  = newwidth; 
          	HEIGHT = newheight; 
          	super.paintComponent(g);
          }
          g2d.drawLine(xpoint[counter], ypoint[counter], xpoint[counter]+10, ypoint[counter]+10);
      }
      
      public void actionPerformed(ActionEvent e){
      	repaint(); 
      	counter++; 
      }
         
    }
    Why should ArrayIndexOutOfBoundsException occur here?
    -------------------------------------------------------------------------
    Edit: It is a case of runaway 'counter'. The counter is incrementing without any logic to stop it.
    Thanks everyone for your help.
    Last edited by rng; 01-20-2012 at 03:01 PM.

  10. #30
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: 'sleep' not working well

    You've got nothing in there that stops you incrementing the counter once you have drawn everything.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Thread.sleep
    By Gog in forum New To Java
    Replies: 3
    Last Post: 01-13-2011, 09:14 AM
  2. Do I have to put Thread to sleep?
    By atch in forum New To Java
    Replies: 0
    Last Post: 03-08-2010, 11:21 AM
  3. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 03:27 PM
  4. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 03:56 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
  •