Java小程序双缓冲问题(和循环问题)
由于某种原因,当我尝试对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在绘制方法中检查尺寸。 如果尺寸发生变化,则创建一个新图像,然后绘制到它,并存储新的尺寸信息。 另外,不要调用重绘,因为您只是递归调用绘画。
如果在 start 方法中放置一个无限循环,则线程永远不会退出 start 方法。
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.