3

基本上当屏幕刷新时,它会闪烁白色,然后一秒钟后,它会再次闪烁。我在 Mac 上工作,在 Eclipse 上,屏幕不会闪烁,但是一旦我将它放入小程序,它就开始闪烁。

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

}

}

4

1 回答 1

5

First, don't use Applet, use JApplet

Second, don't override the paint methods of a top level container, use something like JPanel or JComponent, they provide double buffering by default.

Third, don't call repaint inside any paint method (in fact, don't call anything that might post a repaint request...)

Forth, do NOT to load your images in the paint method?? See this Problems when drawing images in java applets for why...

UPDATED

Basically...

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
}

I would do a lot more reading about game threads. Generally, should have a single "game loop" which is responsible for updating the state of the various assets within you game, not dozens of threads.

Take this panel and add it to you JApplet. If you don't know how to do that, then you're going to what to read the Creating a GUI with Swing

UPDATE

As to your java.lang.ClassCastException, I'd imagin you're trying to load the panel as an applet, which obviously won't work.

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

Should get you around it...

于 2012-09-22T03:01:20.223 回答