1
//例子1
2
import java.applet.*;import java.awt.*;
3
import java.awt.event.*;
4
public class Example18_1 extends Applet implements MouseListener
5

{ TextField text;
6
public void init()
7
{ text=new TextField(40); add(text);
8
addMouseListener(this) ;//向小程序增加鼠标事件监视器。
9
}
10
public void mousePressed(MouseEvent e)
11
{ text.setText("鼠标键按下了,位置是"+e.getX()+","+e.getY() );
12
}
13
public void mouseReleased(MouseEvent e)
14
{ text.setText(" 鼠标松开了,位置是"+e.getX()+","+e.getY() );
15
}
16
public void mouseEntered(MouseEvent e)
17
{ text.setText(" 鼠标进来了,位置是"+e.getX()+","+e.getY() );
18
}
19
public void mouseExited(MouseEvent e)
20
{ text.setText(" 鼠标走开了");
21
}
22
public void mouseClicked(MouseEvent e)
23
{ if(e.getClickCount()==2)
24
{ text.setText("鼠标键双击,位置:"+e.getX()+","+e.getY());
25
}
26
else
{}
27
}
28
}
29
30
//例子2
31
import java.awt.*;import java.awt.event.*;
32
class MyCanvas extends Canvas implements MouseListener
33

{ int left=-1,right=-1; //记录左、右键用的变量。
34
int x=-1,y=-1; //记录鼠标位置用的变量。
35
MyCanvas()
36
{ setSize(100,100);
37
setBackground(Color.cyan) ;
38
addMouseListener(this);
39
}
40
public void paint(Graphics g)
41
{ if(left==1)
42
{ g.drawOval(x-10,y-10,20,20);
43
}
44
else if(right==1)
45
{ g.drawRect(x-8,y-8,16,16);
46
}
47
}
48
public void mousePressed(MouseEvent e)
49
{ x=e.getX(); y=e.getY();
50
if(e.getModifiers()==InputEvent.BUTTON1_MASK)
51
{ left=1;right=-1;
52
repaint();
53
}
54
else if(e.getModifiers()==InputEvent.BUTTON3_MASK)
55
{ right=1; left=-1;
56
repaint();
57
}
58
}
59
public void mouseReleased(MouseEvent e)
{}
60
public void mouseEntered(MouseEvent e)
{}
61
public void mouseExited(MouseEvent e)
62
{ left=-1;right=-1;
63
repaint();
64
}
65
public void mouseClicked(MouseEvent e)
{}
66
public void update(Graphics g)
67
{ if(left==1||right==1)
68
{ paint(g);
69
}
70
else
71
{ super.update(g);
72
}
73
}
74
}
75
public class Example18_2
76

{ public static void main(String args[])
77
{ Frame f=new Frame();
78
f.setBounds(100,100,200,200);f.setVisible(true);
79
f.addWindowListener(new WindowAdapter() //适配器
80
{public void windowClosing(WindowEvent e)
81
{System.exit(0);
82
}
83
});
84
f.add(new MyCanvas(),BorderLayout.CENTER);//添加画布。
85
f.validate();
86
}
87
}
88
89
//例子3
90
import java.awt.*;import java.awt.event.*;
91
import java.applet.*;
92
public class Example18_3 extends Applet implements MouseListener
93

