java – 如何阻止repaint()方法闪烁

基本上当屏幕刷新时,它会闪烁白色,然后一秒钟后,它会再次闪烁.我在mac上工作,在eclipse上屏幕没有闪烁,但是当我把它放到applet中时,它开始闪烁.

import java.awt.event.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.net.URL;
import java.io.*; 
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;     //Copyright MRE Dev
//Anyone that steals this code will be prostituted
public class RadicalRacingApplet extends Applet implements KeyListener {
final int WIDTH = 900, HEIGHT = 650;
double p1Speed = 0;
double p2Speed = 0;
double bullet1Speed = 7;
double bullet2Speed = 7;
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
int p1Direction = 1;
int p2Direction = 1;
int counterp1 = 0;
int counterp2 = 0;
int scoreN = 0;
int bullets = 0;
int p1Deaths = 0;
int p2Deaths = 0;
final int shoot = 1;
final int shootOther = 3;
final int shoot2 = 1;
final int shoot2Other = 3;
final int normal = 1;
final int big = 3;
final int normal2 = 1;
final int big2 = 3;
int bullets2 = 0;
int bigBullets = 0;
int bigBullets2 = 0;
String scoreS = " ";

URL eng = this.getClass().getResource ("hit.wav");
AudioClip snd = JApplet.newAudioClip(eng);




Rectangle p1 = new Rectangle(135,220,30,30);
Rectangle p2 = new Rectangle(700,300,30,30);
//Obstacles
Rectangle ob1 = new Rectangle(475,75,50,600);
Rectangle bullets1 = new Rectangle(0,0,10,5);
Rectangle bullets2Rect = new Rectangle(0,0,10,5);
Rectangle powerUp1 = new Rectangle(475,680,50,50);
Rectangle bigBullet = new Rectangle(200,600,50,50);
Rectangle bigBullet2 =  new Rectangle(800,600,50,50);


public void init() {



    Move1 m1 = new Move1();
    m1.start();
    Bullets1 b1 = new Bullets1();
    b1.start();
    Move2 m2 = new Move2();
    m2.start();
    Bullets2 b2 = new Bullets2();
    b2.start();
    PowerUps p1 = new PowerUps();
    p1.start();
    BigBullet1 bg1 = new BigBullet1();
    bg1.start();
    BigBullet2 bg2 = new BigBullet2();
    bg2.start();

    snd.loop();


}

public void paint(Graphics g) {
    super.paint(g);
    super.repaint();
    g.setColor(Color.blue);
    g.fillRect(0,0,1000,750);
    g.setColor(Color.WHITE);
    g.setFont(new Font("Arial",Font.PLAIN,16));




    g.setColor(Color.BLACK);


    g.fillRect(ob1.x, ob1.y, ob1.width, ob1.height);
    g.setColor(Color.WHITE);
    g.drawString(scoreS, 100, 100);

    g.fill3DRect(powerUp1.x, powerUp1.y, powerUp1.width, powerUp1.height, true);
    g.fill3DRect(bigBullet.x, bigBullet.y, bigBullet.width, bigBullet.height,true);
    g.fill3DRect(bigBullet2.x, bigBullet2.y, bigBullet2.width, bigBullet2.height, true);
    g.setColor(Color.YELLOW);
    g.setFont(new Font("Arial",Font.BOLD,30));
    g.drawString("P1 Deaths " + p1Deaths, 200, 50);
    g.drawString("P2 Deaths" + p2Deaths, 400, 50);


    Image img1;
    Image img2;
    Image img3;
    Image img4;
    Image img5;
    Image img6;
    Image img7;
    Image img8;
    try{
        URL url = this.getClass().getResource("p1.png");
        URL url2 = this.getClass().getResource("p1Right.png");
        URL url3 = this.getClass().getResource("p1Left.png");
        URL url4 = this.getClass().getResource("p1Down.png");
        URL url5 = this.getClass().getResource("bullet.png");
        URL url6 = this.getClass().getResource("bullet2.png");
        URL url7 = this.getClass().getResource("bigBullet.png");
        URL url8 = this.getClass().getResource("bigBullet2.png");
        img1 = Toolkit.getDefaultToolkit().getImage(url);
        img2 = Toolkit.getDefaultToolkit().getImage(url2);
        img3 = Toolkit.getDefaultToolkit().getImage(url3);
        img4 = Toolkit.getDefaultToolkit().getImage(url4);
        img5 = Toolkit.getDefaultToolkit().getImage(url5);
        img6 = Toolkit.getDefaultToolkit().getImage(url6);
        img7 = Toolkit.getDefaultToolkit().getImage(url7);
        img8 = Toolkit.getDefaultToolkit().getImage(url8);

    if(p1Direction == UP)  {
        g.drawImage(img1, p1.x, p1.y, this);
    }
    if(p1Direction == RIGHT)  {
        g.drawImage(img2, p1.x, p1.y, this);
    }
    if(p1Direction == LEFT) {
        g.drawImage(img3, p1.x, p1.y, this);
    }
    if(p1Direction == DOWN)  {
        g.drawImage(img4, p1.x, p1.y, this);
    }

    if(p2Direction == UP)  {
        g.drawImage(img1, p2.x, p2.y, this);
    }
    if(p2Direction == RIGHT)  {
        g.drawImage(img2, p2.x, p2.y, this);
    }
    if(p2Direction == LEFT) {
        g.drawImage(img3, p2.x, p2.y, this);
    }
    if(p2Direction == DOWN)  {
        g.drawImage(img4, p2.x, p2.y, this);
    }
    if(bullets == shoot)  {
        g.drawImage(img5, bullets1.x, bullets1.y, this);
    }
    if(bullets == shootOther)  {
        g.drawImage(img6, bullets1.x, bullets1.y, this);
    }
    if(bullets2 == shoot2)  {
        g.drawImage(img6, bullets2Rect.x, bullets2Rect.y, this);
    }
    if(bullets2 == shoot2Other)  {
        g.drawImage(img5, bullets2Rect.x, bullets2Rect.y, this);
    }
    if((bigBullets == big) && (bullets == shoot))  {
        g.drawImage(img7,bullets1.x, bullets1.y, this);
    }
    if((bigBullets2 == big2) && (bullets2 == shoot2Other)) {
        g.drawImage(img7, bullets2Rect.x, bullets2Rect.y, this);
    }
    if((bigBullets2 == big2) && (bullets2 == shoot2))  {
        g.drawImage(img8, bullets2Rect.x, bullets2Rect.y, this);
    }
    if((bigBullets == big) && (bullets == shootOther))  {
        g.drawImage(img8, bullets1.x, bullets1.y, this);
    }
    }
    catch(Exception e)  {
        System.out.println("The images dont work");
    }
}

public class Move1 extends Thread implements KeyListener {
    public void run() {
        addKeyListener(this);
        while(true) {
            try {



                if(p1.intersects(ob1))  {
                    p1.x = 135;
                    p1.y = 220;
                    p1Deaths++;
                }

                if(p1.intersects(p2))  {
                    p1.x = 135;
                    p1.y = 220;
                    p1Deaths++;
                }


                if(p1.x >1000)  {
                    p1.x = 0;
                }
                if(p1.y > 750)  {
                    p1.y = 0;
                }
                if(p1.x <0)  {
                    p1.x = 1000;
                }
                if(p1.y < 0)  {
                    p1.y = 750;
                }

                if(p1Direction == UP) {

                    p1.y-=(int)p1Speed;
                    if(p1Speed <=3)  {
                        p1Speed+=.2;


                    }

                }

                if(p1Direction == DOWN) {

                    p1.y+=(int)p1Speed;
                    if(p1Speed <=3)  {
                        p1Speed+=.2;

                    }
                }
                if(p1Direction == LEFT) {
                    p1.x-=(int)p1Speed;
                    if(p1Speed <=3)  {
                        p1Speed+=.2;

                    }
                }
                if(p1Direction == RIGHT) {
                    p1.x+=(int)p1Speed;
                    if(p1Speed <=3)  {
                        p1Speed+=.2;

                    }
                }

                Thread.sleep(35);

            }
            catch(Exception e) {
                break;
            }
        }

    }

public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
public void keyTyped(KeyEvent event) {
    if(event.getKeyChar() == 'a') {
        p1Direction = LEFT;
    }
    if(event.getKeyChar() == 's') {
        p1Direction = DOWN;
    }
    if(event.getKeyChar() == 'd') {
        p1Direction = RIGHT;
    }
    if(event.getKeyChar() == 'w') {
        p1Direction = UP;
    }

}

}
public class Move2 extends Thread implements KeyListener {
    public void run() {
        addKeyListener(this);
        while(true) {
            try {


                if(p2.intersects(ob1))  {
                    p2.x = 700;
                    p2.y = 300;
                    p2Deaths++;
                }

                if(p2.intersects(p1))  {
                    p2.x = 700;
                    p2.y = 300;
                    p2Deaths++;
                }

                if(p2.x >1000)  {
                    p2.x = 0;
                }
                if(p2.y > 750)  {
                    p2.y = 0;
                }
                if(p2.x <0)  {
                    p2.x = 1000;
                }
                if(p2.y < 0)  {
                    p2.y = 750;
                }
                if(p2Direction == UP) {

                    p2.y-=(int)p2Speed;
                    if(p2Speed <=3)  {
                        p2Speed+=.2;


                    }

                }

                if(p2Direction == DOWN) {

                    p2.y+=(int)p2Speed;
                    if(p2Speed <=3)  {
                        p2Speed+=.2;

                    }
                }
                if(p2Direction == LEFT) {
                    p2.x-=(int)p2Speed;
                    if(p2Speed <=3)  {
                        p2Speed+=.2;

                    }
                }
                if(p2Direction == RIGHT) {
                    p2.x+=(int)p2Speed;
                    if(p2Speed <=3)  {
                        p2Speed+=.2;

                    }
                }


                Thread.sleep(35);

            }
            catch(Exception e) {
                break;
            }
        }

    }

public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
public void keyTyped(KeyEvent event) {
    if(event.getKeyChar() == 'j') {
        p2Direction = LEFT;
    }
    if(event.getKeyChar() == 'k') {
        p2Direction = DOWN;
    }
    if(event.getKeyChar() == 'l') {
        p2Direction = RIGHT;
    }
    if(event.getKeyChar() == 'i') {
        p2Direction = UP;
    }

}

}
public class Bullets1 extends Thread implements KeyListener{
    public void run()  {
        addKeyListener(this);
        while(true)  {
            try {

                if(bullets1.intersects(p2))  {
                    p2.x = 700;
                    p2.y = 300;
                    p2Deaths++;
                }
                if(bullets1.x > 1000)  {
                    bullet1Speed = 7;
                    bullets = 0;
                    bullets1.x = 0;
                    bullets1.y = 0;
                }
                if(bullets1.x < 0)  {
                    bullet1Speed = 7;
                    bullets = 0;
                    bullets1.x = 0;
                    bullets1.y = 0;
                }
                double p1X = p1.getX();
                double p1Y = p1.getY();
                if(bullets == shoot)  {
                    bullets1.x = (int)p1X;
                    bullets1.y = (int)p1Y;
                    if(bullet1Speed >= 5) {
                        bullet1Speed+=5;


                    }
                    bullets1.x+=(int)bullet1Speed;

                }
                if(bullets == shootOther)  {
                    bullets1.x = (int)p1X;
                    bullets1.y = (int)p1Y;
                    if(bullet1Speed >=5)  {
                        bullet1Speed+=5;
                    }
                    bullets1.x-=(int)bullet1Speed;
                }




                Thread.sleep(20);

            }
            catch(Exception e)  {
                break;
            }
        }
    }


