新手求助,java在消除图像闪烁以后会留下影子,不知道怎么消除这个影子。
这个是程序运行的效果,程序如下:packagetest;importjava.awt.event.*;importjava.awt.*;publicclassMybaid...
这个是程序运行的效果,程序如下:
package test;
import java.awt.event.*;
import java.awt.*;
public class Mybaidu extends Frame implements Runnable {
Image img,bufferPage=null;
int x=0,y=10,dx=5,dy=0;
int bx,by,dbx=0,dby=-5,bflag=0;
public Mybaidu(){
super("smwanyi a ");
setSize(2000,2000);
Toolkit tk=Toolkit.getDefaultToolkit();
img=tk.getImage("1.gif");
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
enableEvents(AWTEvent.KEY_EVENT_MASK);
setVisible(true);
new Thread(this).start();
}
public void processWindowEvent(WindowEvent e){
if(e.getID()==WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
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(30);}
catch(InterruptedException e){;}
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g)
{
Graphics bufferg;
if(bufferPage==null)
bufferPage=createImage(3460,3460);
bufferg=bufferPage.getGraphics();
bufferg.drawImage(img, x, y, this);
bufferg.dispose();
g.drawImage(bufferPage,getInsets().left,getInsets().top,this);
}
public static void main(String argument[]){
Mybaidu bf=new Mybaidu();
}
} 展开
package test;
import java.awt.event.*;
import java.awt.*;
public class Mybaidu extends Frame implements Runnable {
Image img,bufferPage=null;
int x=0,y=10,dx=5,dy=0;
int bx,by,dbx=0,dby=-5,bflag=0;
public Mybaidu(){
super("smwanyi a ");
setSize(2000,2000);
Toolkit tk=Toolkit.getDefaultToolkit();
img=tk.getImage("1.gif");
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
enableEvents(AWTEvent.KEY_EVENT_MASK);
setVisible(true);
new Thread(this).start();
}
public void processWindowEvent(WindowEvent e){
if(e.getID()==WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
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(30);}
catch(InterruptedException e){;}
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g)
{
Graphics bufferg;
if(bufferPage==null)
bufferPage=createImage(3460,3460);
bufferg=bufferPage.getGraphics();
bufferg.drawImage(img, x, y, this);
bufferg.dispose();
g.drawImage(bufferPage,getInsets().left,getInsets().top,this);
}
public static void main(String argument[]){
Mybaidu bf=new Mybaidu();
}
} 展开
1个回答
展开全部
是左面的条纹影子吗?那个影子是长时间存在还是存在一会就消失了?
追问
长时间存在。。我就是希望这个图片能够流畅运动,不希望留下这个影子,应该怎么办
追答
paint的时候,bufferPage应该每次都是一个新对象。而不是是原来的图片,只改变坐标
public void paint(Graphics g)
{
Graphics bufferg;
if(bufferPage==null)
bufferPage=createImage(3460,3460);
bufferg=bufferPage.getGraphics();
bufferg.drawImage(img, x, y, this);
bufferg.dispose();
g.drawImage(bufferPage,getInsets().left,getInsets().top,this);
}
这个方法中,去掉 if(bufferPage==null) 这一行就行了
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询