重新绘制java时闪烁

Cuf*_*ufe -1 java swing paint repaint

这是我的油漆代码的一个非常简短的版本.请你能告诉我如何阻止它闪烁.感谢您使用JPanel的想法.

 public void paint(Graphics g ) {
    g.fillRect(BulletX, BulletY,0,0);
    g.setColor(Color.yellow);
    try{
    FileInputStream saveFile = new FileInputStream("Wins1.sav");
    ObjectInputStream save = new ObjectInputStream(saveFile);

    wins1 = (int) save.readObject();


    save.close();
    }
    catch(Exception exc){
    exc.printStackTrace();
    }
    Image image;
    URL resource = getClass().getClassLoader().getResource("moon1.jpg");
       URL resource1 = getClass().getClassLoader().getResource("Tank.jpg");
       URL resource2 = getClass().getClassLoader().getResource("Tank2.jpg");

       ImageIcon i2 = new ImageIcon(resource);
        ImageIcon i1 = new ImageIcon(resource1);
        ImageIcon i3 = new ImageIcon(resource2);
    image = i2.getImage();
        g.drawImage(image, 0,0,null);
        if (SHOW.equals("ON")){
        g.setFont(new Font("TimesRoman", Font.BOLD, 30));
        g.drawString("Player 1's health ="+Integer.toString(Player1H), 50, 25);}
    if (wins == 1){
        g.setColor(Color.BLACK);
        g.fillOval(20, 10, 30, 30);
        g.setColor(Color.YELLOW);
    }
    if (wins == 2){
        g.setColor(Color.BLACK);
        g.fillOval(20, 10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 60,10, 30, 30);
        g.setColor(Color.YELLOW);
    }
    if (wins == 3){
        g.setColor(Color.BLACK);
        g.fillOval(20, 10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 60,10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 100,10, 30, 30);
        g.setColor(Color.YELLOW);
    }
    if (wins == 4){
        g.setColor(Color.BLACK);
        g.fillOval(20, 10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 60,10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 100,10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval(140,10, 30, 30);
        g.setColor(Color.YELLOW);
    }
    if (wins1 == 1){
        g.setColor(Color.BLACK);
        g.fillOval( 360,10, 30, 30);
        g.setColor(Color.YELLOW);
    }
    if (wins1 == 2){
        g.setColor(Color.BLACK);
        g.fillOval( 360,10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 400,10, 30, 30);
        g.setColor(Color.YELLOW);
    }
    if (wins1 == 3){
        g.setColor(Color.BLACK);
        g.fillOval( 360,10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 400,10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 480,10, 30, 30);
        g.setColor(Color.YELLOW);
    }
    if (wins1 == 4){
        g.setColor(Color.BLACK);
        g.fillOval( 360,10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 400,10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 480,10, 30, 30);
        g.setColor(Color.BLACK);
        g.fillOval( 520,10, 30, 30);
        g.setColor(Color.YELLOW);
    }
    image = i1.getImage();
    g.drawImage(image,myX, myY,null);
    image = i3.getImage();
    g.setColor(Color.blue);
    if (Boss == 1){
        g.setColor(Color.RED);
        g.fillOval(myX1, myY1, 50, 50);
         try {
        Thread.sleep(800);
    } catch (InterruptedException ex) {
        Logger.getLogger(onePlayer1.class.getName()).log(Level.SEVERE, null, ex);
    }
         Boss = 2;
        win();
    }

    g.drawImage(image,myX1, myY1,null);

    if (hit2 ==1){
        g.setColor(Color.red);
        hit2 =0;
        g.fillOval(myX1, myY1, 20, 20);

        try {
        Thread.sleep(200);
    } catch (InterruptedException ex) {
        Logger.getLogger(onePlayer1.class.getName()).log(Level.SEVERE, null, ex);
    }
        repaint();
    }
    if (hit1 ==1){
        g.setColor(Color.red);
        hit1 =0;
        g.fillOval(myX, myY, 20, 20);

        try {
        Thread.sleep(200);
    } catch (InterruptedException ex) {
        Logger.getLogger(onePlayer1.class.getName()).log(Level.SEVERE, null, ex);
    }
        repaint();
    }
    if (Explode == true){
        g.setColor(Color.RED);
        g.fillOval(myX11,myY11,40,40);
        number = 0;
        Explode = false;
        myX11 = -10;
        myY11 = -10;
        Player3H = 0;

        try {
        Thread.sleep(800);
    } catch (InterruptedException ex) {
        Logger.getLogger(onePlayer1.class.getName()).log(Level.SEVERE, null, ex);
    }
        repaint();
    }
    if (number ==11){
        URL resource3 = getClass().getClassLoader().getResource("Dynamite.jpg");
        ImageIcon i4 = new ImageIcon(resource3);
        image = i4.getImage();
        g.setColor(Color.GREEN);
                g.drawImage(image,myX11, myY11,null);
    }
    if (number == 1){

        g.setColor(Color.GREEN);
        myX11 = myX1+10;
        myY11 = myY1+10;
                g.drawImage(image,myX11, myY11,null);
        number =11;
        Player3H = 10;
        repaint();
    }
    g.setColor(Color.BLACK);
    if (fire1.equals("Yes")){


        if (aim1.equals("right")){
        BulletY1 = myY1;    
        BulletX1 = myX1;



        g.fillRect(BulletX1, BulletY1, 1000, 5);

        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(HandlingEvents.class.getName()).log(Level.SEVERE, null, ex);
        }

        fire1="no";
        repaint();}
        if (aim1.equals("down")){
        BulletY1 = myY1;    
        BulletX1 = myX1;
        g.fillRect(BulletX1, BulletY1, 5, 1000);

        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(HandlingEvents.class.getName()).log(Level.SEVERE, null, ex);
        }

        fire1="no";
        repaint();}
        if (aim1.equals("left")){
        BulletY1 = myY1;    
        BulletX1 = myX1-1000;
        g.fillRect(BulletX1, BulletY1, 1000,5);
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(HandlingEvents.class.getName()).log(Level.SEVERE, null, ex);
        }

        fire1="no";
        repaint();}
        if (aim1.equals("up")){

        BulletY1 = myY1-1000;    
        BulletX1 = myX1;
        g.fillRect(BulletX1, BulletY1, 5, 1000);

        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(HandlingEvents.class.getName()).log(Level.SEVERE, null, ex);
        }

        fire1="no";
        repaint();}}
    if (fire.equals("Yes")){


        if (aim.equals("right")){
        BulletY = myY;    
        BulletX = myX;
        BulletXX = myX+10;
        BulletYY = myY+10;

        g.fillRect(BulletXX, BulletYY, 1000, 5);
        g.fillRect(BulletX, BulletY, 1000, 5);



        fire="no";

            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(onePlayer1.class.getName()).log(Level.SEVERE, null, ex);
            }
        repaint();}
        if (aim.equals("down")){
        BulletY = myY;    
        BulletX = myX;
        BulletXX = myX+10;
        BulletYY = myY+10;

        g.fillRect(BulletXX, BulletYY, 5, 1000);
        g.fillRect(BulletX, BulletY, 5, 1000);

        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(HandlingEvents.class.getName()).log(Level.SEVERE, null, ex);
        }

        fire="no";
        repaint();}
        if (aim.equals("left")){
        BulletY = myY;    
        BulletX = myX-1000;
         BulletXX = myX-990;
         BulletYY = myY+10;

        g.fillRect(BulletXX, BulletYY, 1000, 5);
        g.fillRect(BulletX, BulletY, 1000,5);
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(HandlingEvents.class.getName()).log(Level.SEVERE, null, ex);
        }

        fire="no";
        repaint();}
        if (aim.equals("up")){

        BulletY = myY-1000;    
        BulletX = myX;
         BulletXX = myX+10;
         BulletYY = myY-990;

        g.fillRect(BulletXX, BulletYY, 5, 1000);
        g.fillRect(BulletX, BulletY, 5, 1000);

        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(HandlingEvents.class.getName()).log(Level.SEVERE, null, ex);
        }
        g.fillRect(BulletX, BulletY, 0, 0);
        fire="no";
        repaint();}}}
