0

This is the code for my game. The entire image flickers every 30 ms (aprox)

public class MainGame extends Canvas implements Runnable, KeyListener, MouseListener, MouseMotionListener {

    boolean isRunning = false;
    public boolean isClicked, isReleased, lClick, rClick, down, up, upPressed;
    public boolean drawArrow;
    public boolean lastLeft, lastRight, goingLeft, goingRight, left, right;
    public int x = 0, y = 0, aX = 250, aY = 250;
    double modifier = 4;
    public Graphics g;
    ImageIcon background = new ImageIcon("F:/workspace/RPGGame/src/healy.jpg");
    Image picture = background.getImage();
    Image offscreen;
    ControlBase base = new ControlBase();
    Graphics buffer;`
    private boolean fireLeft;
    private boolean fireRight;
    private int arrowCounter = 0;

    public MainGame () {

        base.frame.setVisible(true);
        base.frame.setResizable(false);
        base.frame.setMinimumSize(new Dimension(1024, 768));
        base.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        base.frame.setLocationRelativeTo(null);
        base.frame.addMouseListener(this);
        base.frame.addKeyListener(this);
        JPanel panel = new JPanel();
        Container pane = base.frame.getContentPane();
        pane.add(panel);
        pane.paint(g);
        base.frame.setVisible(true);
        Graphics g = base.frame.getGraphics();
        g.drawImage(picture, x, y, this);
    }
    public void run() {
        while(isRunning) {
            paint(base.frame.getGraphics());
            //moves left
            if (left == true){
                x+=1;
                lastLeft = true;
                lastRight = false;
            }
            //moves right
            if (right == true){
                x-=1;
                lastLeft = false;
                lastRight = true;
            }
            //moves down
            if (down == true){
                y-=1;
            }
            if (goingLeft == true) {
                aX--;
            }
            if (goingRight == true) {
                aX++;
            }
            if(attackStyle == 1) {
                melee();
            }
            else if (attackStyle == 2) {
                range();
            }
            else {
                magic();
            }
            System.out.println(arrowCounter);
            base.arrowMech();
            fireLeft = base.fireLeft;
            fireRight = base.fireRight;
            base.frame.repaint();
        }
    }

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.clearRect(0, 0, 1024, 768);
      g.setColor(Color.red);
      g.fillOval(250, 250, 20, 20);
      g.drawImage(picture, x, y, this);

      if (drawArrow == true) {
          if (fireLeft) {
              arrowCounter++;
              goingLeft = true;
              goingRight = false;
              base.drawArrow(g);
          }
          if (fireRight) {
              arrowCounter++;
              goingLeft = false;
              goingRight = true;
              base.drawArrow(g);
          }
      }
      if ((arrowCounter >=450) || (!drawArrow)){
          arrowCounter = 0;
          aX = 250;
          aY = 250;
      }

}
public void update(Graphics g) {
    repaint();
}
public void start() {
    isRunning = true;
    new Thread(this).start();
}
public void stop() {
    isRunning = false;
}

public void mouseDragged(MouseEvent e) {

}
public void mouseMoved(MouseEvent e) {

}
public void mouseClicked(MouseEvent click) {
    if(click.getButton() == 1) {

    }

}
public void mouseEntered(MouseEvent e) {

}
public void mouseExited(MouseEvent e) {

}
/** This method handles mouse clicks
*/
public void mousePressed(MouseEvent click) {
    if(SwingUtilities.isLeftMouseButton(click)) {
        lClick = true;
    }
    else if (SwingUtilities.isRightMouseButton(click)) {
        rClick = true;
    }

}
/** This method handles the release of mouse clicks
*/
public void mouseReleased(MouseEvent release) {
    if(SwingUtilities.isLeftMouseButton(release)) {
        lClick = false;
    }
    else if (SwingUtilities.isRightMouseButton(release)) {
        rClick = false;
    }
}
/**
* This method handle the movement for the character and all other key binds
*/
public void keyPressed(KeyEvent e) {
    //left arrow
    if(e.getKeyCode() == 37 || e.getKeyCode() == 65){
        left = true;
    }
    //up arrow
    if(e.getKeyCode() == 38 || e.getKeyCode() ==  87){
        up = true;
        upPressed = true;
        down = false;
    }
    //right arrow
    if(e.getKeyCode() == 39 || e.getKeyCode() == 68){
        right = true;
    }
    //down arrow
    if(e.getKeyCode() == 40 || e.getKeyCode() == 83){
        down = true;
    }
}
/**
* This method handles the stopping of movement for the character and stoping all other key binds
*/
public void keyReleased(KeyEvent e) {
    //left arrow
    if(e.getKeyCode() == 37 || e.getKeyCode() == 65){
        left = false;
    }
    //up arrow
    if(e.getKeyCode() == 38 || e.getKeyCode() ==  87){
        up = false;
        upPressed = false;
    }
    //right arrow
    if(e.getKeyCode() == 39 || e.getKeyCode() == 68){
        right = false;
    }
    //down arrow
    if(e.getKeyCode() == 40 || e.getKeyCode() == 83){
        down = false;
    }

}
public void keyTyped(KeyEvent e) {

}

Any help would be appreciated. Have tried using a bufferstrategy to no avail. Is there any other way to do it without using a bufferstrategy?

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
DWigley
  • 33
  • 7
  • possible duplicate of [How do you double buffer in java for a game?](http://stackoverflow.com/questions/10508042/how-do-you-double-buffer-in-java-for-a-game) – Lukas Knuth Dec 08 '13 at 01:20

1 Answers1

0

I don't think you need to call the paint(Graphics g) method. It should be called automatically by the parent when needed. Especially since you are calling repaint at the end of the code

Basile Perrenoud
  • 4,039
  • 3
  • 29
  • 52