Java小程序双缓冲问题(和循环问题)

发布于 2024-07-14 09:41:34 字数 1511 浏览 5 评论 0原文

由于某种原因,当我尝试对 Java 小程序进行双缓冲时,它会显示一个白色方块,即使我没有告诉它。 另外,如果我尝试在 start() 中放置一个循环,即使 repaint() 在最后,我也只会得到一个白屏。

/**
* @(#)vo4k.java
*
* vo4k Applet application
*
* @author William Starkovich
* @version 1.00 2009/2/21
*/

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

public class vo4k extends Applet implements KeyListener
{
obj p1, e1;
boolean[] keys;
boolean end = false;
Graphics g2d;
Dimension size;
Image buf; 

public void init() 
{
    keys = new boolean[256];

    for(int i = 0; i < 256; i++)
        keys[i] = false;

    addKeyListener(this);
    p1 = new obj();
    p1.x = 0;

    size = getSize();
    buf = createImage(size.width,size.height); 
    g2d = buf.getGraphics();
}

public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {keys[e.getKeyCode()] = true;}
public void keyReleased(KeyEvent e) {keys[e.getKeyCode()] = false;}
public void controls()
{
    if(keys[KeyEvent.VK_SPACE])
        end = true;

    if(keys[KeyEvent.VK_W])
        p1.x += 10;

}

public void start()
{
//  while(!end)
//  {


//  }
}

public void paint(Graphics g) 
{

    controls();
    //g2d = (Graphics2D) g;
    g2d.setColor(Color.RED);
    g2d.fillRect(0,0,size.width,size.height);
    g2d.setColor(Color.BLUE);
    g2d.drawString("Welcome 2 Java!!", (int) 50, 60 );
    //g2d.drawString("Welcome to Java!!", (int) p1.x, 60 );


    g.drawImage(buf, 0, 0, this);
    repaint();
}
}

class obj
{
    double x,y,l,a,s;
}

For some reason when I try to double buffer my Java applet it shows up a white square even though I'm not telling it to. Also if I try to put a loop in start() I get only a white screen even if repaint() is at the end.

/**
* @(#)vo4k.java
*
* vo4k Applet application
*
* @author William Starkovich
* @version 1.00 2009/2/21
*/

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

public class vo4k extends Applet implements KeyListener
{
obj p1, e1;
boolean[] keys;
boolean end = false;
Graphics g2d;
Dimension size;
Image buf; 

public void init() 
{
    keys = new boolean[256];

    for(int i = 0; i < 256; i++)
        keys[i] = false;

    addKeyListener(this);
    p1 = new obj();
    p1.x = 0;

    size = getSize();
    buf = createImage(size.width,size.height); 
    g2d = buf.getGraphics();
}

public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {keys[e.getKeyCode()] = true;}
public void keyReleased(KeyEvent e) {keys[e.getKeyCode()] = false;}
public void controls()
{
    if(keys[KeyEvent.VK_SPACE])
        end = true;

    if(keys[KeyEvent.VK_W])
        p1.x += 10;

}

public void start()
{
//  while(!end)
//  {


//  }
}

public void paint(Graphics g) 
{

    controls();
    //g2d = (Graphics2D) g;
    g2d.setColor(Color.RED);
    g2d.fillRect(0,0,size.width,size.height);
    g2d.setColor(Color.BLUE);
    g2d.drawString("Welcome 2 Java!!", (int) 50, 60 );
    //g2d.drawString("Welcome to Java!!", (int) p1.x, 60 );


    g.drawImage(buf, 0, 0, this);
    repaint();
}
}

class obj
{
    double x,y,l,a,s;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も星光 2024-07-21 09:41:35

在绘制方法中检查尺寸。 如果尺寸发生变化,则创建一个新图像,然后绘制到它,并存储新的尺寸信息。 另外,不要调用重绘,因为您只是递归调用绘画。

如果在 start 方法中放置一个无限循环,则线程永远不会退出 start 方法。

public void paint(Graphics g) 
{

        controls();
        Dimension currentSize  = getSize();
        if ( ! currentSize.equals(size) ) {
          size = currentSize;
          buf = createImage(size.width,size.height); 
          g2d = buf.getGraphics();
        }
        //g2d = (Graphics2D) g;
        g2d.setColor(Color.RED);
        g2d.fillRect(0,0,size.width,size.height);
        g2d.setColor(Color.BLUE);
        g2d.drawString("Welcome 2 Java!!", (int) 50, 60 );
        //g2d.drawString("Welcome to Java!!", (int) p1.x, 60 );


        g.drawImage(buf, 0, 0, this);

}

Put a check for size in the paint method. If the size has changed, create a new image and then draw to it, and store the new size information. Also, don't call repaint as you are just have a recursive call to paint.

If you put an unending loop in the start method then the thread never exits the start method.

public void paint(Graphics g) 
{

        controls();
        Dimension currentSize  = getSize();
        if ( ! currentSize.equals(size) ) {
          size = currentSize;
          buf = createImage(size.width,size.height); 
          g2d = buf.getGraphics();
        }
        //g2d = (Graphics2D) g;
        g2d.setColor(Color.RED);
        g2d.fillRect(0,0,size.width,size.height);
        g2d.setColor(Color.BLUE);
        g2d.drawString("Welcome 2 Java!!", (int) 50, 60 );
        //g2d.drawString("Welcome to Java!!", (int) p1.x, 60 );


        g.drawImage(buf, 0, 0, this);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文