1package game6;
2
3/**//*
4 这里我们按空格后就会出现一个子弹形状的东西向上移动,用双缓冲实现的
5*/
6
7import java.awt.*;
8import java.awt.event.*;
9import javax.swing.*;
10
11class TestCanvas extends Canvas implements Runnable
12{
13 private int x,y; //那个圆的坐标
14
15 private final int ovalWidth=50;
16
17 private final int bulletHeight=10; //确定子弹大小的
18
19 private int speed=1; //子弹的速度
20
21 private int beginX,beginY; //子弹头的坐标
22
23 private int bulletX[]; // 绘制子弹形状的数组 横纵坐标
24 private int bulletY[];
25 private boolean isDrawBullet; //是否画子弹 true表示是,false表示否
26
27 TestCanvas()
28 {
29 this.setSize(400, 400);
30 x=200;
31 y=50;
32 isDrawBullet=false;
33 bulletX=new int[5];
34 bulletY=new int[5];
35 initBeginXY();
36 setBulletXY();
37 }
38
39 public void setBulletXY() //设置子弹5个点的坐标
40 {
41 bulletX[0]=beginX;
42 bulletX[1]=beginX+bulletHeight/2;
43 bulletX[2]=beginX+bulletHeight/2;
44 bulletX[3]=beginX-bulletHeight/2;
45 bulletX[4]=beginX-bulletHeight/2;
46 bulletY[0]=beginY;
47 bulletY[1]=beginY+bulletHeight;
48 bulletY[2]=beginY+2*bulletHeight;
49 bulletY[3]=beginY+2*bulletHeight;
50 bulletY[4]=beginY+bulletHeight;
51 }
52
53 public void initBeginXY() //初始化子弹头坐标
54 {
55 beginX=200;
56 beginY=200;
57 }
58
59 public void leftMove() //左移
60 {
61 x-=50;
62 if(x<=-50) x=this.getWidth();
63 repaint();
64 }
65
66 public void rightMove() //右移
67 {
68 x+=50;
69 if(x>=this.getWidth()) x=-50;
70 repaint();
71 }
72
73 public void upMove() //上移
74 {
75 y-=50;
76 if(y<=-50) y=this.getHeight();
77 repaint();
78 }
79
80 public void downMove() //下移
81 {
82 y+=50;
83 if(y>=this.getHeight()) y=-50;
84 repaint();
85 }
86
87 public void drawBullet(Graphics g) //画出子弹
88 {
89 g.fillPolygon(bulletX, bulletY, 5);
90 }
91
92 public void doOrNotdoDrawBullet(boolean tag) //是否画出子弹
93 {
94 isDrawBullet=tag;
95 repaint();
96 }
97
98 public boolean getIsDrawBullet()
99 {
100 return isDrawBullet;
101 }
102
103 public void paint(Graphics g)
104 {
105 g.fillOval(x, y, ovalWidth, ovalWidth);
106 }
107
108 public void update(Graphics g) //实现消除闪烁
109 {
110 Image image; //创建一张和原来大小一样的图像
111 image=this.createImage(this.getWidth(),this.getHeight());
112 Graphics gp=image.getGraphics(); //获得此创建图像的 画笔
113 paint(gp); //调用paint 对此图像作画
114
115 if(isDrawBullet) drawBullet(gp); //可能画子弹
116
117 g.drawImage(image, 0, 0, this); //将此图像画到(this)画布上
118 }
119
120 public void run()
121 {
122 while(true)
123 {
124 if( isDrawBullet&&
125 beginX>=0&&beginX<=this.getWidth()&&
126 beginY>=0&&beginY<=this.getHeight()&&
127 !(beginX>=x&&beginX<=x+ovalWidth&&
128 beginY>=y&&beginY<=y+ovalWidth) )
129 {
130 beginY-=speed; //移动
131 setBulletXY(); //设置
132 repaint(); //重绘
133 }
134 else
135 doOrNotdoDrawBullet(false); //子弹消失
136 try
137 {
138 Thread.sleep(10);
139 }
140 catch(InterruptedException e)
141 {
142
143 }
144 }
145 }
146
147}
148
149class MyKeyAdapter extends KeyAdapter
150{
151 TestCanvas tc;
152 Thread th;
153 MyKeyAdapter(TestCanvas tc)
154 {
155 this.tc=tc;
156 th=new Thread(this.tc);
157 }
158 public void keyPressed(KeyEvent e)
159 {
160 if(e.getKeyCode()==KeyEvent.VK_LEFT)
161 tc.leftMove();
162 else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
163 tc.rightMove();
164 else if(e.getKeyCode()==KeyEvent.VK_UP)
165 tc.upMove();
166 else if(e.getKeyCode()==KeyEvent.VK_DOWN)
167 tc.downMove();
168 if(e.getKeyCode()==KeyEvent.VK_SPACE)
169 {
170 if(!tc.getIsDrawBullet()) //判断子弹是否已画
171 {
172 tc.initBeginXY(); //要画子弹时初始化
173 tc.setBulletXY(); //立即设置
174 }
175 tc.doOrNotdoDrawBullet(true);
176 try
177 {
178 th.start();
179 }
180 catch(IllegalThreadStateException ee)
181 {
182
183 }
184 }
185 }
186}
187
188public class BulletAppearAndDis extends JFrame
189{
190 BulletAppearAndDis()
191 {
192 super("学习如何在按空格后出现一个会自动移动的子弹,且是画的");
193 setBounds(300, 150, 400, 400);
194 setVisible(true);
195 this.addWindowListener(new WindowAdapter()
196 {
197 public void windowClosing(WindowEvent e)
198 {
199 dispose();
200 System.exit(0);
201 }
202 });
203 }
204 public static void main(String[] args)
205 {
206 BulletAppearAndDis jf=new BulletAppearAndDis();
207 TestCanvas tc=new TestCanvas();
208 tc.addKeyListener(new MyKeyAdapter(tc));
209 jf.add(tc);
210 tc.requestFocusInWindow();
211 }
212}