    public void keyPressed(KeyEvent event) {
        if(event.getKeyChar() == 'e') {
            bullets = shoot;
        }
        if(event.getKeyChar() == 'q')  {
            bullets = shootOther;
        }



    }

    @Override
    public void keyReleased(KeyEvent event) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent event) {
        // TODO Auto-generated method stub

    }
}

public class Bullets2 extends Thread implements KeyListener{
    public void run()  {
        addKeyListener(this);
        while(true)  {
            try {

                if(bullets2Rect.intersects(p1))  {
                    p1.x = 135;
                    p1.y = 300;
                    p1Deaths++;
                }
                if(bullets2Rect.x < 0)  {
                    bullet2Speed = 7;
                    bullets2 = 0;
                    bullets2Rect.x = 0;
                    bullets2Rect.y = 0;
                }
                if(bullets2Rect.x > 1000)  {
                    bullet2Speed = 7;
                    bullets2 = 0;
                    bullets2Rect.x = 0;
                    bullets2Rect.y = 0;
                }
                double p2X = p2.getX();
                double p2Y = p2.getY();
                if(bullets2 == shoot2)  {
                    bullets2Rect.x = (int)p2X;
                    bullets2Rect.y = (int)p2Y;
                    if(bullet2Speed >= 5) {
                        bullet2Speed+=5;


                    }
                    bullets2Rect.x-=(int)bullet2Speed;

                }
                if(bullets2 == shoot2Other)  {
                    bullets2Rect.x = (int)p2X;
                    bullets2Rect.y = (int)p2Y;
                    if(bullet2Speed >=5)  {
                        bullet2Speed+=5;
                    }
                    bullets2Rect.x+=(int)bullet2Speed;
                }


                Thread.sleep(20);

            }
            catch(Exception e)  {
                break;
            }
        }
    }