{ TextField text; Button button;
94
TextArea textArea;
95
public void init()
96
{ text=new TextField(10); text.addMouseListener(this);
97
button=new Button("按钮"); button.addMouseListener(this);
98
addMouseListener(this);
99
textArea=new TextArea(8,28);
100
add(button);add(text);add(textArea);
101
}
102
public void mousePressed(MouseEvent e)
103
{ if(e.getSource()==button)
104
{textArea.append("\n在按钮上鼠标按下,位置:"+"("+e.getX()+","+e.getY()+")");
105
}
106
else if(e.getSource()==text)
107
{textArea.append("\n在文本框上鼠标按下,位置:"+"("+e.getX()+","+e.getY()+")");
108
}
109
else if(e.getSource()==this)
110
{textArea.append("\n在容器上鼠标按下,位置:"+"("+e.getX()+","+e.getY()+")");
111
}
112
}
113
public void mouseReleased(MouseEvent e)
{}
114
public void mouseEntered(MouseEvent e)
{}
115
public void mouseExited(MouseEvent e)
{}
116
public void mouseClicked(MouseEvent e)
117
{ if(e.getClickCount()>=2)
118
textArea.setText("鼠标连击,位置:"+"("+e.getX()+","+e.getY()+")");
119
}
120
}
121
122
//例子4
123
import java.applet.*;import java.awt.*;import java.awt.event.*;
124
public class Example18_4 extends Applet implements MouseMotionListener
125

{ int x=-1,y=-1;
126
public void init()
127
{ setBackground(Color.green);
128
addMouseMotionListener(this);
129
}
130
public void paint(Graphics g)
131
{ if(x!=-1&&y!=-1)
132
{ g.setColor(Color.red);
133
g.drawLine(x,y,x,y);
134
}
135
}
136
public void mouseDragged(MouseEvent e)
137
{ x=(int)e.getX();y=(int)e.getY();
138
repaint();
139
}
140
public void mouseMoved(MouseEvent e)
{}
141
public void update(Graphics g)
142
{ paint(g);
143
}
144
}
145
146
//例子5
147
import java.applet.*;import java.awt.*;
148
import java.awt.event.*;
149
public class Example18_5 extends Applet
150
implements ActionListener,MouseMotionListener
151

{ int x=-1,y=-1,橡皮擦通知=0,清除通知=0;
152
Color c=new Color(255,0,0);int con=3;
153
Button b_red,b_blue,b_green,
154
清除,b_quit;
155
public void init()
156
{ addMouseMotionListener(this);
157
b_red=new Button("画红色图形");
158
b_blue=new Button("兰色图形");
159
b_green=new Button("画绿色图形");
160
b_quit=new Button("橡皮");
161
清除=new Button("清除");
162
add(b_red); add(b_green); add(b_blue); add(b_quit);add(清除);
163
b_red.addActionListener(this); b_green.addActionListener(this);
164
b_blue.addActionListener(this); b_quit.addActionListener(this);
165
清除.addActionListener(this);
166
}
167
public void paint(Graphics g)
168
{ if(x!=-1&&y!=-1&&橡皮擦通知==0&&清除通知==0)
169
{ g.setColor(c); g.fillOval(x,y,con,con);
170
}
171
else if(橡皮擦通知==1&&清除通知==0)
172
{ g.clearRect(x,y,10,10);
173
}
174
else if(清除通知==1&&橡皮擦通知==0)
175
{ g.clearRect(0,0,getSize().width,getSize().height);
176
}
177
}
178
public void mouseDragged(MouseEvent e)
179
{ x=(int)e.getX();y=(int)e.getY(); repaint();
180
}
181
public void mouseMoved(MouseEvent e)
{ }
182
public void update(Graphics g)
183
{ paint(g);
184
}
185
public void actionPerformed(ActionEvent e)
186
{ if(e.getSource()==b_red)
187
{ 橡皮擦通知=0;清除通知=0; c=new Color(255,0,0);
188
}
189
else if(e.getSource()==b_green)
190
{ 橡皮擦通知=0;清除通知=0; c=new Color(0,255,0);
191
}
192
else if(e.getSource()==b_blue)
193
{ 橡皮擦通知=0;清除通知=0; c=new Color(0,0,255);
194
}
195
if(e.getSource()==b_quit)
196
{ 橡皮擦通知=1;清除通知=0 ;
197
}
198
if(e.getSource()==清除)
199
{ 清除通知=1; 橡皮擦通知=0;repaint();
200
}
201
}
202
}
203
204
//例子6
205
import java.awt.*;import java.awt.event.*;import java.applet.*;
206
import javax.swing.SwingUtilities;
207
public class Example18_6 extends Frame implements MouseListener,MouseMotionListener
208

