简体   繁体   English

java双缓冲问题

[英]java double buffering problem

Whats wrong with my applet code which does not render double buffering correctly.I am trying and trying.But failed to get a solution.Plz Plz someone tell me whats wrong with my code. 我的小程序代码有什么问题,它无法正确呈现双重缓冲。我正在尝试,但是未能获得解决方案.Plz Plz有人告诉我我的代码有什么问题。

import java.applet.* ;
import java.awt.* ;
import java.awt.event.* ;


public class Ball extends Applet implements  Runnable
{
    // Initialisierung der Variablen
    int x_pos = 10;     // x - Position des Balles
    int y_pos = 100;    // y - Position des Balles
    int radius = 20;    // Radius des Balles

    Image buffer=null;
    //Graphics graphic=null;

    int w,h;

    public void init()
    {
        Dimension d=getSize();
        w=d.width;
        h=d.height;

        buffer=createImage(w,h);
        //graphic=buffer.getGraphics();

        setBackground (Color.black);

    }

    public void start ()
    {
        // Schaffen eines neuen Threads, in dem das Spiel lไuft
        Thread th = new Thread (this);
        // Starten des Threads
        th.start ();
    }

    public void stop()
    {

    }

    public void destroy()
    {

    }

    public void run ()
    {
        // Erniedrigen der ThreadPriority um zeichnen zu erleichtern
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        // Solange true ist lไuft der Thread weiter
        while (true)
        {
            // Verไndern der x- Koordinate
            repaint();

            x_pos++;
            y_pos++;
            //x2--;
            //y2--;

            // Neuzeichnen des Applets


            if(x_pos>410)
                x_pos=20;

            if(y_pos>410)
                y_pos=20;

            try
            {

                Thread.sleep (30);
            }
            catch (InterruptedException ex)
            {
                // do nothing
            }

            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
    }

    public void paint (Graphics g)
    {

        Graphics screen=null;

        screen=g;
        g=buffer.getGraphics();

        g.setColor(Color.red);


        g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);


        screen.drawImage(buffer,0,0,this);

    }


    public void update(Graphics g)
    {

        paint(g);
    }

}

what change should i make.When offscreen image is drawn the previous image also remain in screen.How to erase the previous image from the screen?? 我应该做些什么更改。绘制离屏图像时,以前的图像也会保留在屏幕中。如何从屏幕上删除以前的图像?

You are not clearing the buffer between two renderings. 您没有清除两个渲染之间的缓冲区。

How about adding 如何添加

g.fillRect(0, 0, buffer.getWidth(), buffer.getHeight());

using your background color (black?), before calling g.setColor(Color.RED); g.fillOval... 在调用g.setColor(Color.RED); g.fillOval...之前使用背景色(黑色? g.setColor(Color.RED); g.fillOval... g.setColor(Color.RED); g.fillOval... ? g.setColor(Color.RED); g.fillOval...

It looks like you do not cleanup your buffer image between draws. 看起来您没有在绘制之间清理缓冲区图像。 You could test this by changing your code to: 您可以通过将代码更改为以下内容进行测试:

    g.setColor(Color.red);
    g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);

    screen.drawImage(buffer,0,0,this);

    g.setColor(Color.green);
    g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);

Which draws the red ball and wipes it green afterwards, so you sould see a red ball leaving a green trail. 它画出红色的球,然后将其擦成绿色,因此您会看到一个红色的球留下绿色的痕迹。 Replacing the green color by your background color would do the trick. 用您的背景色代替绿色就可以了。

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

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