    public void keyPressed(KeyEvent event) {
        if(event.getKeyChar() == 'u') {
            bullets2 = shoot2;
        }
        if(event.getKeyChar() == 'o')  {
            bullets2 = shoot2Other;
        }


    }

    @Override
    public void keyReleased(KeyEvent event) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent event) {
        // TODO Auto-generated method stub

    }
}

public class PowerUps extends Thread implements KeyListener  {
    public void run()  {

        addKeyListener(this);
        while(true) {
            try {

                int countPower = 0;
                int countPower2 = 0;
                if(p1.intersects(powerUp1))  {
                    powerUp1.setSize(0,0);
                    countPower = 1;
                }

                if(countPower == 1) {

                        p1Speed = 7;
                        Thread.sleep(10000);
                }
                if(countPower == 0)  {
                    p1Speed = 3;
                }
                if(p2.intersects(powerUp1))  {
                    powerUp1.setSize(0,0);

                    countPower2 = 1;
                }
                if(countPower2 == 1)  {
                    p2Speed = 7;
                    Thread.sleep(10000);

                }
                if(countPower == 0)  {
                        p2Speed = 3;
                    }

                Thread.sleep(35);
            }
            catch (Exception e) {
                break;
            }
        }
    }




    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

}

public class BigBullet1 extends Thread {
    public void run()  {
        while(true)  {
            try {

                if(p1.intersects(bigBullet))  {
                    bigBullet.setSize(0,0);
                    bigBullets = big;
                    Thread.sleep(10000);
                    bigBullets = normal;
                }
                if(p2.intersects(bigBullet))  {
                    bigBullet.setSize(0,0);
                    bigBullets2 = big;
                    Thread.sleep(10000);
                    bigBullets2 = normal2;
                }
            }
            catch(Exception e)  {
                break;
            }
        }
    }
}