{ Button button;
209
int x,y;
210
boolean move=false;
211
Example18_6()
212
{ button=new Button("按钮");
213
button.addMouseListener(this);
214
button.addMouseMotionListener(this);
215
addMouseMotionListener(this);
216
setLayout(new FlowLayout());
217
add(button);
218
addWindowListener(new WindowAdapter()
219
{ public void windowClosing(WindowEvent e)
220
{ System.exit(0);
221
}
222
});
223
setBounds(10,10,200,180);
224
setVisible(true); validate();
225
}
226
public void mousePressed(MouseEvent e)
{}
227
public void mouseReleased(MouseEvent e)
228
{ move=false;
229
}
230
public void mouseEntered(MouseEvent e)
{}
231
public void mouseExited(MouseEvent e)
{}
232
public void mouseClicked(MouseEvent e)
{}
233
public void mouseMoved(MouseEvent e)
{}
234
public void mouseDragged(MouseEvent e)
235
{ Button b=null;
236
if(e.getSource() instanceof Button) //在按钮上拖动鼠标导致按钮上发生鼠标事件。
237
{ b=(Button)e.getSource();
238
move=true;
239
//将鼠标拖动事件转移到棋盘,导致棋盘上发生鼠标拖动事件:
240
e=SwingUtilities.convertMouseEvent(button,e,this);
241
//并将从按钮转移得到的鼠标事件e传递给mouseDragged方法的参数。
242
}
243
if(e.getSource()==this)
244
{ if(move&&b!=null)
245
{ x=e.getX(); y=e.getY();
246
int w=b.getSize().width,h=b.getSize().height;
247
b.setLocation(x-w/2,y-h/2);
248
}
249
}
250
}
251
public static void main(String args[])
252
{ new Example18_6();
253
}
254
}
255
256
//例子7
257
import java.applet.*;import java.awt.*;
258
import java.awt.event.*;
259
public class Example18_7 extends Applet implements
260
KeyListener
261

{ Button b[]=new Button[10];
262
int x,y;
263
public void init()
264
{ for(int i=0;i<=9;i++)
265
{ b[i]=new Button(""+i);
266
b[i].addKeyListener(this);
267
add(b[i]);
268
}
269
}
270
public void keyPressed(KeyEvent e)
271
{ Button button=(Button)e.getSource();
272
x=button.getBounds().x;
273
y=button.getBounds().y;
274
if(e.getKeyCode()==KeyEvent.VK_UP)
275
{ y=y-2;
276
if(y<=0) y=0;
277
button.setLocation(x,y);
278
}
279
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
280
{ y=y+2;
281
if(y>=300) y=300;
282
button.setLocation(x,y);
283
}
284
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
285
{ x=x-2;
286
if(x<=0) x=0;
287
button.setLocation(x,y);
288
}
289
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
290
{ x=x+2;
291
if(x>=300) x=300;
292
button.setLocation(x,y);
293
}
294
}
295
public void keyTyped(KeyEvent e)
{}
296
public void keyReleased(KeyEvent e)
{}
297
}
298
299
//例子8
300
import java.awt.*;import java.awt.event.*;
301
//创建棋盘的类:
302
class ChessPad extends Panel implements MouseListener,ActionListener
303

