java程序有关图片闪烁的问题!
packageplant;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Graphics2D;i...
package plant;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class DubbleBuffer extends JFrame{
public paintThread PT;
private Image iBuffer;
private Graphics gBuffer;
public int X=500;
public int y=1;
public void DoublieBuffer()
{
PT=new paintThread(this);
this.setBounds(200, 100, 1200, 600);
this.setLayout(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PT.start();
}
public void paint(Graphics g) {
Image img = Toolkit.getDefaultToolkit().getImage("E:\\workplace\\植物\\src\\image\\interface\\background1.jpg");
Image image=Toolkit.getDefaultToolkit().getImage("E:\\workplace\\植物\\src\\image\\Zombies\\Zombie\\Zombie"+y+".gif");
Image image1=Toolkit.getDefaultToolkit().getImage("E:\\workplace\\植物\\src\\image\\Plants\\SunFlower\\SunFlower.gif");
g.drawImage(img, 0, 0, img.getWidth(null),img.getHeight(null), this);
g.drawImage(image,X, 200, image.getWidth(null),image.getHeight(null),this);
g.drawImage(image1,400, 200,image1.getWidth(this),image1.getHeight(this), this);
}
public void update(Graphics g)
{
if(iBuffer==null)
{
iBuffer=createImage(this.getSize().width,this.getSize().height);
gBuffer=iBuffer.getGraphics();
}
gBuffer.setColor(getBackground());
// gBuffer.fillRect(0, 0, this.getSize().width, this.getSize().height);
paint(gBuffer);
gBuffer.dispose();
g.drawImage(iBuffer, 0,0, this);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DubbleBuffer DB=new DubbleBuffer();
DB.DoublieBuffer();
}
}
class paintThread extends Thread{
DubbleBuffer DB;
public paintThread(DubbleBuffer DB)
{
this.DB=DB;
}
public void run()
{
while(true)
{
try {
sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
;
if(DB.X>400)
DB.X-=1;
if(DB.X==400)
{
DB.y=7;
}
DB.repaint();
}
}
}
这个程序为什么,我另外加的两张小图片会出现闪烁,我用了双缓冲好像没什么用哦!!难道只对第一张大图起作用? 展开
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class DubbleBuffer extends JFrame{
public paintThread PT;
private Image iBuffer;
private Graphics gBuffer;
public int X=500;
public int y=1;
public void DoublieBuffer()
{
PT=new paintThread(this);
this.setBounds(200, 100, 1200, 600);
this.setLayout(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PT.start();
}
public void paint(Graphics g) {
Image img = Toolkit.getDefaultToolkit().getImage("E:\\workplace\\植物\\src\\image\\interface\\background1.jpg");
Image image=Toolkit.getDefaultToolkit().getImage("E:\\workplace\\植物\\src\\image\\Zombies\\Zombie\\Zombie"+y+".gif");
Image image1=Toolkit.getDefaultToolkit().getImage("E:\\workplace\\植物\\src\\image\\Plants\\SunFlower\\SunFlower.gif");
g.drawImage(img, 0, 0, img.getWidth(null),img.getHeight(null), this);
g.drawImage(image,X, 200, image.getWidth(null),image.getHeight(null),this);
g.drawImage(image1,400, 200,image1.getWidth(this),image1.getHeight(this), this);
}
public void update(Graphics g)
{
if(iBuffer==null)
{
iBuffer=createImage(this.getSize().width,this.getSize().height);
gBuffer=iBuffer.getGraphics();
}
gBuffer.setColor(getBackground());
// gBuffer.fillRect(0, 0, this.getSize().width, this.getSize().height);
paint(gBuffer);
gBuffer.dispose();
g.drawImage(iBuffer, 0,0, this);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DubbleBuffer DB=new DubbleBuffer();
DB.DoublieBuffer();
}
}
class paintThread extends Thread{
DubbleBuffer DB;
public paintThread(DubbleBuffer DB)
{
this.DB=DB;
}
public void run()
{
while(true)
{
try {
sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
;
if(DB.X>400)
DB.X-=1;
if(DB.X==400)
{
DB.y=7;
}
DB.repaint();
}
}
}
这个程序为什么,我另外加的两张小图片会出现闪烁,我用了双缓冲好像没什么用哦!!难道只对第一张大图起作用? 展开
4个回答
展开全部
双缓冲本来就不怎么好用 最笨的方法 你把图面闪烁做成刷新 间隔短点 在白屏和图片之间很快的切换 就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询