升旗(驱动事件)
package v;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Image.*;
public class shen extends JFrame{
public shen()
{
add(new moving());
//add(new moving());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
shen frame=new shen();
frame.setTitle("sheng qi");
frame.setSize(800,700);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class moving extends JPanel{
Image image=new ImageIcon("2.gif").getImage();
int x=300;
int y=700;
public moving(){
Timer timer=new Timer(100,new TimerListener());
timer.start();
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(y<0){
y=y+getHeight();
}else
y=y-10;
g.drawImage(image, x,y,100,60,this);
}
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
}
}
}
}