{ int x=-1,y=-1, 棋子颜色=1; //控制棋子颜色的成员变量。
304
Button button=new Button("重新开局"); //控制重新开局的按扭。
305
TextField text_1=new TextField("请黑棋下子"),
306
text_2=new TextField(); //提示下棋的两个文本框。
307
ChessPad()
308
{ setSize(440,440);
309
setLayout(null);setBackground(Color.pink);
310
addMouseListener(this);add(button);button.setBounds(10,5,60,26);
311
button.addActionListener(this);
312
add(text_1);text_1.setBounds(90,5,90,24);
313
add(text_2);text_2.setBounds(290,5,90,24);
314
text_1.setEditable(false);text_2.setEditable(false);
315
}
316
public void paint(Graphics g) //绘制围棋棋盘的外观。
317
{ for(int i=40;i<=380;i=i+20)
318
{ g.drawLine(40,i,400,i);
319
}
320
g.drawLine(40,400,400,400);
321
for(int j=40;j<=380;j=j+20)
322
{ g.drawLine(j,40,j,400);
323
}
324
g.drawLine(400,40,400,400);
325
g.fillOval(97,97,6,6); g.fillOval(337,97,6,6);
326
g.fillOval(97,337,6,6);g.fillOval(337,337,6,6);
327
g.fillOval(217,217,6,6);
328
}
329
public void mousePressed(MouseEvent e) //当按下鼠标左键时下棋子。
330
{ if(e.getModifiers()==InputEvent.BUTTON1_MASK)
331
{ x=(int)e.getX();y=(int)e.getY(); //获取按下鼠标时的坐标位置。
332
ChessPoint_black chesspoint_black=new ChessPoint_black(this);
333
ChessPoint_white chesspoint_white=new ChessPoint_white(this);
334
int a=(x+10)/20,b=(y+10)/20;
335
if(x/20<2||y/20<2||x/20>19||y/20>19) //棋盘以外不下棋子。
336
{}
337
else
338
{
339
if(棋子颜色==1) //当棋子颜色是1时下黑棋子。
340
{ this.add(chesspoint_black);
341
chesspoint_black.setBounds(a*20-7,b*20-7,16,16);
342
棋子颜色=棋子颜色*(-1);
343
text_2.setText("请白棋下子");
344
text_1.setText("");
345
}
346
else if(棋子颜色==-1) //当棋子颜色是-1时下白棋子。
347
{ this.add(chesspoint_white);
348
chesspoint_white.setBounds(a*20-7,b*20-7,16,16);
349
棋子颜色=棋子颜色*(-1);
350
text_1.setText("请黑棋下子");
351
text_2.setText("");
352
}
353
}
354
}
355
}
356
public void mouseReleased(MouseEvent e)
{}
357
public void mouseEntered(MouseEvent e)
{}
358
public void mouseExited(MouseEvent e)
{}
359
public void mouseClicked(MouseEvent e)
{}
360
public void actionPerformed(ActionEvent e)
361
{ this.removeAll();棋子颜色=1;
362
add(button);button.setBounds(10,5,60,26);
363
add(text_1);text_1.setBounds(90,5,90,24);
364
text_2.setText("");text_1.setText("请黑棋下子");
365
add(text_2);text_2.setBounds(290,5,90,24);
366
}
367
}
368
//负责创建黑色棋子的类:
369
class ChessPoint_black extends Canvas implements MouseListener
370

{ ChessPad chesspad=null; //棋子所在的棋盘。
371
ChessPoint_black(ChessPad p)
372
{ setSize(20,20);chesspad=p; addMouseListener(this);
373
}
374
public void paint(Graphics g) //绘制棋子的大小。
375
{ g.setColor(Color.black);g.fillOval(0,0,14,14);
376
}
377
public void mousePressed(MouseEvent e)
378
{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)
379
{ chesspad.remove(this);//当用鼠标右键点击棋子时,从棋盘中去掉该棋子(悔棋)。
380
chesspad.棋子颜色=1;
381
chesspad.text_2.setText("");chesspad.text_1.setText("请黑棋下子");
382
}
383
}
384
public void mouseReleased(MouseEvent e)
{}
385
public void mouseEntered(MouseEvent e)
{}
386
public void mouseExited(MouseEvent e)
{}
387
public void mouseClicked(MouseEvent e)
388
{ if(e.getClickCount()>=2)
389
chesspad.remove(this); //当用左键双击该棋子时,吃掉该棋子。
390
}
391
}
392
//负责创建白色棋子的类:
393
class ChessPoint_white extends Canvas implements MouseListener
394

