Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By jim829

Thread: Bouncing Balls Program, List balls do not move

  1. #1
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Bouncing Balls Program, List balls do not move

    I am trying to create a program that throws a ball at a random location, then it bounces around the box until gravity takes place and the ball falls for good. My program right now will throw the first ball and it moves fine, then when I go to throw the next ball that ball appears in a random location, but does not move. Also my applet rapidly blinks after I throw the first ball causing it to be difficult to even see the other balls on the screen. Sometimes the blinking completely covers them for some reason.

    Java Code:
    	public void move(){
    		
    		speedY = (speedY + GRAVITY); // incorporate gravity
    
    		x = x + speedX; // incorporate speed with location
    
    		y = y + speedY; // incorporate speed with location
    
    		barrier(); // check if ball is out of bounds
    
    	}
    Java Code:
    	public void takeNotice()
    	{
    
    		if (ball!=null){	// only if there's a ball
    			ball.move(); 	// to update ball
    		}
    		
    		if(newBall != null){
    			newBall.move();
    		}
    		repaint();
    
    	}
    public double x,y,speedX,speedY;
    private ArrayList<Ball> list = new ArrayList<Ball>();
    
    
    
    	public void addBall() { // add a new Ball to the window
    		list.add(new Ball());
    	}
    
    
    
    	/**
    	 * 	On the click of the "Throw" button, we start the animation
    	 * 		(unless the animation is already going on)
    	 */
    	public void throwAction()
    	{
    
    		if(alarm==null){   	 			// if nothing is in alarm
    			alarm = new Alarm(this); 	// new thread. calling constructor
    			alarm.start(); 				// start thread
    			alarm.setPeriod(100); 		// sets period to 10th of a second
    		}
    
    		if (ball==null){				// if there's no ball
    			
    			ball = new Ball();			// create new ball
    		}
    		else{
    			
    			addBall();
    			
    		}
    
    	}
    
    	/**
    	 * The only "graphical" method of the class is the paint method.
    	 */
    	public void paint(Graphics pane)
    	{
    		setBackground(Color.pink);		//	Not so pretty.  Or is it?
    		if (throwButton != null)			//	We draw the buttons
    			throwButton.paint(pane);		//		(if instantiated)
    		if (fastButton != null)
    			fastButton.paint(pane);
    		if (resetButton != null)
    			resetButton.paint(pane);
    		if (slowButton != null)
    			slowButton.paint(pane);
    		if (removeButton != null)
    			removeButton.paint(pane);
    		if (ball!=null){					// if there's a ball
    			ball.paint(pane);				// paint ball
    		}
    		
    	    for (Ball newBall : list) {  
    	        // paint each Ball on the window's Graphics
    	        newBall.paint(pane);
    	        
    	        }
    
    		
    		// we paint barrier
    		pane.drawRect(UPPERLEFTRECX, UPPERLEFTRECY , WIDTH, HEIGHT);
    		
    	}
    Any help is appreciated

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Bouncing Balls Program, List balls do not move

    Blinking is usually a buffering problem. You probably need each ball executing in its own thread.

  3. #3
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Bouncing Balls Program, List balls do not move

    Quote Originally Posted by Junky View Post
    Blinking is usually a buffering problem. You probably need each ball executing in its own thread.
    I am not exactly sure what you mean by executing every ball in its own thread? How would I setup the throw button in different threads?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Bouncing Balls Program, List balls do not move

    You don't. You only have one button but each time it is clicked it spawns a new thread which controls the movement of a single ball.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,917
    Rep Power
    5

    Default Re: Bouncing Balls Program, List balls do not move

    You don't need multiple threads. I have a program which has many balls bouncing around off of each other and off of the sides of the walls. But you haven't included either an Short, Self Contained, Correct Example or sufficient code to demonstrate the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,263
    Rep Power
    6

    Default Re: Bouncing Balls Program, List balls do not move

    I see a red flag in the code - paint() is being overriden in stead of paintComponent(). Is this AWT or Swing?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Bouncing Balls Program, List balls do not move

    It is AWT

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,917
    Rep Power
    5

    Default Re: Bouncing Balls Program, List balls do not move

    Why are you using that? It is outdated. Swing does double buffering for you which may be your problem as already stated.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,263
    Rep Power
    6

    Default Re: Bouncing Balls Program, List balls do not move

    Swing is also outdated, but not as much as AWT is ;) AWT is in the retirement home, Swing has just turned 95% bald, JavaFX is the black sheep that has been tossed aside and JavaFX 2 is the current flagship, GUI-wise.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Bouncing Balls Program, List balls do not move

    Lol, I was just using it because that is what we used in our examples. So now I have the balls moving after they are thrown, I just need to figure out how to remove a ball when I click the remove button. Is there anyway to remove an item from a list one at a time? I am confused because I do not have any loops adding elements, so I cannot make a loop to remove them. If I try list.remove(ball) that does not work.

    Here I moved the move method to call after the newBall is painted (now the new balls move)
    Java Code:
    	public void paint(Graphics pane)
    	{
    		setBackground(Color.pink);		//	Not so pretty.  Or is it?
    		if (throwButton != null)			//	We draw the buttons
    			throwButton.paint(pane);		//		(if instantiated)
    		if (fastButton != null)
    			fastButton.paint(pane);
    		if (resetButton != null)
    			resetButton.paint(pane);
    		if (slowButton != null)
    			slowButton.paint(pane);
    		if (removeButton != null)
    			removeButton.paint(pane);
    		if (ball!=null){					// if there's a ball
    			ball.paint(pane);				// paint ball
    		}
    		
    	    for (Ball newBall : list) {  
    	        // paint each Ball on the window's Graphics
    	        newBall.paint(pane);
    	        newBall.move();	        
    	        }
    
    		
    		// we paint barrier
    		pane.drawRect(UPPERLEFTRECX, UPPERLEFTRECY , WIDTH, HEIGHT);
    		
    	}
    Java Code:
    		list.add(new Ball());
    	}
    	
    	/**
    	 * Removes a ball from the application
    	 */
    	public void removeAction()
    	{
    		list.remove(ball);
    	
    	}
    Java Code:
    	public void takeNotice()
    	{
    
    		if (ball!=null){	// only if there's a ball
    			ball.move(); 	// to update ball
    		}
    		
    	
    		repaint();
    
    	}
    public double x,y,speedX,speedY;
    
    
    	/**
    	 * 	On the click of the "Throw" button, we start the animation
    	 * 		(unless the animation is already going on)
    	 */
    	public void throwAction()
    	{
    
    		if(alarm==null){   	 			// if nothing is in alarm
    			alarm = new Alarm(this); 	// new thread. calling constructor
    			alarm.start(); 				// start thread
    			alarm.setPeriod(100); 		// sets period to 10th of a second
    		}
    
    		if (ball==null){				// if there's no ball
    			
    			ball = new Ball();			// create new ball
    		}
    		else{
    			
    			addBall();
    			
    		}
    
    	}

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,917
    Rep Power
    5

    Default Re: Bouncing Balls Program, List balls do not move

    I am curious about something. Are you actually drawing the buttons yourself or are you using buttons you added from the Button class? Even with AWT the components of a frame will be repainted for you.

    Regards,
    Jim
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. bouncing Balls
    By mystery81 in forum New To Java
    Replies: 4
    Last Post: 09-14-2012, 01:16 AM
  2. More bouncing balls...
    By ShaolinMunky in forum New To Java
    Replies: 5
    Last Post: 06-10-2012, 10:43 AM
  3. Replies: 2
    Last Post: 02-13-2012, 07:45 AM
  4. ArrayList and Bouncing Balls
    By jamie23 in forum AWT / Swing
    Replies: 1
    Last Post: 02-20-2011, 07:54 PM
  5. Multiple bouncing balls
    By Algar in forum AWT / Swing
    Replies: 2
    Last Post: 04-24-2008, 09:35 PM

Posting Permissions

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