1.png
2.png
3.png
4.png
5.png
6.png
7.png
8.png
9.png
public class Test extends JFrame {
private static final long serialVersionUID = 1L;
private MyPanel panel;
public static void main(String[] args) {
Test t = new Test();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
public Test() {
setTitle("Gyi-neng");
panel = new MyPanel();
getContentPane().add(panel);
pack();
}
public class MyPanel extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
//private Image bkImage; //用于双缓冲的缓存图片
private ArrayList<Gyineng> gn;
public MyPanel() {
this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
gn = new ArrayList<Gyineng>();
this.addMouseListener(this);
this.setBackground(Color.black);
Thread t = new Thread(new PanelRepaint());
t.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(Gyineng a:gn) {
a.draw(g);
}
}
/*public void update(Graphics g)
{
if(null==bkImage) { bkImage=this.createImage(WIDTH,HEIGHT); }
g.drawImage(bkImage,0,0,null); //把虚拟图片的内容画到当前窗口
Graphics gBkImg=bkImage.getGraphics();
gBkImg.clearRect(0,0,WIDTH,HEIGHT); //先清空图片
this.paint(gBkImg); //将图像先画到虚拟图片上
}*/
private class PanelRepaint implements Runnable {
public void run() {
while(true) {
Gyineng g = null;
for(int i = 0; i < gn.size(); ++i) {
g = gn.get(i);
g.move();
}
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public ArrayList<Gyineng> getGn() {
return gn;
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
gn.add(new Gyineng(this,arg0.getX(),arg0.getY()));
repaint();
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
public class Gyineng {
private Image[] img;
private int num;
private int step;
private int x;
private int y;
private int limit;
private int speed;
private int direct;
private MyPanel panel;
public Gyineng(MyPanel _p,int _x, int _y) {
panel = _p;
num = 9;
x = _x;
y = _y;
limit = 500; //技能范围
speed = 15;
direct = (int) (Math.random() * 2);
img = new Image[num];
for(int i = 0; i < num; ++i) {
String picName = "image/箭矢/"+String.valueOf(i+1)+".png";
img[i] = new ImageIcon(picName).getImage();
}
step = 0;
}
public void draw(Graphics g) {
g.drawImage(img[step],x,y,panel);
++step;
if(step == img.length)
step = 0;
}
public void move() {
if(direct == 0)
x -= speed;
else
x += speed;
if(isOutOfLimit())
panel.getGn().remove(this);
}
public boolean isOutOfLimit() {
limit -= speed;
if(limit < 0)
return true;
return false;
}
}
}