{ ChessPad chesspad=null;
395
ChessPoint_white(ChessPad p)
396
{ setSize(20,20);addMouseListener(this);
397
chesspad=p;
398
}
399
public void paint(Graphics g)
400
{ g.setColor(Color.white);g.fillOval(0,0,14,14);
401
}
402
public void mousePressed(MouseEvent e)
403
{ if(e.getModifiers()==InputEvent.BUTTON3_MASK)
404
{ chesspad.remove(this);chesspad.棋子颜色=-1;
405
chesspad.text_2.setText("请白棋下子"); chesspad.text_1.setText("");
406
}
407
}
408
public void mouseReleased(MouseEvent e)
{}
409
public void mouseEntered(MouseEvent e)
{}
410
public void mouseExited(MouseEvent e)
{}
411
public void mouseClicked(MouseEvent e)
412
{ if(e.getClickCount()>=2)
413
chesspad.remove(this);
414
}
415
}
416
public class Chess extends Frame //添加棋盘的窗口。
417

{ ChessPad chesspad=new ChessPad();
418
Chess()
419
{ setVisible(true);
420
setLayout(null);
421
Label label=
422
new Label("单击左键下棋子,双击吃棋子,用右键单击棋子悔棋",Label.CENTER);
423
add(label);label.setBounds(70,55,440,26);
424
label.setBackground(Color.orange);
425
add(chesspad);chesspad.setBounds(70,90,440,440);
426
addWindowListener(new WindowAdapter()
427
{public void windowClosing(WindowEvent e)
428
{System.exit(0);
429
}
430
});
431
pack();setSize(600,550);
432
}
433
public static void main(String args[])
434
{ Chess chess=new Chess();
435
}
436
}
437
438
//例子9
439
import java.awt.event.*;import java.applet.*;
440
import java.awt.*;
441
public class Move extends.Applet implements KeyListener,ActionListener
442

