为什么我的图像在java jframe中闪烁?

时间:2013-12-08 01:05:51

标签: java eclipse image jframe flicker

这是我游戏的代码。整个图像每30毫秒闪烁一次(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) {

}

任何帮助将不胜感激。尝试过使用bufferstrategy无济于事。没有使用bufferstrategy,有没有其他方法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

我认为你不需要调用paint(Graphics g)方法。它应该在需要时由父母自动调用。特别是因为你在代码的末尾调用了重绘