Web Analytics Made Easy -
StatCounter New Thread per New Object - CodingForum

Announcement

Collapse
No announcement yet.

New Thread per New Object

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • New Thread per New Object

    Currently, I'm trying to make a number of ball objects from a class, and was wondering if there was a way to make a different thread for each to make each ball move at a different speed. The method that I use is the Thread.sleep function, however when I tried doing this with different integers for the sleep delay the balls all seemed to be moving at the same speed. Attached is a sample of my code:

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class balltest extends JFrame
    {
    	int width;
    	int height;
    	Ball a;	
    	Ball b;
    
    	public static void main(String args[])
    	{	
    		new balltest();
    	}
    	
    	public balltest()
    	{
    		width = 250;
    		height = 250;
    		a = new Ball(50, 50, 25, 25, 20);
    		b = new Ball(50, 50, 25, 25, 50); //this is 50 and not 20 because i wanted to see if they started at the same position of one would go faster
    		a.start();
    		b.start();
    		setSize(width, height);
    		setVisible(true);
    	}
    	
    	public void paint(Graphics g)
    	{
    		g.setColor(Color.BLACK);
    		g.fillRect(0,0, width, height);
    			
    		g.setColor(Color.RED);
    		g.fillOval(a.x, a.y, a.w, a.h);
    		g.fillOval(b.x, b.y, b.w, b.h);	
    				
    		try{Thread.sleep(10);}catch(Exception e){}
    		
    		a.move();
    		b.move();		
    		
    		repaint();
    	}
    	
    	class Ball extends Thread
    	{
    		int x, y, w, h; //X and Y location, width and height
    		int speed; //the integer for the thread
    		int sx; //speed X
    		int sy; //speed Y
    		Thread thread; //a new thread
    		
    		public Ball(int x, int y, int w, int h, int speed)
    		{
    			this.x = x;
    			this.y = y;
    			this.w = w;
    			this.h = h;
    			this.speed = speed;
    			sx = 10;
    			sy = 10;
    		}
    
    		public void move()
    		{
    			if(x < 0 || x > width)
    			{
    				sx = sx * -1;
    			}
    
    			if(y < 0 || y > height)
    			{
    				sy = sy * -1;
    			}
    			
    			try{thread.sleep(speed);}catch(Exception e){}
    						
    			x = x + sx;
    			y = y + sy;
    		}
    	}
    }
    If anyone spots anything missing vital for the code to work, please post a reply here.

  • #2
    I haven't spotted anything, but beign a begginer in java, I have this question:
    Shouldn't
    Code:
    a = new Ball(50, 50, 25, 25, 20);
    be like
    Code:
    Ball a = new Ball(50, 50, 25, 25, 20);
    I hope you get your question answered.

    EDIT: oh never mind, I see you declared "Ball a" and "Ball b" already. Sorry.

    Quick observation, could it be because you have gloval variables on the class Ball?
    Last edited by daniel_g; Oct 6, 2006, 12:01 AM.

    Comment


    • #3
      I'm not sure about this, and my intuition is that you shouldn't have to use threads to accomplish your goal. However, I've only played with threads in c and not java so I'm not well versed enough to comment on this.

      However, I copied and pasted your code as you supplied and ran it. What a saw was one ball (well really ball a drawn directly on top of ball b) and the speeds seemed to match exactly.

      But then I changed the starting x,y position of ball a to;
      a = new Ball(25, 25, 25, 25, 20);
      so that they'd start at different positions (I was really just testing that it was actually drawing two balls).

      What I observed with this change was two distinct balls and one was clearly moving faster than the other.

      The speed also seems to have an inverse affect. I changed the speed of ball b from 50 to 500 and observed that the balls were now moving considerably slower, as well the speed ratio between the 2 balls seemed unaffected.

      I'm not sure what this all means, or how you could fix it, but I posted in the hopes that maybe some of the information I provided might help.

      Good Luck.

      Comment


      • #4
        thread.sleep is a function that will delay the execution of whatever is happening, so the higher the value, the slower it will run.
        The change Gox did, did not change the speed of neither a or b, it looks like the rate is changing, but thats because ball a is colliding with ball b, plus its also colliding with a different part of the wall.

        Now, I did this small change to the code:
        Code:
        public static void main(String args[])
        	{	
        		balltest ball1 = new balltest(50, 50, 25, 25, 20);
        		balltest ball2 = new balltest(50, 50, 25, 25, 50);
        
        	   
        	}
        	
        	public balltest(int x, int y, int w, int h, int s)
        	{
        		width = 250;
        		height = 250;
        		a = new Ball(x, y, w, h, s);
        		b = new Ball(x, y, w, h, s); //this is 50 and not 20 because i wanted to see if they started at the same position of one would go faster
        		a.start();//......
        That will create the two balls on two windows. Note that they move at the same speed, but when you close one of the windows, the other one seems to move faster. Since this seems to happen with no matter which window you close, I suspect thread.sleep is just adding up the two speed values.
        Maybe thread.sleep just doesn't work the way you think it does?
        Last edited by daniel_g; Oct 6, 2006, 01:43 PM.

        Comment


        • #5
          possibly, what would be the best way to increase/decrease it's speed then?

          Comment


          • #6
            Actually, sleep.thread is probably good.

            I don't know much about threads, but I'm inclining to belive you only have one thread for the two balls. Maybe you should create a new instance of thread for each ball.

            Comment


            • #7
              That's what I tried, except it didn't work; or possibly I was doing it wrong. Any other ideas?

              Comment

              Working...
              X