【问题标题】:[Java]Double buffering failing to double buffer[Java]双缓冲失败双缓冲
【发布时间】:2011-04-05 03:51:32
【问题描述】:

好吧,正如标题所说,我在使用双缓冲时遇到了问题。我阅读了这本 Java 电子书,但它并没有为您提供他们所教内容的代码——至少不是完整的。所以我必须做很多猜测工作。

目标:在小程序中弹跳球。

球仍在闪烁的方式不起作用。 Aka 双缓冲无法正常工作。

我使用三个类,球类、双缓冲类和 MainApplet 类。 MainApplet 扩展了双缓冲,ball 类扩展了 MainApplet

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

@SuppressWarnings("serial")

public class MainApplet extends DoubleBuffering implements Runnable {

    public Ball ball;
    public Graphics g;

    private Thread ticker;
    public boolean running = false;

    public void init() {

        setSize(100,100);
        ball = new Ball(getWidth() / 5f, getHeight() / 4f, 1.5f,
                2.3f, 12, Color.red);
        moveBall();
    }

    public void run() {
        while(running) {
            try {
                Rectangle bou = new Rectangle(getWidth(), getHeight());
                ball.move(bou);
                ball.update(getGraphics());
                Thread.sleep(1000 / 15);
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
            repaint();
        }
    }

    public void moveBall() {
        start();
    }

    public synchronized void start() {
        running = true;
        ticker = new Thread(this);
        ticker.setPriority(Thread.MIN_PRIORITY + 1);
        ticker.start();
    }


    public synchronized void stop() {
        running = false;
        ticker.stop();
    }
}


import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class DoubleBuffering extends Applet
{
    Image offScreenBuffer;

    @SuppressWarnings("deprecation")
    public void update(Graphics g)
    {
        System.out.println("We are buffing");
        Graphics gr; 
        if (offScreenBuffer==null ||
                (! (offScreenBuffer.getWidth(this) == this.size().width
                        && offScreenBuffer.getHeight(this) == this.size().height)))
        {
            offScreenBuffer = this.createImage(size().width, size().height);
        }
        gr = offScreenBuffer.getGraphics();
        System.out.println("Something else");
        paint(gr); 
        g.drawImage(offScreenBuffer, 0, 0, this);     
    }
}


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;


public class Ball extends MainApplet{

    int size;
    private Color color;
    public float x, y, dx, dy;

    public Ball ball;
    public int width, height;
    public Image offscreenImage;
    public Graphics offscr;
    private MainApplet ma;


    Ball (float x, float y, float dx, float dy, int size,
            Color color) {
        this.x = x;
        this.y = y;
        this.dy = dx;
        this.dy = dy;
        this.color = color;
        this.size = size;

    }

    public void draw (Graphics g) {
        g.setColor(this.color);
        g.fillOval((int) x, (int) y, size, size);
    }

    public void update(Graphics g)  {

        g.clearRect(0, 0, getWidth(), getHeight());
        draw(g);

    }

    public void move(Rectangle bounds) {
        // Add velocity values dx/dy to position to
        // ball s new position
        x += dx;
        y += dy;

        // Check for collision with left edge
        if (x < bounds.x && dx < 0) {
            dx = -dx;
            x -= 2 * (x - bounds.x);
        } 
        // Check for collision with right edge
        else if (x + size >  bounds.x + bounds.width &&
                dx > 0) {
            dx = -dx;
            x -= 2 * ((x + size) - (bounds.x + bounds.width));
        }
        // Checks for collision with top edge
        else if (y < bounds.y && dy < 0) {
            dy = -dy;
            y -= 2 * (y - bounds.y);
        }
        // Checks for collision with bottom edge
        else if (y + size > bounds.y + bounds.height && dy >0) {
            dy = -dy;
            y -= 2 * ((y + size) - (bounds.y + bounds.width));
        }
    }
}  

注意:我不太确定这段代码会如何产生 >.

无论如何,不​​要太讨厌我的约定,我还是个新手。提示和答案将不胜感激。谢谢。

【问题讨论】:

    标签: java double buffering


    【解决方案1】:

    不要调用Thread.Sleep(),而是尝试像这样使用swing Timer。

    private Timer t;
    
    public void moveBall() {
    t = new Timer(1000/15, new ActionListener() {
    
            public void actionPerformed(ActionEvent e) {
                 Rectangle bou = new Rectangle(getWidth(), getHeight());
                    ball.move(bou);
                    ball.update(getGraphics());
                    repaint();
            }
        });
        t.start();
    }
    
    public void destroy() {
        if(t!=null) t.stop();
        super.destroy();
    }
    

    这应该至少有一点帮助。

    【讨论】:

    • 差不多。我不知道你可以这样做。感谢那。否则,我仍然在同一个地方。
    【解决方案2】:

    我假设 DoubleBuffering 类是一些教程类。我猜它源自 Applet 而不是 JApplet。如果您使用 JApplet,您将默认获得双缓冲。

    【讨论】:

    • 也没有真正为我做任何事情。结果相同。
    猜你喜欢
    • 2011-08-20
    • 2012-03-22
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 2011-01-05
    • 2013-08-07
    • 2011-02-20
    相关资源
    最近更新 更多