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 5 of 5

Thread: my "pong" code keeps flashing!

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post my "pong" code keeps flashing!

    ok my friend in highschool created this java app in netbeans. its pong but a very basic version, it has a menu system with controls and stuff and when you click play you should move a paddle along the bottom of the screen while a ball bounces off the four walls and each time you hit the ball with the paddle you get a point. like i said very basic, however the ball paddle and walls keep flashing, and theres no way for you to tell whats going on, so something tells me that its the draw method or something related to it, i havent coded in java in quite a while and my other friend recommended me to this site and so i thought id try you guys out here is a copy of the code. let me know if any of you know whats wrong

    package pong;
     
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Image;
    import javax.swing.*; // Imports the necessary components to make the program function.
     
    public class ping extends Applet implements ActionListener {
     
        public void init() {
            setSize(700, 500); // Sets screen size to 700, 500.
        }
        int xPosition1, xPosition2, xPosition3, xPosition4, xPosition5, xPosition6, xPosition7, xPosition8, xPosition9, xPosition10, xPosition11, xPosition12, xPosition13, xPosition14, xPosition15, xPosition16, xPosition17, xPosition18, xPosition19, xPosition20; // Creates variables for the posistion of 20 diffrent strings.
        int fontWidth1, fontWidth2, fontWidth3, fontWidth4, fontWidth5, fontWidth6, fontWidth7, fontWidth8, fontWidth9, fontWidth10, fontWidth11, fontWidth12, fontWidth13, fontWidth14, fontWidth15, fontWidth16, fontWidth17, fontWidth18, fontWidth19, fontWidth20; // Creates variablesto measure the width of 20 strings.
        int start = 0; // Variable used to initialize settings everytime a pong game is started.
        int level = 1; // Variable to decide what screen of the game you see.
        int x2, y2; // Variables created to store mouse co-ordinates for use of the buttons.
        int ballX = (int) (Math.random() * (675 - 25 + 1)) + 25; // Creates a random location for the ball's x co-ordinate.
        int ballY = (int) (Math.random() * (300 - 25 + 1)) + 25; // Creates a random location for the ball's y co-ordinate.
        int xVelo = 1, yVelo = 1; // Creats variables for the velocity of the ball, and starts it at one.
        int paddleX = 325; // Variable for the location of the paddle's x co-ordinate.
        int paddleY = 490; // Variable for the location of the paddle's y co-ordinate.
        int paddleX2 = 325; // Variable for the location of player 2's paddle x co-ordinate.
        int paddleY2 = 0; // Variable for the location of player 2's paddle y co-ordinate.
        int score = 0; // A variable to record the score of the player's.
        int pause = 0; // A variable used for the pauseing of a pong game.
        int high[] = new int[6]; // A variable created to store high scores and display them on the high score screen.
        int record = 0; // When record == 1 it will record the current score into the high score array.
        int lev = 0; // Used to decide whether it should show the player 1 death screen or the player 2 death screen.
        int win = 0; // Records which player one in a two player game.
     
        public void start() {
            Timer timer;
            final int FREQ = 1;
            timer = new Timer(FREQ, this);
            timer.start(); // Creates a timer used for the refreshing of the game screen, which is used to create the movement of the ball.
        }
     
        public void actionPerformed(ActionEvent evt) {
            if (level == 2 && pause == 0 || level == 7 && pause == 0) {
                repaint(); // Refreshes the screen with the timer when a pong game is active.
            }
        }
     
        public boolean mouseDown(Event evt, int x, int y) {
            x2 = x; // Records the x location of the mouse.
            y2 = y; // Recors the y location of the mouse.
            if (start != 1) {
                repaint(); // Only refreshes the screen if a game is not currently running.
            }
            return true;
        }
     
        public boolean keyDown(Event evt, int key) {
     
            if (key == Event.LEFT && pause == 0) {
                paddleX -= 10; // Moves the paddle left when the left arrow is pressed, and the game is not paused.
            }
            if (key == Event.RIGHT && pause == 0) {
                paddleX += 10; // Moves the paddle right when the right arrow is pressed, and the game is not paused.
            }
            if (key == 'a' && pause == 0 || key == 'A' && pause == 0) {
                paddleX2 -= 10; // Moves the second paddle left when the A key is pressed, and the game is not paused.
            }
            if (key == 'd' && pause == 0 || key == 'D' && pause == 0) {
                paddleX2 += 10; // Moves the second paddle right when the D key is pressed, and the game is not paused.
            }
            if (key == Event.UP && xVelo > -5 && xVelo < 5 && (level == 2 || level == 7)) {
                if (xVelo > 0 && yVelo > 0) {
                    xVelo += 1;
                    yVelo += 1; // Increases ball speed when Up arrow is pressed.
                }
                if (xVelo < 0 && yVelo < 0) {
                    xVelo -= 1;
                    yVelo -= 1;  // Increases ball speed when Up arrow is pressed.
                }
                if (xVelo > 0 && yVelo < 0) {
                    xVelo += 1;
                    yVelo -= 1;  // Increases ball speed when Up arrow is pressed.
                }
                if (xVelo < 0 && yVelo > 0) {
                    xVelo -= 1;
                    yVelo += 1;  // Increases ball speed when Up arrow is pressed.
                }
            }
            if (key == Event.DOWN && (xVelo > 1 || xVelo < -1) && (level == 2 || level == 7)) {
                if (xVelo > 0 && yVelo > 0) {
                    xVelo -= 1;
                    yVelo -= 1; // Decreases ball speed when Down arrow is pressed.
                }
                if (xVelo < 0 && yVelo < 0) {
                    xVelo += 1;
                    yVelo += 1; // Decreases ball speed when Down arrow is pressed.
                }
                if (xVelo > 0 && yVelo < 0) {
                    xVelo -= 1;
                    yVelo += 1; // Decreases ball speed when Down arrow is pressed.
                }
                if (xVelo < 0 && yVelo > 0) {
                    xVelo += 1;
                    yVelo -= 1; // Decreases ball speed when Down arrow is pressed.
                }
            }
            if (key == 'q' && level == 2 || key == 'Q' && level == 2 || key == 'q' && level == 7 || key == 'Q' && level == 7) {
                start = 0; // Initializes the game variables.
                if (level == 2) {
                    lev = 1; // Records that it was a single player game that was quit.
                } else if (level == 7) {
                    lev = 0; // Records that a two player game was quit.
                }
                level = 6; // Tells the game to start the game over screen.
                record = 1; // Tells the program to record the score.
                repaint();
            }
            if (key == 'p' && level == 2 || key == 'P' && level == 2 || key == 'p' && level == 7 || key == 'P' && level == 7) {
                if (pause == 0) {
                    pause = 1; // When unpaused the program becomes paused.
                } else if (pause == 1) {
                    pause = 0; // When paused the program becomes unpaused.
                }
            }
            if (paddleX < 0) {
                paddleX = 620; // When paddle reaches the left side of screen it appears at the right side.
            }
            if (paddleX > 620) {
                paddleX = 0; // When paddle reaches the right side of screen it appears at the left side.
            }
            if (paddleX2 < 0) {
                paddleX2 = 620; // When paddle 2 reaches the left side of screen it appears at the right side.
            }
            if (paddleX2 > 620) {
                paddleX2 = 0; // When paddle 2 reaches the right side of screen it appears at the left side.
            }
            return true;
        }
     
        public void paint(Graphics g) {
            Font arial = new Font("Arial", Font.BOLD, 24);
            FontMetrics fontMetricsName = getFontMetrics(arial);
            Font arial2 = new Font("Arial", Font.BOLD, 10);
            FontMetrics fontMetricsName2 = getFontMetrics(arial2);
            Font arial3 = new Font("Arial", Font.BOLD, 12);
            Font arial4 = new Font("Arial", Font.BOLD, 48);
            FontMetrics fontMetricsName3 = getFontMetrics(arial4); // Creates Fonts that are needed for the programs.
            Image background = getImage(getCodeBase(), ""); // Uploads a background image for my menu's.
            if (level == 1) { // The Main Menu
                g.drawImage(background, 0, 0, 700, 500, this); // Sets the background image.
     
                g.setFont(arial4);
                fontWidth1 = fontMetricsName3.stringWidth("Pong");
                xPosition1 = (size().width - fontWidth1) / 2;
                g.drawString("Pong", xPosition1, 55); // Creates the Pong Title.
     
                g.setFont(arial);
                fontWidth2 = fontMetricsName.stringWidth("Start Game");
                xPosition2 = (size().width - fontWidth2) / 2;
                g.fillRoundRect(xPosition2, 80, fontWidth2, 28, 10, 10);
                g.setColor(Color.white);
                g.drawString("Start Game", xPosition2, 104); // Creates the Start Game button.
     
                fontWidth17 = fontMetricsName.stringWidth("2 Player");
                xPosition17 = (size().width - fontWidth17) / 2;
                g.setColor(Color.black);
                g.fillRoundRect(xPosition17, 180, fontWidth17, 28, 10, 10);
                g.setColor(Color.white);
                g.drawString("2 Player", xPosition17, 204); // Creates the 2 Player button.
     
                fontWidth3 = fontMetricsName.stringWidth("Rules");
                xPosition3 = (size().width - fontWidth3) / 2;
                g.setColor(Color.black);
                g.fillRoundRect(xPosition3, 280, fontWidth3, 28, 10, 10);
                g.setColor(Color.white);
                g.drawString("Rules", xPosition3, 304); // Creates the Rules button.
     
                fontWidth4 = fontMetricsName.stringWidth("Controls");
                xPosition4 = (size().width - fontWidth4) / 2;
                g.setColor(Color.black);
                g.fillRoundRect(xPosition4, 380, fontWidth4, 28, 10, 10);
                g.setColor(Color.white);
                g.drawString("Controls", xPosition4, 404); // Creates the Controls Button.
     
                fontWidth5 = fontMetricsName.stringWidth("Exit Program");
                xPosition5 = (size().width - fontWidth5) / 2;
                g.setColor(Color.black);
                g.fillRoundRect(xPosition5, 470, fontWidth5, 28, 10, 10);
                g.setColor(Color.white);
                g.drawString("Exit Program", xPosition5, 494); // Creates the Exit Button.
     
     
                g.setFont(arial2);
                g.setColor(Color.black);
                g.drawString("Matt Macleod", 20, 485); // Places my name in the bottom-left corner.
                fontWidth6 = fontMetricsName2.stringWidth("© January 24th, 2012"); // Places a copyright with the date made.
                xPosition6 = 680 - fontWidth6;
                g.drawString("© December 13th, 2012", xPosition6, 485);
            } else if (level == 2) { // Main Pong Game
                if (start == 0) {
                    ballX = (int) (Math.random() * (675 - 25 + 1)) + 25;
                    ballY = (int) (Math.random() * (300 - 25 + 1)) + 25;
                    paddleX = 325;
                    paddleY = 490;
                    xVelo = 1;
                    yVelo = 1;
                    lev = 0;// All above reinitialize the pong variables.
                }
                start = 1; // Keeps the program from constantly reinitalizing.
                g.setColor(Color.black);
                g.fillRoundRect(paddleX, paddleY, 80, 10, 10, 10); // Creates Paddle.
                ballX += xVelo; // Moves the ball according to velocity.
                ballY += yVelo; // Moves the ball according to velocity.
                g.fillOval(ballX, ballY, 25, 25); // Creats pong ball.
                g.setFont(arial);
                fontWidth11 = fontMetricsName.stringWidth("Score: " + score);
                xPosition11 = size().width - fontWidth11;
                g.drawString("Score: " + score, 700 - fontWidth11, 20); // Creates a score display in the top right of the screen.
                if (ballX < 0) {
                    xVelo = -xVelo; // Left Wall Collision.
                }
                if (ballX > 675) {
                    xVelo = -xVelo; // Right Wall Collision.
                }
                if (ballY < 0) {
                    yVelo = -yVelo; // Top Wall Collision
                }
                if (ballY > 475) {
                    start = 0; // Allows pong settings to reinitialize on re-start up.
                    level = 6; // Displays game over screen.
                    record = 1; // Tells it to record the score of the player.
                    lev = 1; // Says this was a single player game.
                    repaint();
                }
                if (ballX >= paddleX && ballX <= paddleX + 80 && ballY >= paddleY && ballY <= paddleY + 10 && ballY < 490 || ballX + 25 >= paddleX && ballX + 25 <= paddleX + 80 && ballY + 25 >= paddleY && ballY + 25 <= paddleY + 10 && ballY < 490 || ballX + 25 >= paddleX && ballX + 25 <= paddleX + 80 && ballY >= paddleY && ballY <= paddleY + 10 && ballY < 490 || ballX >= paddleX && ballX <= paddleX + 80 && ballY + 25 >= paddleY && ballY + 25 <= paddleY + 10 && ballY < 490) {
                    yVelo = -yVelo;
                    ballY = 465;
                    score += 1; // Collsion with paddle along with a score increase. The paddle collision works by detecting all corner of the ball and disallowing the ball to travel inside of the paddle. This also raises the balls to the top of the paddle to avoid it from being stuck inside and rapidly collide without getting out.
                }
            } else if (level == 3) { // Rules Screen
                g.drawImage(background, 0, 0, 700, 500, this); // Creates a background image.
                g.setColor(Color.black);
                g.setFont(arial);
                g.drawString("Rules", 0, 24);
                g.setFont(arial3);
                g.drawString("Move the paddle to keep the ball from hitting the bottom edge of the screen.", 0, 36);
                g.drawString("A point will be scored every time the user successfully intercepts the ball.", 0, 48);
                g.drawString("Try to maximize your high score and challenge friends to beat you.", 0, 60);
                g.drawString("Once the ball hits the bottom of the screen it is game over.", 0, 72); // Displays all the rules needed and the title.
                fontWidth7 = fontMetricsName.stringWidth("Return to Main Menu");
                xPosition7 = 0;
                g.fillRoundRect(xPosition7, 96, fontWidth7, 28, 10, 10);
                g.setColor(Color.white);
                g.setFont(arial);
                g.drawString("Return to Main Menu", xPosition7, 120); // Creates a Return to Main Menu button.
            } else if (level == 4) { // Creates Controls Screen
                g.drawImage(background, 0, 0, 700, 500, this); // Displays the background image.
                g.setColor(Color.black);
                g.setFont(arial);
                g.drawString("Controls", 0, 24);
                g.setFont(arial3);
                g.drawString("Left Arrow Key: Move Paddle Left", 0, 36);
                g.drawString("Right Arrow Key: Move Paddle Right", 0, 48);
                g.drawString("A: Move Paddle Left (Player 2)", 0, 60);
                g.drawString("D: Move Paddle Right (Player 2)", 0, 72);
                g.drawString("Up Arrow Key: Increase Ball Speed", 0, 84);
                g.drawString("Down Arrow Key: Decrease Ball Speed", 0, 96);
                g.drawString("P: Pause Game", 0, 108);
                g.drawString("Q: Quit Game", 0, 120); // Displays all controls along with a title.
                fontWidth8 = fontMetricsName.stringWidth("Return to Main Menu");
                xPosition8 = 0;
                g.fillRoundRect(xPosition8, 126, fontWidth8, 28, 10, 10);
                g.setColor(Color.white);
                g.setFont(arial);
                g.drawString("Return to Main Menu", xPosition8, 150); // Creates a Return to Main Menu button.
            } else if (level == 5) { // Exits Game
                System.exit(0); // Closes the applet.
            } else if (level == 6) { // Game Over / Highscore
                while (record == 1) { // Checks to see if it should record a score.
                    high[5] = score; // Sets score to the appropriate spot in the array.
                    score = 0; // Resets the score for next round.
                    bubbleSort(high); // Sorts it so that the highest number will be displayed first.
                    record = 0; // Used so that it will only record once.
                }
                g.drawImage(background, 0, 0, 700, 500, this); // Displays the background image.
                fontWidth10 = fontMetricsName.stringWidth("Return to Main Menu");
                xPosition10 = (size().width - fontWidth10) / 2;
                g.fillRoundRect(xPosition10, 426, fontWidth10, 28, 10, 10);
                g.setColor(Color.white);
                g.setFont(arial);
                g.drawString("Return to Main Menu", xPosition10, 450); // Creates Return to Main Menu button.
                g.setColor(Color.black);
                if (lev == 0) { // When a 2 player game is quit.
                    fontWidth20 = fontMetricsName.stringWidth("Rage Quit!");
                    xPosition20 = (size().width - fontWidth20) / 2;
                    g.setColor(Color.black);
                    g.setFont(arial);
                    g.drawString("Rage Quit!", xPosition20, 150); // Displays the words rage quit.
                }
                if (lev == 1) { // When a single player game is over.
                    fontWidth9 = fontMetricsName.stringWidth("Game Over");
                    xPosition9 = (size().width - fontWidth9) / 2;
                    g.setColor(Color.black);
                    g.setFont(arial);
                    g.drawString("Game Over", xPosition9, 150); // Displays game over.
                    g.drawString("High Score:", xPosition9, 180);
                    g.drawString("1. " + high[0], xPosition9, 210);
                    g.drawString("2. " + high[1], xPosition9, 240);
                    g.drawString("3. " + high[2], xPosition9, 270);
                    g.drawString("4. " + high[3], xPosition9, 300);
                    g.drawString("5. " + high[4], xPosition9, 330);// Displays the high score chart.
                }
                if (lev == 2) { // If a 2 player game is finished.
                    if (win == 1) { // If Player One wins the game
                        fontWidth18 = fontMetricsName.stringWidth("Player 1 Wins!");
                        xPosition18 = (size().width - fontWidth18) / 2;
                        g.setColor(Color.black);
                        g.setFont(arial);
                        g.drawString("Player 1 Wins!", xPosition18, 150); // Displays Player 1 Wins.
                    } else if (win == 2) { // If Player Two wins the game
                        fontWidth19 = fontMetricsName.stringWidth("Player 2 Wins!");
                        xPosition19 = (size().width - fontWidth19) / 2;
                        g.setColor(Color.black);
                        g.setFont(arial);
                        g.drawString("Player 2 Wins!", xPosition19, 150); // Display Player 2 Wins.
                    }
                }
            } else if (level == 7) { // 2 Player Pong
                if (start == 0) {
                    ballX = (int) (Math.random() * (675 - 25 + 1)) + 25;
                    ballY = (int) (Math.random() * (300 - 25 + 1)) + 25;
                    paddleX = 325;
                    paddleY = 490;
                    paddleX2 = 325;
                    paddleY2 = 0;
                    xVelo = 1;
                    yVelo = 1;
                    lev = 0; // Reinitializes the variables for pong.
                }
                start = 1; // Keeps reinitializing from happening over and over.
                g.setColor(Color.black);
                g.fillRoundRect(paddleX, paddleY, 80, 10, 10, 10);
                g.fillRoundRect(paddleX2, paddleY2, 80, 10, 10, 10); // Creates both paddles.
                ballX += xVelo; // Moves the ball according to velocity.
                ballY += yVelo; // Moves the ball according to velocity.
                g.fillOval(ballX, ballY, 25, 25); // Displays the ball.
                if (ballX < 0) {
                    xVelo = -xVelo; // Left Wall Collison
                }
                if (ballX > 675) { // Right Wall Collison
                    xVelo = -xVelo;
                }
                if (ballY < 0) { // Ball exits in player 2's side of the screen.
                    start = 0; // So that variables will reinitialize when a new game is started.
                    level = 6; //Opens the Game Over Screen
                    lev = 2; // Tells the program it is a 2 player game.
                    win = 1; // Will display that Player 1 wins.
                    repaint();
                }
                if (ballY > 475) { // Ball exits in player 1's side of the screen.
                    start = 0; // So that variables will reinitialize when a new game is started.
                    level = 6; //Opens the Game Over Screen
                    lev = 2; // Tells the program it is a 2 player game.
                    win = 2; // Will display that Player 2 wins.
                    repaint();
                }
                if (ballX >= paddleX && ballX <= paddleX + 80 && ballY >= paddleY && ballY <= paddleY + 10 && ballY < 490 || ballX + 25 >= paddleX && ballX + 25 <= paddleX + 80 && ballY + 25 >= paddleY && ballY + 25 <= paddleY + 10 && ballY < 490 || ballX + 25 >= paddleX && ballX + 25 <= paddleX + 80 && ballY >= paddleY && ballY <= paddleY + 10 && ballY < 490 || ballX >= paddleX && ballX <= paddleX + 80 && ballY + 25 >= paddleY && ballY + 25 <= paddleY + 10 && ballY < 490) {
                    yVelo = -yVelo;
                    ballY = 465; // Collsion with paddle. The paddle collision works by detecting all corner of the ball and disallowing the ball to travel inside of the paddle. This also raises the balls to the top of the paddle to avoid it from being stuck inside and rapidly collide without getting out.
                }
                if (ballX >= paddleX2 && ballX <= paddleX2 + 80 && ballY >= paddleY2 && ballY <= paddleY2 + 10 && ballY > 0 || ballX + 25 >= paddleX2 && ballX + 25 <= paddleX2 + 80 && ballY + 25 >= paddleY2 && ballY + 25 <= paddleY2 + 10 && ballY > 0 || ballX + 25 >= paddleX2 && ballX + 25 <= paddleX2 + 80 && ballY >= paddleY2 && ballY <= paddleY2 + 10 && ballY > 0 || ballX >= paddleX2 && ballX <= paddleX2 + 80 && ballY + 25 >= paddleY2 && ballY + 25 <= paddleY2 + 10 && ballY > 0) {
                    yVelo = -yVelo;
                    ballY = 10; // Collsion with paddle 2. The paddle collision works by detecting all corner of the ball and disallowing the ball to travel inside of the paddle. This also raises the balls to the bottom of the paddle to avoid it from being stuck inside and rapidly collide without getting out.
                }
            }
            if (x2 >= xPosition2 && x2 <= xPosition2 + fontWidth2 && y2 >= 80 && y2 <= 108 && level == 1) { // Detects when the Start Game button is pressed.
                level = 2; // Starts up the game.
                x2 = 0;
                y2 = 0; // Resets the x an y co-ordinates.
                repaint();
            }
            if (x2 >= xPosition3 && x2 <= xPosition3 + fontWidth3 && y2 >= 280 && y2 <= 308 && level == 1) {
                level = 3; // Displays the rules screen
                x2 = 0;
                y2 = 0; // Resets the x an y co-ordinates.
                repaint();
            }
            if (x2 >= xPosition4 && x2 <= xPosition4 + fontWidth4 && y2 >= 380 && y2 <= 408 && level == 1) {
                level = 4; // Displays the controls screen.
                x2 = 0;
                y2 = 0; // Resets the x an y co-ordinates.
                repaint();
            }
            if (x2 >= xPosition4 && x2 <= xPosition4 + fontWidth4 && y2 >= 470 && y2 <= 498 && level == 1) {
                level = 5; // Exits the program
                x2 = 0;
                y2 = 0; // Resets the x an y co-ordinates.
                repaint();
            }
            if (x2 >= xPosition7 && x2 <= xPosition7 + fontWidth7 && y2 >= 96 && y2 <= 124 && level == 3) {
                level = 1; // Opens the Main Menu.
                x2 = 0;
                y2 = 0; // Resets the x an y co-ordinates.
                repaint();
            }
            if (x2 >= xPosition8 && x2 <= xPosition8 + fontWidth8 && y2 >= 126 && y2 <= 154 && level == 4) {
                level = 1; // Opens the Main Menu.
                x2 = 0;
                y2 = 0; // Resets the x an y co-ordinates.
                repaint();
            }
            if (x2 >= xPosition10 && x2 <= xPosition10 + fontWidth10 && y2 >= 426 && y2 <= 454 && level == 6) {
                level = 1; // Opens the Main Menu.
                x2 = 0;
                y2 = 0; // Resets the x an y co-ordinates.
                repaint();
            }
            if (x2 >= xPosition17 && x2 <= xPosition17 + fontWidth17 && y2 >= 180 && y2 <= 208 && level == 1) {
                level = 7; // Starts up 2 player pong.
                x2 = 0;
                y2 = 0; // Resets the x an y co-ordinates.
                repaint();
            }
        }
     
        private static void bubbleSort(int[] high) {
            int n = high.length; // Sets n to the length of the array.
            int temp = 0; // Declares a temperary variable
     
            for (int i = 0; i < n - 1; i++) {
                for (int j = 0; j < (n - i - 1); j++) {
                    if (high[j] < high[j + 1]) {
                        temp = high[j];
                        high[j] = high[j + 1];
                        high[j + 1] = temp; // Sorts in reverse order, placing the highest numbers first in the list.
                    }
                }
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: my "pong" code keeps flashing!

    I see several problems:
    Uses many old methods that are deprecated
    There are calls to repaint() from inside the paint() method. Use a timer loop to call repaint.
    For double buffering (reduces flashing) of the drawing, extend a class like JPanel, override its paintComponent() method and move the code from paint() to paintComponent. Add the JPanel to the applet (its a container).
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    atrixes (December 15th, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: my "pong" code keeps flashing!

    like i said its been a LONG time since ive coded in java, can i just have a quick refresher on timers and how to use the timer loop to call repaint? and thank you very much for your quick response

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: my "pong" code keeps flashing!

    Google the Swing Timer tutorial and there you will find not only a quick refresher but also sample code.

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: my "pong" code keeps flashing!

    thank you both curmudgeon, and Norm i shall update in a few minutes to let you guys know if i fixed the problem or not

    --- Update ---

    ok i seem to have fixed the probelm, it seems that it was just "repaint"ing every milisecond! so i just changed it to once a second and now the program runs without flashing, if you wish to test it for yourselves all i did was change FREQ, from FREQ=1; to FREQ =100;

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM

Tags for this Thread