DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2009

    Question Help rid ball trailing colors in balls bounce


    Please understand that I am new to Java and this is my homework assignment. Therefore please do not give me the coded answer. Please do give me hints, guidance to get me going in the right direction.

    My applet correctly displays balls of different colors traveling around the applet area. The number of balls is defined in the html file. The balls bounce off the applet "walls" as they should and pass through each other as they should. However, each ball leaves its trailing color.

    I have tried this in run():
    1. set the ball color to the background color.
    2. repaint
    3. move the ball
    4. set the ball color to its original color.
    5. repaint.

    This didn't work as it just gave me the same results as if those 5 steps didn't exist in run().

    Please guide me on how to get rid of the trailing colors. Thank you very much in advance.

    /* Part 2: Write a second applet that bounces multiple balls; 
     * the number is specified by a parameter in the HTML file. 
     * Assign different colours to different balls 
     * (you can have 10 unique colours and re-use colours after that). 
     * The balls can move independently, passing through each other. 
     * Each should have its own thread. 
     * 1. DONE - loop through however many balls are specified in html file creating a thread for each in an array.
     * 2. DONE - The array size is dictated by the "balls" parameter in the html file.
     * 3. DONE - Create an enum with 10 different colors.
     * 4. Need to track what enum value we have used or should use next.
     * 5. Need to check if the enum value has hit 10 then reset to 1.
     * 6. Need to be able to associate the color value with the thread.
     * int numLines = Integer.parseInt(getParameter("lines"));
    import java.applet.*;
    import java.awt.*;
    import java.util.Random;
    class Ball{
    	Graphics g;
    	int x;
    	int y;
    	int xmove; // x movement
    	int ymove; // y movement
    	int radius;
    	Color bColor;
    	private final Random generator = new Random();
    	public Ball(Color c){
    		x = generator.nextInt(400);
    		y = generator.nextInt(400);
    		// calculate random direction (and speed)
    		xmove = generator.nextInt(5)+1;
    		ymove = generator.nextInt(5)+1;
    		radius = 10;
    		bColor = c;
    	}// end constructor Ball
    	public void paint(Graphics graphik){
    		g = graphik;
    		g.fillOval(x-radius, y-radius, 2*radius, 2*radius);
    	}// end method paint
    	public void moveBall(){
    		if ((x - radius + xmove < 0) || (x + radius + xmove > 400)){
    			xmove = -xmove;
    	    if ((y - radius + ymove < 0) || (y + radius + ymove > 400)){
    	    	ymove = -ymove;
    	    // recalulate the x,y coordinates of the circle
    	}// end method moveBall
    }// end class ball
    public class BallsBounce extends Applet implements Runnable{
    	Thread th;
    	int numBalls;
    	Graphics graphic;
    	Image img;
    	Ball balls[];
    	private Color Colors[] = {Color.GRAY, Color.BLUE, Color.ORANGE, Color.CYAN, Color.BLACK, Color.MAGENTA, Color.GREEN,  Color.PINK, Color.YELLOW, Color.RED};
    	Color ballColor[];
    	int c; // count the colors used
    	public void init(){
    		numBalls = Integer.parseInt(getParameter("balls"));
    		img = createImage(getWidth(),getHeight());
    		graphic = img.getGraphics();
    		balls = new Ball[numBalls];
    		ballColor = new Color[numBalls];
    		for (int i=0; i<numBalls; i++){
    			if (c >= 10){
    			}// end if
    			ballColor[i] = Colors[c];
    			balls[i]=new Ball(ballColor[i]);
    		}// end for
    	}// end method init
    	public void start(){
    		if(th == null){
    			th = new Thread(this);
    		}// end if
    	}// end method start
    	public void run(){
    			for (int j=0; j<numBalls; j++){
    			}// end for
    			}// end try
    			catch(InterruptedException except){
    			}// end catch
    		}// end while
    	}// end method run
    	public void paint(Graphics g){
    		for (int k=0; k<numBalls; k++){
    		}// end for
    		g.drawImage(img, 10,10,this);
    	}// end method paint
    }// end class BallsBounce

  2. #2
    Join Date
    Mar 2004
    Since you've overridden the applet's paint method and are only drawing the balls, nothing is technically clearing the screen. You can either draw a solid box over the displayed area prior to painting the balls, or you could try calling super.paint() from the applet's paint method.

    As a side note, after you get this corrected, if you get flickering you'll want to look into "double buffering" the graphics. You could also look into extending Canvas and drawing on that instead.

Similar Threads

  1. Bouncing Ball Applet
    By nescafe in forum Java
    Replies: 1
    Last Post: 10-07-2005, 08:51 AM
  2. Replies: 1
    Last Post: 03-11-2001, 08:06 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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center