0

I am working on a small Java Applet that has 3 balls bounce around on screen. When they collide with the walls they make one sound and when they collide with eachother they make another sound.

My issue is that the balls flicker for some reason and I am not sure why. If someone can explain why they are flickering and how I can go about fixing the issue that would be greatly appreciated.

Here is my code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ass3 extends Applet implements Runnable {
int[] positionX = {0, 0, 0};
int[] positionY = {0, 0, 0};
int[] incrementX = {3, 7, 4};
int[] incrementY = {8, 4, 5};
Thread t;
AudioClip sound;
AudioClip collide;
public void init(){
    sound = getAudioClip(getDocumentBase(), "Audio1.au");
    collide = getAudioClip(getDocumentBase(), "Audio2.au");
}
public void start() {
    t = new Thread(this);
    t.start();
}
public void paint(Graphics g) {
    g.fillOval(getWidth()-50-positionX[0], getHeight()-50-positionY[0], 50, 50);
    g.fillOval(getWidth()-450-positionX[1], getHeight()-450-positionY[1], 50, 50);
    g.fillOval(getWidth()-450-positionX[2], getHeight()-50-positionY[2], 50, 50);
}
public void run() {
    while (true) {
        try {
        Thread.sleep(30);
        } catch (InterruptedException e) {}
        for (int i=0; i < 3; i++) {
            positionX[i] += incrementX[i];
            positionY[i] += incrementY[i];
        }
        // ball 1
        if (positionX[0] > 500 || positionX[0] < 0) {
            incrementX[0] = -incrementX[0];
            sound.play();
        }
        repaint();
        if (positionY[0] > 500 || positionY[0] < 0) {
            incrementY[0] = -incrementY[0];
            sound.play();
        }
        repaint();
        // ball 2
        if (positionX[1] > 100 || positionX[1] < -400) {
            incrementX[1] = -incrementX[1];
            sound.play();
        }
        repaint();
        if (positionY[1] > 100 || positionY[1] < -400) {
            incrementY[1] = -incrementY[1];
            sound.play();
        }
        repaint();
        // ball 3
        if (positionX[2] > 100 || positionX[2] < -400) {
            incrementX[2] = -incrementX[2];
            sound.play();
        }
        repaint();
        if (positionY[2] > 500 || positionY[2] < 0) {
            incrementY[2] = -incrementY[2];
            sound.play();
        }
        repaint();
        // collision
        if (Math.sqrt(((getWidth()-50-positionX[0])-(getWidth()-450-positionX[1]))*((getWidth()-50-positionX[0])-(getWidth()-450-positionX[1])) + ((getHeight()-50-positionY[0])-(getHeight()-450-positionY[1]))*((getHeight()-50-positionY[0])-(getHeight()-450-positionY[1]))) <= 50) {
            incrementX[0] = -incrementX[0];
            incrementY[0] = -incrementY[0];
            incrementX[1] = -incrementX[1];
            incrementY[1] = -incrementY[1];
            collide.play();
        }
        repaint();
        if (Math.sqrt(((getWidth()-50-positionX[0])-(getWidth()-450-positionX[2]))*((getWidth()-50-positionX[0])-(getWidth()-450-positionX[2])) + ((getHeight()-50-positionY[0])-(getHeight()-50-positionY[2]))*((getHeight()-50-positionY[0])-(getHeight()-50-positionY[2]))) <= 50) {
            incrementX[0] = -incrementX[0];
            incrementY[0] = -incrementY[0];
            incrementX[2] = -incrementX[2];
            incrementY[2] = -incrementY[2];
            collide.play();
        }
        repaint();
        if (Math.sqrt(((getWidth()-450-positionX[2])-(getWidth()-450-positionX[1]))*((getWidth()-450-positionX[2])-(getWidth()-450-positionX[1])) + ((getHeight()-50-positionY[2])-(getHeight()-450-positionY[1]))*((getHeight()-50-positionY[2])-(getHeight()-450-positionY[1]))) <= 50) {
            incrementX[2] = -incrementX[2];
            incrementY[2] = -incrementY[2];
            incrementX[1] = -incrementX[1];
            incrementY[1] = -incrementY[1];
            collide.play();
        }
        repaint();
    }
}

}

  • Possible duplicate of [How do you double buffer in java for a game?](https://stackoverflow.com/questions/10508042/how-do-you-double-buffer-in-java-for-a-game) – LhasaDad Mar 28 '18 at 23:57
  • you might be interested in this: https://stackoverflow.com/questions/10508042/how-do-you-double-buffer-in-java-for-a-game – LhasaDad Mar 28 '18 at 23:57

1 Answers1

0

Simple, stop calling repaint multiple times. (You're calling it multiple times in your while loop).

Oleg
  • 6,124
  • 2
  • 23
  • 40