Run Code Online (Sandbox Code Playgroud)

Fra*_*ool 6

你的代码太长了,没有人想调试它,但我发现你在快速检查中发现它有一些错误:

  1. public void paint(Graphics g ) {你应该从来没有覆盖paint(...)的方法,但是paintComponent(...),作为Swing组件都是双重缓冲(由@Radiodef在他的回答解释):

    Swing确保将适当类型的Graphics对象(用于双缓冲的屏幕外图像Graphics,否则为常规Graphics)传递给组件的paint回调,因此所有组件需要做的就是使用它绘制

    我猜你使用这种方法的唯一原因是因为你正在扩展JFrame,你应该扩展JPanel而基于它们构建你的GUI,作为JFrame一个僵化的容器,在这里阅读更多:扩展JFrame vs在程序中创建它

  2. 你没有打电话super.paint(g);(或者如果按照上面的建议super.paintComponent(g);作为方法中的第一行......那可能会破坏油漆链

  3. 读取绘制方法中的文件可能是程序闪烁的原因,因为它增加了处理时间和延迟,这些行:

    FileInputStream saveFile = new FileInputStream("Wins1.sav");
    ObjectInputStream save = new ObjectInputStream(saveFile);
    ...
    //until this line
    image = i2.getImage();
    
    Run Code Online (Sandbox Code Playgroud)

    更好地放置在构造函数级别,绘制方法应该绘制,只有那样,因为绘制方法被多次调用(每次将鼠标移动到GUI上它都会重新绘制,因此每次都会加载资源)

  4. Thread.sleep(...);您正在阻止事件调度线程,这可能是您的程序闪烁的另一个原因,您应该使用Swing Timer来执行peridic任务

  5. Boss = 2;,Explode == true您不遵循Java命名约定:

    • FirstWordUpperCaseClass
    • firstWordLowerCaseVariable
    • firstWordLowerCaseMethod()
    • ALL_WORDS_UPPER_CASE_CONSTANT
  6. 你在方法repaint();里面多次打电话paint(...)!这是另一个原因,因为每次都是递归调用......

  7. if (Explode == true),可以写成if (explode)可以防止像这样的拼写错误if (explode = true)

  8. 您的代码没有正确缩进,因此很难阅读

如果您遵循以上所有建议但仍有问题,请考虑发布一个有效的最小,完整和可验证的示例来说明问题,它应该很短但仍然完整且可编译


Rad*_*def 6

默认情况下,Swing组件已经是双缓冲的.你要覆盖paint哪种颠覆那种机制.如果你改写paintComponent(就像我们在Swing中绘画时应该做的那样),那么绘画将自动双重缓冲.

但是:

  • 你正在阅读文件paint,你不应该这样做.这使得您的绘画效率非常低,以至于低帧速率可能会明显变得明显.相反,你应该载入您的文件,一旦第一次启动程序时,把资源的地方,你可以很容易地从程序中引用.

  • 你调用Thread.sleeppaint其冻结GUI.Swing程序应该使用a Timer代替.

  • 你正在repaint()递归调用,它会使用绘制事件来泛滥事件队列,并启动一个你无法控制的无限循环.使用a Timer远远好得多.

如果你想学习如何使用Swing编写简单的游戏,你可以看看我的这两个答案:https://stackoverflow.com/a/44371593/2891664,https : //stackoverflow.com/a/30175751/2891664.