{ Button b_go=new Button("go"),
443
b_replay=new Button("replay");
444
Rectangle rect1,rect2,rect3;
445
int b_x=0,b_y=0;
446
public void init()
447
{ b_go.addKeyListener(this);
448
b_replay.addActionListener(this);
449
setLayout(null);
450
//代表迷宫的矩形:
451
rect1=new Rectangle(20,40,200,40);
452
rect2=new Rectangle(200,40,24,240);
453
rect3=new Rectangle(200,220,100,40);
454
b_go.setBackground(Color.red); //代表走迷宫者的按扭。
455
add(b_go);b_go.setBounds(22,45,20,20);
456
b_x=b_go.getBounds().x;b_y=b_go.getBounds().y;
457
b_go.requestFocus() ;
458
add(b_replay);b_replay.setBounds(2,2,45,16);//点击重新开始的按扭。
459
}
460
public void paint(Graphics g)
461
{ //画出迷宫:
462
g.setColor(Color.green);
463
g.fillRect(20,40,200,40);
464
g.setColor(Color.yellow);
465
g.fillRect(200,40,40,240);
466
g.setColor(Color.cyan);
467
g.fillRect(200,220,100,40);
468
g.drawString("出口",310,220);
469
g.setColor(Color.black);
470
g.drawString("点击红色按扭,然后用键盘上的方向键移动按扭",100,20);
471
}
472
public void keyPressed(KeyEvent e)
473
{ //按键盘上的上下、左右键在迷宫中行走:
474
if((e.getKeyCode()==KeyEvent.VK_UP))
475
{ //创建一个和按扭:b_go 同样大小的矩形:
476
Rectangle rect=new Rectangle(b_x,b_y,20,20);
477
//要求必须在迷宫内行走:
478
if(rect.intersects(rect1)||rect.intersects(rect2)||
479
rect.intersects(rect3))
480
{ b_y=b_y-2;b_go.setLocation(b_x,b_y);
481
}
482
}
483
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
484
{ Rectangle rect=new Rectangle(b_x,b_y,20,20);
485
if(rect.intersects(rect1)||rect.intersects(rect2)||
486
rect.intersects(rect3))
487
{ b_y=b_y+2;b_go.setLocation(b_x,b_y);
488
}
489
}
490
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
491
{ Rectangle rect=new Rectangle(b_x,b_y,20,20);
492
if(rect.intersects(rect1)||rect.intersects(rect2)
493
||rect.intersects(rect3))
494
{ b_x=b_x-2;b_go.setLocation(b_x,b_y);
495
}
496
}
497
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
498
{ Rectangle rect=new Rectangle(b_x,b_y,20,20);
499
if(rect.intersects(rect1)||rect.intersects(rect2)||
500
rect.intersects(rect3))
501
{ b_x=b_x+2;b_go.setLocation(b_x,b_y);
502
}
503
}
504
}
505
public void keyReleased(KeyEvent e)
{}
506
public void keyTyped(KeyEvent e)
{}
507
public void actionPerformed(ActionEvent e)
508
{ b_go.setBounds(22,45,20,20);
509
b_x=b_go.getBounds().x;b_y=b_go.getBounds().y;
510
b_go.requestFocus() ;
511
}
512
}
513
514
//例子10
515
import java.awt.*;import java.applet.*;import java.awt.event.*;
516
class People extends Button implements FocusListener //代表华容道人物的类。
517

{ Rectangle rect=null;
518
int left_x,left_y;//按扭的左上角坐标.
519
int width,height; //按扭的宽和高.
520
String name; int number;
521
People(int number,String s,int x,int y,int w,int h,Hua_Rong_Road road)
522
{ super(s);
523
name=s;this.number=number;
524
left_x=x;left_y=y;
525
width=w;height=h;setBackground(Color.orange);
526
road.add(this); addKeyListener(road);
527
setBounds(x,y,w,h);addFocusListener(this);
528
rect=new Rectangle(x,y,w,h);
529
}
530
public void focusGained(FocusEvent e)
531
{ setBackground(Color.red);
532
}
533
public void focusLost(FocusEvent e)
534
{ setBackground(Color.orange);
535
}
536
}
537
public class Hua_Rong_Road extends Applet implements KeyListener,ActionListener
538