public class BigBullet2 extends Thread  {
    public void run()  {
        while(true)  {
            try {

                if(p2.intersects(bigBullet2))  {
                    bigBullet2.setSize(0,0);
                    bigBullets2 = big2;

                    Thread.sleep(10000);
                    bigBullets2 = normal2;

                }
                if(p1.intersects(bigBullet2))  {
                    bigBullet2.setSize(0,0);
                    bigBullets = big2;
                    bullets1.setSize(10,10);
                    Thread.sleep(10000);
                    bigBullets = normal2;
                }
            }
            catch(Exception e)  {
                break;
            }
        }
    }
}



@Override
public void keyPressed(KeyEvent event) {
    // TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent event) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent event) {
    // TODO Auto-generated method stub

}

}

解决方法:

首先,不要使用Applet,使用JApplet

其次,不要覆盖*容器的绘制方法,使用类似JPanel或JComponent的东西,它们默认提供双缓冲.

第三,不要在任何paint方法中调用repaint(事实上,不要调用任何可能发布重绘请求的东西……)

第四,不要在paint方法中加载你的图像?请参阅此Problems when drawing images in java applets了解原因……

更新

基本上…

public class GamePane extends JPanel implements KeyListener {

    final int WIDTH = 900, HEIGHT = 650;
    double p1Speed = 0;
    double p2Speed = 0;
    double bullet1Speed = 7;
    double bullet2Speed = 7;
    final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
    int p1Direction = 1;
    int p2Direction = 1;
    int counterp1 = 0;
    int counterp2 = 0;
    int scoreN = 0;
    int bullets = 0;
    int p1Deaths = 0;
    int p2Deaths = 0;
    final int shoot = 1;
    final int shootOther = 3;
    final int shoot2 = 1;
    final int shoot2Other = 3;
    final int normal = 1;
    final int big = 3;
    final int normal2 = 1;
    final int big2 = 3;
    int bullets2 = 0;
    int bigBullets = 0;
    int bigBullets2 = 0;
    String scoreS = " ";
    URL eng = this.getClass().getResource("hit.wav");
    AudioClip snd = JApplet.newAudioClip(eng);
    Rectangle p1 = new Rectangle(135, 220, 30, 30);
    Rectangle p2 = new Rectangle(700, 300, 30, 30);
//Obstacles
    Rectangle ob1 = new Rectangle(475, 75, 50, 600);
    Rectangle bullets1 = new Rectangle(0, 0, 10, 5);
    Rectangle bullets2Rect = new Rectangle(0, 0, 10, 5);
    Rectangle powerUp1 = new Rectangle(475, 680, 50, 50);
    Rectangle bigBullet = new Rectangle(200, 600, 50, 50);
    Rectangle bigBullet2 = new Rectangle(800, 600, 50, 50);

