绘图想用双缓冲,可生成的Image总是空的。

lefteye 2004-07-22 05:00:59
我用双缓冲机制绘图,可发现生成的Image总是空的。
于是用JFrame测试一下。发现总是无法生成Image。请教各位大侠。

以下是用JFrame测试的代码:
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Color;
public class Temp extends JFrame{
Image data;
public Temp(){
try{
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400,400);
this.setVisible(true);
}catch(Exception ee){
ee.printStackTrace();
}
}
public static void main(String []args){
new Temp();
}
public void paint1(){
if(data==null)data=createImage(100,100);
else {
Graphics2D g=(Graphics2D)data.getGraphics();
g.setColor(new Color(0,0,255));
g.fillRect(0,0,50,50);
}
}
public void paint(Graphics g){
if(data!=null)g.drawImage(data,50,50,null);
else System.out.println("data is null");
}
}
...全文
99 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lefteye 2004-07-22
  • 打赏
  • 举报
回复
大侠,我做的错了。
lEFTmOON 2004-07-22
  • 打赏
  • 举报
回复
原来这一贴有50分:)

VolatileImage offScreenImage;

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();

offScreenImage = gc.createCompatibleVolatileImage(imageWidth, imageHeight); //创建图像缓冲
offScreenGraphics = offScreenImage.createGraphics(); //取得缓冲的graphics对象

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