java 双缓冲 初始化问题
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Graphics;
public class picmove extends Canvas{
int y;
private Image imagebuff=this.createImage(300,600);//双缓冲
private Graphics graphics=imagebuff.getGraphics();
public picmove(int i)
{
y=i;
setPreferredSize(new Dimension(300, 600));
}
public void paint(Graphics g)
{
update(g);
}
public void update(Graphics g){
graphics.setColor(Color.RED);
graphics.fillOval(90, y, 80, 80);
g.drawImage(imagebuff, 0, 0, null);
}
public static void main(String[] args){
int x=0;
while(true){
if(x<=500)
{
x=x+10;
new picmove(x);
}
else{System.exit(1);}
}
}
}
Eclipse运行了好几次,一直说是private Graphics graphics=imagebuff.getGraphics();有问题,求帮助!!!! --------------------编程问答-------------------- 是不是由于getGraphics()方法是abstract的导致?! --------------------编程问答-------------------- createImage api来看,是因为this对象不是displayable的才创建了null的image
像这样就可以
JFrame jf = new JFrame();
jf.add(this);
imagebuff = this.createImage(300, 600);// 双缓冲
graphics = imagebuff.getGraphics();
y = i;
setPreferredSize(new Dimension(300, 600)); --------------------编程问答-------------------- 这一句:
private Image imagebuff=this.createImage(300,600);//双缓冲
改为:
private Image imagebuff = new BufferedImage(300, 600, BufferedImage.TYPE_3BYTE_BGR);
缓冲用BufferedImage。
补充:Java , Java SE