{ People people[]=new People[10];
539
Rectangle left,right,above ,below;//华容道的边界 .
540
Button restart=new Button("重新开始");
541
public void init()
542
{ setLayout(null); add(restart);
543
restart.setBounds(5,5,80,25);
544
restart.addActionListener(this);
545
people[0]=new People(0,"曹操",104,54,100,100,this);
546
people[1]=new People(1,"关羽",104,154,100,50,this);
547
people[2]=new People(2,"张飞",54, 154,50,100,this);
548
people[3]=new People(3,"刘备",204,154,50,100,this);
549
people[4]=new People(4,"张辽",54, 54, 50,100,this);
550
people[5]=new People(5,"曹仁",204, 54, 50,100,this);
551
people[6]=new People(6,"兵 ",54,254,50,50,this);
552
people[7]=new People(7,"兵 ",204,254,50,50,this);
553
people[8]=new People(8,"兵 ",104,204,50,50,this);
554
people[9]=new People(9,"兵 ",154,204,50,50,this);
555
people[9].requestFocus();
556
left=new Rectangle(49,49,5,260);
557
people[0].setForeground(Color.white) ;
558
right=new Rectangle(254,49,5,260);
559
above=new Rectangle(49,49,210,5);
560
below=new Rectangle(49,304,210,5);
561
}
562
public void paint(Graphics g)
563
{ //画出华容道的边界:
564
g.setColor(Color.cyan);
565
g.fillRect(49,49,5,260);//left.
566
g.fillRect(254,49,5,260);//right.
567
g.fillRect(49,49,210,5); //above.
568
g.fillRect(49,304,210,5);//below.
569
//提示曹操逃出位置和按键规则:
570
g.drawString("点击相应的人物,然后按键盘上的上下左右箭头移动",100,20);
571
g.setColor(Color.red);
572
g.drawString("曹操到达该位置",110,300);
573
}
574
public void keyPressed(KeyEvent e)
575
{ People man=(People)e.getSource();//获取事件源.
576
man.rect.setLocation(man.getBounds().x, man.getBounds().y);
577
if(e.getKeyCode()==KeyEvent.VK_DOWN)
578
{ man.left_y=man.left_y+50; //向下前进50个单位。
579
man.setLocation(man.left_x,man.left_y);
580
man.rect.setLocation(man.left_x,man.left_y);
581
//判断是否和其它人物或下边界出现重叠,如果出现重叠就退回50个单位距离。
582
for(int i=0;i<10;i++)
583
{if((man.rect.intersects(people[i].rect))&&(man.number!=i))
584
{ man.left_y=man.left_y-50;
585
man.setLocation(man.left_x,man.left_y);
586
man.rect.setLocation(man.left_x,man.left_y);
587
}
588
}
589
if(man.rect.intersects(below))
590
{ man.left_y=man.left_y-50;
591
man.setLocation(man.left_x,man.left_y);
592
man.rect.setLocation(man.left_x,man.left_y);
593
}
594
}
595
if(e.getKeyCode()==KeyEvent.VK_UP)
596
{ man.left_y=man.left_y-50; //向上前进50个单位。
597
man.setLocation(man.left_x,man.left_y);
598
man.rect.setLocation(man.left_x,man.left_y);
599
//判断是否和其它人物或上边界出现重叠,如果出现重叠就退回50个单位距离。
600
for(int i=0;i<10;i++)
601
{ if((man.rect.intersects(people[i].rect))&&(man.number!=i))
602
{ man.left_y=man.left_y+50;
603
man.setLocation(man.left_x,man.left_y);
604
man.rect.setLocation(man.left_x,man.left_y);
605
}
606
}
607
if(man.rect.intersects(above))
608
{ man.left_y=man.left_y+50;
609
man.setLocation(man.left_x,man.left_y);
610
man.rect.setLocation(man.left_x,man.left_y);
611
}
612
}
613
if(e.getKeyCode()==KeyEvent.VK_LEFT)
614
{ man.left_x=man.left_x-50; //向左前进50个单位。
615
man.setLocation(man.left_x,man.left_y);
616
man.rect.setLocation(man.left_x,man.left_y);
617
//判断是否和其它人物或左边界出现重叠,如果出现重叠就退回50个单位距离。
618
for(int i=0;i<10;i++)
619
{ if((man.rect.intersects(people[i].rect))&&(man.number!=i))
620
{ man.left_x=man.left_x+50;
621
man.setLocation(man.left_x,man.left_y);
622
man.rect.setLocation(man.left_x,man.left_y);
623
}
624
}
625
if(man.rect.intersects(left))
626
{ man.left_x=man.left_x+50;
627
man.setLocation(man.left_x,man.left_y);
628
man.rect.setLocation(man.left_x,man.left_y);
629
}
630
}
631
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
632
{ man.left_x=man.left_x+50; //向右前进50个单位。
633
man.setLocation(man.left_x,man.left_y);
634
man.rect.setLocation(man.left_x,man.left_y);
635
//判断是否和其它人物或右边界出现重叠,如果出现重叠就退回50个单位距离。
636
for(int i=0;i<10;i++)
637
{ if((man.rect.intersects(people[i].rect))&&(man.number!=i))
638
{ man.left_x=man.left_x-50;
639
man.setLocation(man.left_x,man.left_y);
640
man.rect.setLocation(man.left_x,man.left_y);
641
}
642
}
643
if(man.rect.intersects(right))
644
{ man.left_x=man.left_x-50;
645
man.setLocation(man.left_x,man.left_y);
646
man.rect.setLocation(man.left_x,man.left_y);
647
}
648
}
649
}
650
public void keyTyped(KeyEvent e)
{}
651
public void keyReleased(KeyEvent e)
{}
652
public void actionPerformed(ActionEvent e)
653
{ this.removeAll();
654
this.init();
655
}
656
}
657