    public BadApplet2() {



        Move1 m1 = new Move1();
        m1.start();
        Bullets1 b1 = new Bullets1();
        b1.start();
        Move2 m2 = new Move2();
        m2.start();
        Bullets2 b2 = new Bullets2();
        b2.start();
        PowerUps p1 = new PowerUps();
        p1.start();
        BigBullet1 bg1 = new BigBullet1();
        bg1.start();
        BigBullet2 bg2 = new BigBullet2();
        bg2.start();

        snd.loop();


    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // This bad, don't do it
        //super.repaint();
        g.setColor(Color.blue);
        g.fillRect(0, 0, 1000, 750);
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.PLAIN, 16));

        g.setColor(Color.BLACK);
        g.fillRect(ob1.x, ob1.y, ob1.width, ob1.height);

        g.setColor(Color.WHITE);
        g.drawString(scoreS, 100, 100);

        g.fill3DRect(powerUp1.x, powerUp1.y, powerUp1.width, powerUp1.height, true);
        g.fill3DRect(bigBullet.x, bigBullet.y, bigBullet.width, bigBullet.height, true);
        g.fill3DRect(bigBullet2.x, bigBullet2.y, bigBullet2.width, bigBullet2.height, true);
        g.setColor(Color.YELLOW);
        g.setFont(new Font("Arial", Font.BOLD, 30));
        g.drawString("P1 Deaths " + p1Deaths, 200, 50);
        g.drawString("P2 Deaths" + p2Deaths, 400, 50);


        // This is bad, don't do it...
        // Move these to the field level of the class
        // and load them in the constructor
        /*
        Image img1;
        Image img2;
        Image img3;
        Image img4;
        Image img5;
        Image img6;
        Image img7;
        Image img8;
        */
        //try {
            // This is bad don't do it...
            /*
            URL url = this.getClass().getResource("p1.png");
            URL url2 = this.getClass().getResource("p1Right.png");
            URL url3 = this.getClass().getResource("p1Left.png");
            URL url4 = this.getClass().getResource("p1Down.png");
            URL url5 = this.getClass().getResource("bullet.png");
            URL url6 = this.getClass().getResource("bullet2.png");
            URL url7 = this.getClass().getResource("bigBullet.png");
            URL url8 = this.getClass().getResource("bigBullet2.png");
            img1 = Toolkit.getDefaultToolkit().getImage(url);
            img2 = Toolkit.getDefaultToolkit().getImage(url2);
            img3 = Toolkit.getDefaultToolkit().getImage(url3);
            img4 = Toolkit.getDefaultToolkit().getImage(url4);
            img5 = Toolkit.getDefaultToolkit().getImage(url5);
            img6 = Toolkit.getDefaultToolkit().getImage(url6);
            img7 = Toolkit.getDefaultToolkit().getImage(url7);
            img8 = Toolkit.getDefaultToolkit().getImage(url8);
            */

            if (p1Direction == UP) {
                g.drawImage(img1, p1.x, p1.y, this);
            }
            if (p1Direction == RIGHT) {
                g.drawImage(img2, p1.x, p1.y, this);
            }
            if (p1Direction == LEFT) {
                g.drawImage(img3, p1.x, p1.y, this);
            }
            if (p1Direction == DOWN) {
                g.drawImage(img4, p1.x, p1.y, this);
            }

            if (p2Direction == UP) {
                g.drawImage(img1, p2.x, p2.y, this);
            }
            if (p2Direction == RIGHT) {
                g.drawImage(img2, p2.x, p2.y, this);
            }
            if (p2Direction == LEFT) {
                g.drawImage(img3, p2.x, p2.y, this);
            }
            if (p2Direction == DOWN) {
                g.drawImage(img4, p2.x, p2.y, this);
            }
            if (bullets == shoot) {
                g.drawImage(img5, bullets1.x, bullets1.y, this);
            }
            if (bullets == shootOther) {
                g.drawImage(img6, bullets1.x, bullets1.y, this);
            }
            if (bullets2 == shoot2) {
                g.drawImage(img6, bullets2Rect.x, bullets2Rect.y, this);
            }
            if (bullets2 == shoot2Other) {
                g.drawImage(img5, bullets2Rect.x, bullets2Rect.y, this);
            }
            if ((bigBullets == big) && (bullets == shoot)) {
                g.drawImage(img7, bullets1.x, bullets1.y, this);
            }
            if ((bigBullets2 == big2) && (bullets2 == shoot2Other)) {
                g.drawImage(img7, bullets2Rect.x, bullets2Rect.y, this);
            }
            if ((bigBullets2 == big2) && (bullets2 == shoot2)) {
                g.drawImage(img8, bullets2Rect.x, bullets2Rect.y, this);
            }
            if ((bigBullets == big) && (bullets == shootOther)) {
                g.drawImage(img8, bullets1.x, bullets1.y, this);
            }
        //} catch (Exception e) {
        //    System.out.println("The images dont work");
        //}
    }

    // Add the rest of the code
}

我会做更多关于游戏线程的阅读.通常,应该有一个“游戏循环”,它负责更新游戏中各种资产的状态,而不是几十个线程.

使用此面板并将其添加到JApplet.如果您不知道该怎么做,那么您将阅读Creating a GUI with Swing的内容

UPDATE

至于你的java.lang.ClassCastException,我想你正试图将面板加载为applet,这显然是行不通的.

public class RadicalRacingApplet extends JApplet {
    public void init() {
        setLayout(new BorderLayout());
        add(new GamePanel());
    }
}

应该让你解决它…

上一篇:如何在Java Applet中立即显示图像?


下一篇:将数据从JApplet发送到JavaScript的最简单方法是什么?