日期:2014-05-20 浏览次数:20833 次
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class ReflashBack extends JFrame{
public static void main(String[] args){
new ReflashBack();
}
public ReflashBack(){
ImageComponent com=new ImageComponent();
add(com);
setVisible(true);
setSize(1366,768);
Timer timer = new Timer();
//在0.1秒后执行此任务,每次间隔0.2秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
timer.schedule(com.mytask(), 100, 200);
}
}
class ImageComponent extends JComponent{
int y=0;
public ImageComponent(){
try{
img=ImageIO.read(new File("razer.jpg"));
}
catch(IOException e){
e.printStackTrace();
}
}
public TimerTask mytask() {
// TODO Auto-generated method stub
return new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
y+=20;
repaint();
}
};
}
public void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D) g;
if(img==null) return;
int imageWidth=img.getWidth(this);
int imageHeight=img.getHeight(this);
g.drawImage(img,0,0,null);
Rectangle2D.Double d=new Rectangle2D.Double(200,200+y,200,200);
g2.draw(d);
g2.setColor(Color.RED);
g2.fill(d);
}
private java.awt.Image img;
}