java双缓冲问题!!
importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Tool...
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class zy23_1 extends JFrame{
BufferedImage x=null;
Graphics g1=null;
Image age=null;
Image age1=null;
Toolkit t1=null;
int i=0;
zy23_1()
{
A a=new A();
a.start();
x=new BufferedImage(400,400,BufferedImage.TYPE_INT_RGB);
g1=x.getGraphics();
t1=Toolkit.getDefaultToolkit();
age=t1.createImage("22.gif");
age1=t1.createImage("22.JPG");
this.setBounds(200,200,202,154);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void paint(Graphics g)
{
g1.setColor(this.getBackground());
g1.fillRect(0,0,202,154);
g.drawImage(age1,0,0,200,150,i,96,96,i,this);
g.drawImage(age,0,0,200,150,0,0,200,150,this);
}
public static void main(String[] args) {
new zy23_1();
}
class A extends Thread
{
public void run()
{
while(true){
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
repaint();
if(i>=96) i=0;
}
}
}
}
我运行时图片还是会不停的闪烁,我已经做了双缓冲。哪里出问题了??懂的朋友请讲解下!谢谢 展开
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class zy23_1 extends JFrame{
BufferedImage x=null;
Graphics g1=null;
Image age=null;
Image age1=null;
Toolkit t1=null;
int i=0;
zy23_1()
{
A a=new A();
a.start();
x=new BufferedImage(400,400,BufferedImage.TYPE_INT_RGB);
g1=x.getGraphics();
t1=Toolkit.getDefaultToolkit();
age=t1.createImage("22.gif");
age1=t1.createImage("22.JPG");
this.setBounds(200,200,202,154);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void paint(Graphics g)
{
g1.setColor(this.getBackground());
g1.fillRect(0,0,202,154);
g.drawImage(age1,0,0,200,150,i,96,96,i,this);
g.drawImage(age,0,0,200,150,0,0,200,150,this);
}
public static void main(String[] args) {
new zy23_1();
}
class A extends Thread
{
public void run()
{
while(true){
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
repaint();
if(i>=96) i=0;
}
}
}
}
我运行时图片还是会不停的闪烁,我已经做了双缓冲。哪里出问题了??懂的朋友请讲解下!谢谢 展开
2个回答
展开全部
paint()方法中其实并未实现双缓冲,双缓冲是在后台虚拟的Image中绘制完成后再返回当前窗口,而这个代码直接调用g进行两次drawImage,其实就是直接对当前窗口进行绘制,有覆盖区域,会产生闪烁,,按照下列修改paint()方法就可以了
public void paint(Graphics g) {
g1.setColor(this.getBackground());
g1.fillRect(0,0,202,154);
g1.drawImage(age1,0,0,200,150,i,96,96,i,this);
g1.drawImage(age,0,0,200,150,0,0,200,150,this);
g.drawImage(x,0,0,this); //把x图像返回到当前窗口
}
public void paint(Graphics g) {
g1.setColor(this.getBackground());
g1.fillRect(0,0,202,154);
g1.drawImage(age1,0,0,200,150,i,96,96,i,this);
g1.drawImage(age,0,0,200,150,0,0,200,150,this);
g.drawImage(x,0,0,this); //把x图像返回到当前窗口
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询