0

Hello I am having these flickering issues with this pong game, and was hoping someone else could steer me in the right direction. been at this for like an hour or 2 and can not figure it out.

It would be greatly appreciated ?.? like the ball and score are the worst and disappear almost completely when I embed the game.

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

public class Pong extends Applet implements MouseMotionListener, KeyListener {

int       doubleb,i,my,bx,by,px,py,compx,compy,width,height,speedx,speedy,bwidth,bheight,pwidth,pheight,score;
boolean started;
private Timer timer1;

    @Override
public void init() { 
    setSize(800,500); //setting screan size, declaring background color and some of my listeners
    width = getSize().width;
    height = getSize().height;
    setBackground(Color.black);
    pheight = 120;
    pwidth = 15;
    bheight = 30;
    bwidth = 30;
    addKeyListener(this); //listers used for the mouse and keybored input
    addMouseMotionListener(this);
    px = 35;
    compx = width - 35 - pwidth;
    newgame();
    timer1 = new Timer(10,new ActionListener() {
                @Override
        public void actionPerformed(ActionEvent e) { //momvement, physics, collusion
            height = getSize().height;
            width = getSize().width;
            bx += speedx;
            by += speedy;
            if (by <= 0 || by + bheight >= height) {
                speedy = -speedy;
            }
            if (bx <= px + pwidth && by + bheight >= py && by <= py + pheight && bx > px) {
                speedx = -speedx;
                ++score;
            }
            if (bx + bwidth >= compx && by + bheight >= compy && by <= compy + pheight && bx < compx + pwidth) {
                speedx = -speedx;
            }
            if (speedx < 0) {
                if (compy + pheight / 2 != height / 2) {
                    if (compy + pheight / 2 > height / 2) {
                        compy -= -speedx;
                    }
                    else {
                        compy += -speedx;
                    }
                }
            }
            else {
                if (by + bheight / 2 <= compy + pheight / 2) {
                    compy -= speedx;
                }
                else {
                    compy += speedx;
                }
            }
            if (compy < 0) {
                compy = 0;
            }
            if (compy + pheight > height) {
                compy = height - pheight;
            }
            if (bx + bwidth < 0) {
                py = height / 2 - pheight / 2;
                timer1.stop();
                started = false;
            }
            repaint();
        }    
    });
}

    @Override
public void mouseMoved(MouseEvent e) { 
    if (started) {
        my = e.getY();
        if (my + pheight / 2 > height) {
            my = height - pheight / 2;
        }
        if (my < pheight / 2) {
            my = pheight / 2;
        }
        py = my - pheight / 2;
        repaint();
    }
}

    @Override
public void mouseDragged(MouseEvent e) { }

    @Override
public void paint(Graphics g) {

    Font font1 = new Font("Arial", Font.BOLD, 18); //set font 
    Font font2 = new Font("Arial", Font.BOLD,40); //set font
    g.setColor(Color.white);
    g.drawRect(0,0,width - 1,height - 1);
    g.fillRect(px,py,pwidth,pheight);
    g.fillRect(compx,compy,pwidth,pheight);
    g.setFont(font1);
    g.drawString("Score: " + score,20,20); //paints the sorce 

            if (started) {
        g.fillArc(bx,by,bwidth,bheight,0,360);
    }
    else {
        g.setFont(font2); //set font
        g.setColor(Color.green); //set color
        g.drawString("Pong",width / 2 - 46,height / 2 - 16); //draws text to the screen
        g.setFont(font1);
        g.drawString("Press 's' to start",width / 2 - 69,height / 2 + 30);              
        }
    }

public void newgame() {
    py = height / 2 - pheight / 2;
    compy = py;
    bx = width / 2 - bwidth / 2;
    by = height / 2 - bheight / 2;
    speedx = 10;
    speedy = 10;
    score = 0;
}

    @Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyChar() == 's') {
        started = true;
        newgame();
        timer1.start();
    }
}

    @Override
public void keyTyped(KeyEvent e) { }

    @Override
public void keyReleased(KeyEvent e) { }

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rune
  • 1
  • 1
    Won't fix your immediate problem, but you should call `super.paint` to help prevent possible issues in the future. The next question is why are you using an API which is over 15 years out-of-date and has been replaced with at least two other APIs? You could use a `JPanel` and override it's `paintComponent` and perform you custom painting there. Swing components are double buffered by default. You could then add the panel to what ever top level container you want (`JApplet` or `JFrame`) based on you needs – MadProgrammer Jun 19 '14 at 23:52
  • 1
    1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Jun 20 '14 at 09:19

1 Answers1

0

you might want to look into double buffering

Basically, the issue you're having is that you clear the screen in order to redraw it. However, before the Graphics objects is completely finished updating, it gets drawn to the screen. This results in a half finished screen, and a noticeable flickering effect.

The fix to this is to instead draw your image to a seperate Graphics object first, then when it is done, swap that Graphics object with the one being displayed.

more information can be found here

Community
  • 1
  • 1
clearlyspam23
  • 1,624
  • 13
  • 15