2

3

4

5



6

7



8

9

10

11



12

13

14



15

16

17



18

19

20



21

22

23



24



25

26



27

28

29

30

31

32

33



34

35

36



37

38

39

40

41



42



43

44

45



46

47

48

49



50

51



52

53

54

55



56

57

58

59



60



61

62



63

64

65



66

67



68



69

70

71



72

73

74

75

76



77



78

79

80



81



82

83

84

85

86

87

88

89

90

91

92

93



94

95

96



97

98

99

100

101

102

103



104



105

106

107



108

109

110



111

112

113



114



115



116

117



118

119

120

121

122

123

124

125



126

127



128

129

130

131



132



133

134

135

136

137



138

139

140



141

142



143

144

145

146

147

148

149

150

151



152

153

154

155

156



157

158

159

160

161

162

163

164

165

166

167

168



169



170

171

172



173

174

175



176

177

178

179



180

181



182

183



184

185

186



187



188

189

190



191

192

193



194

195

196



197

198

199



200

201

202

203

204

205

206

207

208



209

210

211

212



213

214

215

216

217

218

219



220



221

222

223

224

225

226



227

228



229

230



231



232



233



234

235



236

237



238

239

240

241

242

243

244



245



246

247

248

249

250

251

252



253

254

255

256

257

258

259

260

261



262

263

264



265



266

267

268

269

270

271



272

273

274

275



276

277

278

279

280



281

282

283

284

285



286

287

288

289

290



291

292

293

294

295



296



297

298

299

300

301

302

303



304

305

306

307

308



309

310

311

312

313

314

315

316

317



318



319

320

321

322



323

324

325

326

327

328

329

330



331



332

333

334

335

336



337

338



339

340



341

342

343

344

345

346

347



348

349

350

351

352

353

354

355

356



357



358



359



360

361



362

363

364

365

366

367

368

369

370



371

372



373

374

375



376

377

378



379



380

381

382

383

384



385



386



387

388



389

390

391

392

393

394



395

396



397

398

399

400



401

402

403



404



405

406

407

408



409



410



411

412



413

414

415

416

417



418

419



420

421

422

423

424

425

426

427



428



429

430

431

432

433

434



435

436

437

438

439

440

441

442



443

444

445

446

447



448

449

450

451

452

453

454

455

456

457

458

459

460

461



462

463

464

465

466

467

468

469

470

471

472

473



474

475



476

477

478

479

480



481

482

483

484



485

486

487



488

489

490

491



492

493

494



495

496

497

498



499

500

501



502

503

504

505



506



507

508



509

510

511

512

513

514

515

516

517



518

519

520

521

522



523

524

525

526

527

528

529

530

531



532

533

534



535

536

537

538



539

540

541

542



543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563



564

565

566

567

568

569

570

571

572

573

574

575



576

577

578



579

580

581

582

583



584



585

586

587

588

589

590



591

592

593

594

595

596



597

598

599

600

601



602



603

604

605

606

607

608



609

610

611

612

613

614



615

616

617

618

619



620



621

622

623

624

625

626



627

628

629

630

631

632



633

634

635

636

637



638



639

640

641

642

643

644



645

646

647

648

649

650



651



652

653



654

655

656

657
