java 双缓冲问题
importjava.awt.*;importjava.awt.event.*;importjava.awt.Graphics;publicclasstestextend...
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class test extends Frame implements Runnable{
int x=0,y=160;
int dx=5,dy=5;
Image bufferPage=null,img;
public static void main(String args[]){
test workStar =new test();
}
public test(){
super("test");
setSize(350,350);
setVisible(true);
Toolkit tk=Toolkit.getDefaultToolkit();
img=tk.getImage("1.gif");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);
}
}
);
new Thread(this).start();
}
public void run(){
while(true){
x=x+dx;
y=y+dy;
repaint();
if(x<=0)dx=5;
else if((x+50)>=getWidth())dx=-5;
if(y<=0)dy=5;
else if((y+50)>=getHeight())dy=-5;
try{
Thread.sleep(230);
}
catch(InterruptedException e){;}
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
Graphics bufferg;
if(bufferPage==null){
bufferPage=createImage(350,350);
}
bufferg=bufferPage.getGraphics();
bufferg.setColor(getBackground());
bufferg.fillRect(0,0, this.getSize().width, this.getSize().height);
bufferg.drawImage(img, x,y,this);
g.drawImage(bufferPage,0,0,this);
}
}
看看这段代码,为什么没有实现双缓冲以及如何实现。 展开
import java.awt.event.*;
import java.awt.Graphics;
public class test extends Frame implements Runnable{
int x=0,y=160;
int dx=5,dy=5;
Image bufferPage=null,img;
public static void main(String args[]){
test workStar =new test();
}
public test(){
super("test");
setSize(350,350);
setVisible(true);
Toolkit tk=Toolkit.getDefaultToolkit();
img=tk.getImage("1.gif");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);
}
}
);
new Thread(this).start();
}
public void run(){
while(true){
x=x+dx;
y=y+dy;
repaint();
if(x<=0)dx=5;
else if((x+50)>=getWidth())dx=-5;
if(y<=0)dy=5;
else if((y+50)>=getHeight())dy=-5;
try{
Thread.sleep(230);
}
catch(InterruptedException e){;}
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
Graphics bufferg;
if(bufferPage==null){
bufferPage=createImage(350,350);
}
bufferg=bufferPage.getGraphics();
bufferg.setColor(getBackground());
bufferg.fillRect(0,0, this.getSize().width, this.getSize().height);
bufferg.drawImage(img, x,y,this);
g.drawImage(bufferPage,0,0,this);
}
}
看看这段代码,为什么没有实现双缓冲以及如何实现。 展开
3个回答
展开全部
应该把双缓冲放在update方法里面的
调用情况是repaint() -> update(Graphics g) -> paint(Graphics g)
具体方法如下
public void update(Graphics g){
Graphics bufferg;
if(bufferPage==null){
buffer=createImage(350,350);
}
bufferg=bufferPage.getGraphics();
Color c = bufferg.getColor();
bufferg.setColor(getBackground());
bufferg.fillRect(0,0, this.getSize().width, this.getSize().height);
bufferg.drawImage(img, x,y,this);
paint(bufferg);
g.drawImage(bufferPage,0,0,this);
bufferg.setColor(c);
}
public void paint(Graphics g){
g.drawImage(img,0,0,this);
}
调用情况是repaint() -> update(Graphics g) -> paint(Graphics g)
具体方法如下
public void update(Graphics g){
Graphics bufferg;
if(bufferPage==null){
buffer=createImage(350,350);
}
bufferg=bufferPage.getGraphics();
Color c = bufferg.getColor();
bufferg.setColor(getBackground());
bufferg.fillRect(0,0, this.getSize().width, this.getSize().height);
bufferg.drawImage(img, x,y,this);
paint(bufferg);
g.drawImage(bufferPage,0,0,this);
bufferg.setColor(c);
}
public void paint(Graphics g){
g.drawImage(img,0,0,this);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询