简体   繁体   English

ActiveRendering-在缓冲区上绘图不起作用

[英]ActiveRendering - Drawing on Buffer not working

I am creating a pong game. 我正在创建一个乒乓球游戏。 I have fininshed the ball and paddle classes, the animator and everything else related. 我已经完成了球类和桨类,动画师以及其他所有相关的工作。 However when I open my created program only 1 paddle shows, while the other paddle and the ball do not show. 但是,当我打开创建的程序时,仅显示一个桨,而另一个桨和球不显示。 If I change the way I draw things, the ball will show and the other 2 paddles don't. 如果更改绘画方式,则球将显示,而其他2个桨叶则不显示。 So it only draws 1 thing, whatever comes first. 因此,它只画出一件事,无论先发生什么。 Here is the code for part which paints to the buffer. 这是油漆到缓冲区的零件代码。

public void renderlojen(){ // render game function
        if(pamja==null){
            pamja=createImage(GJERESIA,LARTESIA); // Image - serves as buffer

        }

        g =(Graphics2D) pamja.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, GJERESIA, LARTESIA);

        doreza1.vizatodorezen(g); // paddle1
        doreza2.vizatodorezen(g); // paddle2
        topi1.vizatotopin(g); // ball

        g.dispose();
    }

public void updatolojen(){  // update game function
        topi1.leviztopin();
        doreza1.levizdorezen();
        doreza2.levizdorezen();
    }

public void pikturolojen(){ // draw from buffer to screen
        if (pamja!=null){
            g=(Graphics2D)this.getGraphics();
            g.drawImage(pamja, 0, 0, null);
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
            System.out.println(doreza1.merrX());
            System.out.println(doreza2.merrX());
        }
        else
            System.out.println("Ska pamje");

    }

public void vizatotopin(Graphics2D g2d){ // draw the ball code
        topiforma =new Ellipse2D.Float(pozicioniX,pozicioniY,2*rrezja,2*rrezja);
        g2d.setColor(Color.CYAN);
        g2d.fill(topiforma);
        g2d.dispose();
    }

public void vizatodorezen (Graphics2D g2d){ // draw paddle code
        drejtkendeshforma = new Rectangle2D.Float(pozicioniX,pozicioniY,GJERESIA,LARTESIA);
        g2d.setColor(ngjyra);
        g2d.fill(drejtkendeshforma);
        g2d.dispose();
    }

The problem is calling Graphics.dispose() in the rendering methods of the game objects. 问题是在游戏对象的渲染方法中调用Graphics.dispose() Drawing to the Graphics is not valid after that, so only the first object gets drawn. 在那之后绘制到Graphics无效,因此仅绘制第一个对象。

In general, call Graphics.dispose() only in the same method where you created it. 通常,仅以与创建它相同的方法调用Graphics.dispose() Not in methods that receive one as a parameter. 不能在将一个作为参数的方法中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM