Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java bouncing balls flickering

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java bouncing balls flickering

    Hi all, I am very new to Java and need help with this code if possible. I am to create an applet that creates a number of balls derived from a HTML.They bounce of the bounds of the frame but do not bounce of each other!

    I think I have most of the code however this keeps Flickering any help appreciated! I am very new to Java and coding in general, threads are killing me!
    I have been at this for days so any help at all appreciated!

    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,int width,int height) {
     
            x = generator.nextInt(width);
            y = generator.nextInt(height);
            // 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) {
     
            graphik.setColor(bColor);
            graphik.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
     
     
        }// end method paint
     
        public void moveBall(int width,int height) {
            if ((x - radius + xmove < 0) || (x + radius + xmove > width)) {
                xmove = -xmove;
            }
            if ((y - radius + ymove < 0) || (y + radius + ymove > height)) {
                ymove = -ymove;
            }
            // recalulate the x,y coordinates of the circle
            x += xmove;
            y += ymove;
        }// 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};
        int c = 0; // count the colors used
     
        public void init() {
            setBackground(Color.LIGHT_GRAY);
            numBalls = Integer.parseInt(getParameter("balls"));
            //
            img = createImage(getWidth(), getHeight());
            graphic = img.getGraphics();
     
            balls = new Ball[numBalls];
     
     
            for (int i = 0; i < numBalls; i++) {
                if (c >= 10) {
                    c = 0;
                }// end if
     
                balls[i] = new Ball(Colors[c],getWidth(), getHeight());
                c++;
            }// end for
        }// end method init
     
        public void start() {
            if (th == null) {
                th = new Thread(this);
                th.start();
            }// end if
        }// end method start
     
        public void run() {
            while (true) {
     
                for (int j = 0; j < numBalls; j++) {
                    balls[j].moveBall(getWidth(), getHeight());
     
                }// end for
     
                try {
                    th.sleep(20);
                }// end try
                catch (InterruptedException except) {
                }// end catch
                repaint();
            }// end while
        }// end method run
     
        public void paint(Graphics g) {
            super.paint(g);
            //g.clearRect(0, 0, this.getWidth(), this.getHeight());
            graphic.clearRect(0, 0, this.getWidth(), this.getHeight());
            for (int k = 0; k < numBalls; k++) {
     
                balls[k].paint(graphic);
                g.drawImage(img, 10, 10, this);
            }// end for
     
     
        }// end method paint
    }// end class BallsBounce
    Last edited by Norm; April 21st, 2013 at 06:42 AM. Reason: Removed spaces from code tag


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java bouncing balls flickering

    Neat code formatting. Could use some comments.
    Do you have a question?

Similar Threads

  1. Help with bouncing on a panel
    By hellhunt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 9th, 2012, 08:28 PM
  2. How to find collision between balls in Applet animation
    By AJAXx195 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 4th, 2011, 01:01 PM
  3. Bouncing Balls
    By roza in forum Threads
    Replies: 6
    Last Post: June 7th, 2011, 10:10 AM
  4. Java swing buttons are flickering in windows 7 environment
    By javasam in forum Member Introductions
    Replies: 1
    Last Post: February 17th, 2011, 09:51 AM
  5. Java swing buttons are flickering in windows 7 environment
    By javasam in forum AWT / Java Swing
    Replies: 1
    Last Post: February 14th, 2011, 12:13 PM

Tags for this Thread