H:\CSC120\Java\lec30\src\MUPanel.java |
1
2
3
4
5
6
7
8 import java.awt.*;
9 import javax.swing.*;
10
11 public class MUPanel extends JPanel {
12
13 private Mouse mickey, minnie, ratso, mousey;
14
15 private Mouse[] vermin;
16
17
18
19
20
21
22
23
24 public MUPanel() {
25 setLayout(null);
26 setPreferredSize(new Dimension(760, 640));
27 setName("Mount Union Java Program");
28 setUp();
29 setBackground(new Color(0xe8, 0xe8, 0xcc));
30
31
32
33 vermin = new Mouse[50];
34
35
36 for (Integer index = 0; index < 10; index++) {
37 vermin[index] = new Mouse(70, 25*index+10, Color.RED);
38 }
39 for (Integer index = 10; index < 20; index++) {
40 vermin[index] = new Mouse(70, 25*index+10);
41 }
42 for (Integer index = 20; index < 24; index++) {
43 vermin[index] = new Mouse(70, 25*index+10, Color.PINK);
44 }
45 for (Integer index = 24; index < vermin.length; index++) {
46 vermin[index] = new Mouse();
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 @Override
76 public void paintComponent(Graphics g) {
77 super.paintComponent(g);
78
79
80
81 for (Integer index = 0; index < vermin.length; index++) {
82 vermin[index].draw(g);
83 }
84
85
86
87
88 }
89
90
91 public void moveMiceForward() {
92
93 for (Integer index = 0; index < vermin.length; index++) {
94 vermin[index].moveForward(2);
95 }
96
97
98
99 }
100
101 public void turnMiceAround() {
102
103 for (Integer index = 0; index < vermin.length; index += 2) {
104 vermin[index].turnAround();
105 }
106
107
108 }
109
110
111
112
113
114
115
116 public void setUp() {
117 for (Component c: getComponents())
118 c.setSize(c.getPreferredSize());
119 JFrame f = new JFrame(getName());
120 f.setContentPane(this);
121 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
122 f.pack();
123 f.setVisible(false);
124 }
125
126 public static void main(String args[]){new MUPanel();}
127
128 }
129