packageaaaa;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.ImageIcon;importjavax.swin...
package aaaa;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class flag extends JFrame{
public flag(){
add(new flagPanel());
}
public static void main(String[] args) {
flag f1=new flag();
f1.setTitle("flag");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(800,600);
f1.setVisible(true);
}
static class flagPanel extends JPanel{
private int x=0;
private int y=0;
private int xnum;
private int ynum;
Image I1=new ImageIcon("C:/Users/dongwei/Pictures/f98089b4cac659538ad4b223.jpg").getImage();
public flagPanel(){
Timer timer=new Timer(1000,new TimerListener());
timer.start();
}
protected void paintComponent(Graphics g){
super.paintComponents(g);
if(x<=0){
xnum=x;
x+=10;
}
if(x>=getSize().width-80){
xnum=x;
x-=10;
}
if((x>0)&&(x
if(xnum
x+=10;
}
else{
x-=10;
}
}
if(y<=0){
ynum=y;
y+=10;
}
if(y>=getSize().height-60){
ynum=y;
y-=10;
}
if((y>0)&&(y
if(ynum
y+=10;
}
else{
y-=10;
}
}
g.drawImage(I1,x,y,80,60,this);
}
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
}
}
}
}
为什么程序显示出来的图标在Frame中移动时,后面有拖影,也就是原来画上去的图标不消失,怎样改才能让后面的拖影去掉?谢谢!
展开