H:\CSC120\Java\lec16\src\MUPanel.java |
1
2
3
4
5
6
7
8 import java.awt.*;
9 import java.awt.event.*;
10 import javax.swing.*;
11 import javax.swing.event.*;
12
13 public class MUPanel extends JPanel implements ActionListener {
14
15 private TVSet livingRoomTV, garageTV, kitchenTV;
16 private JButton cupLr, cdnLr, vupLr, vdnLr, onLr, offLr;
17 private JButton cupG, cdnG, vupG, vdnG, onG, offG;
18 private JButton cupK, cdnK, vupK, vdnK, onK, offK;
19
20 public MUPanel() {
21 setLayout(null);
22 setPreferredSize(new Dimension(820, 600));
23 setName("TVSet Demo");
24 setBackground(new Color(0xFE, 0xDE, 0x95));
25
26 livingRoomTV = new TVSet(10, 10, true, 3, 50);
27 garageTV = new TVSet(330, 250, false, 19, 75);
28 kitchenTV = new TVSet(650, 10, true, 43, 20);
29
30 setUpUserInterface();
31
32 }
33
34
35 @Override
36 public void paintComponent(Graphics g) {
37 super.paintComponent(g);
38
39 livingRoomTV.draw(g);
40 garageTV.draw(g);
41 kitchenTV.draw(g);
42
43 }
44
45
46 public void setUpUserInterface() {
47 cupLr = new JButton("^");
48 cupLr.setBounds(65, 170, 50, 50);
49 cupLr.addActionListener(this);
50 add(cupLr);
51
52 cdnLr = new JButton("v");
53 cdnLr.setBounds(65, 270, 50, 50);
54 cdnLr.addActionListener(this);
55 add(cdnLr);
56
57 vupLr = new JButton(">");
58 vupLr.setBounds(90, 220, 50, 50);
59 vupLr.addActionListener(this);
60 add(vupLr);
61
62 vdnLr = new JButton("<");
63 vdnLr.setBounds(40, 220, 50, 50);
64 vdnLr.addActionListener(this);
65 add(vdnLr);
66
67 onLr = new JButton("1");
68 onLr.setBounds(10, 120, 50, 50);
69 onLr.setBackground(Color.GREEN);
70 onLr.addActionListener(this);
71 add(onLr);
72
73 offLr = new JButton("0");
74 offLr.setBounds(120, 120, 50, 50);
75 offLr.setBackground(Color.RED);
76 offLr.addActionListener(this);
77 add(offLr);
78
79 cupK = new JButton("^");
80 cupK.setBounds(705, 170, 50, 50);
81 cupK.addActionListener(this);
82 add(cupK);
83
84 cdnK = new JButton("v");
85 cdnK.setBounds(705, 270, 50, 50);
86 cdnK.addActionListener(this);
87 add(cdnK);
88
89 vupK = new JButton(">");
90 vupK.setBounds(730, 220, 50, 50);
91 vupK.addActionListener(this);
92 add(vupK);
93
94 vdnK = new JButton("<");
95 vdnK.setBounds(680, 220, 50, 50);
96 vdnK.addActionListener(this);
97 add(vdnK);
98
99 onK = new JButton("1");
100 onK.setBounds(650, 120, 50, 50);
101 onK.setBackground(Color.GREEN);
102 onK.addActionListener(this);
103 add(onK);
104
105 offK = new JButton("0");
106 offK.setBounds(760, 120, 50, 50);
107 offK.setBackground(Color.RED);
108 offK.addActionListener(this);
109 add(offK);
110
111 cupG = new JButton("^");
112 cupG.setBounds(385, 410, 50, 50);
113 cupG.addActionListener(this);
114 add(cupG);
115
116 cdnG = new JButton("v");
117 cdnG.setBounds(385, 510, 50, 50);
118 cdnG.addActionListener(this);
119 add(cdnG);
120
121 vupG = new JButton(">");
122 vupG.setBounds(410, 460, 50, 50);
123 vupG.addActionListener(this);
124 add(vupG);
125
126 vdnG = new JButton("<");
127 vdnG.setBounds(360, 460, 50, 50);
128 vdnG.addActionListener(this);
129 add(vdnG);
130
131 onG = new JButton("1");
132 onG.setBounds(330, 360, 50, 50);
133 onG.setBackground(Color.GREEN);
134 onG.addActionListener(this);
135 add(onG);
136
137 offG = new JButton("0");
138 offG.setBounds(440, 360, 50, 50);
139 offG.setBackground(Color.RED);
140 offG.addActionListener(this);
141 add(offG);
142
143 }
144
145 public void actionPerformed ( ActionEvent ae ) {
146 Object source = ae.getSource();
147
148 if (source.equals(cupLr)) {
149 livingRoomTV.channelUp();
150 }
151 if (source.equals(cdnLr)) {
152 livingRoomTV.channelDown();
153 }
154 if (source.equals(vupLr)) {
155 livingRoomTV.volumeUp();
156 }
157 if (source.equals(vdnLr)) {
158 livingRoomTV.volumeDown();
159 }
160 if (source.equals(onLr)) {
161 livingRoomTV.setTurnedOn(true);
162 }
163 if (source.equals(offLr)) {
164 livingRoomTV.setTurnedOn(false);
165 }
166
167 if (source.equals(cupG)) {
168 garageTV.channelUp();
169 }
170 if (source.equals(cdnG)) {
171 garageTV.channelDown();
172 }
173 if (source.equals(vupG)) {
174 garageTV.volumeUp();
175 }
176 if (source.equals(vdnG)) {
177 garageTV.volumeDown();
178 }
179 if (source.equals(onG)) {
180 garageTV.setTurnedOn(true);
181 }
182 if (source.equals(offG)) {
183 garageTV.setTurnedOn(false);
184 }
185
186 if (source.equals(cupK)) {
187 kitchenTV.channelUp();
188 }
189 if (source.equals(cdnK)) {
190 kitchenTV.channelDown();
191 }
192 if (source.equals(vupK)) {
193 kitchenTV.volumeUp();
194 }
195 if (source.equals(vdnK)) {
196 kitchenTV.volumeDown();
197 }
198 if (source.equals(onK)) {
199 kitchenTV.setTurnedOn(true);
200 }
201 if (source.equals(offK)) {
202 kitchenTV.setTurnedOn(false);
203 }
204
205 repaint();
206 }
207
208
209
210
211 public void frame() {
212
213
214 JFrame f = new JFrame(getName());
215 f.setContentPane(this);
216 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
217 f.pack();
218 f.setVisible(true);
219 }
220
221 public static void main(String args[]){new MUPanel().frame();}
222
223 }
224