Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
157 kaklik 1
/*
2
    AVRcamVIEW: A PC application to test out the functionallity of the
3
     AVRcam real-time image processing engine.
4
    Copyright (C) 2004    Brent A. Taylor
5
 
6
    This program is free software; you can redistribute it and/or
7
    modify it under the terms of the GNU General Public
8
    License as published by the Free Software Foundation; either
9
    version 2 of the License, or (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
    General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public
17
    License along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
   For more information on the AVRcamVIEW, please contact:
21
 
22
   taylorba@comcast.net
23
 
24
   or go to www.jrobot.net for more details regarding the system.
25
*/
26
 
27
package avr.swing;
28
 
29
import java.awt.*;
30
import java.awt.event.*;
31
import java.io.*;
32
import java.util.*;
33
import javax.swing.*;
34
import javax.swing.border.*;
35
import javax.swing.event.*;
36
 
37
import avr.connection.event.*;
38
import avr.device.event.*;
39
import avr.swing.*;
40
 
41
import avr.lang.*;
42
 
43
public class JColorMapPanel extends JColorMapInterface {
44
 
45
   public static void main(String[] args) {
46
      JFrame frame = new JFrame();
47
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48
      frame.add(new JColorMapPanel(null));
49
      frame.pack();
50
      frame.setVisible(true);
51
   }
52
 
53
   private JLabel[] systemColorsL;
54
 
55
   private JMessagePanel messageP;
56
   private JSelectionPanel[] redColorPanels;
57
   private JSelectionPanel[] greenColorPanels;
58
   private JSelectionPanel[] blueColorPanels;
59
 
60
   private Action checkAction;
61
   private Action clearColumnAction;
62
   private Action clearAction;
63
   private Action resetAction;
64
   private Action sendAction;
65
 
66
   public JColorMapPanel(JMessagePanel messageP) {
67
      super(new BorderLayout());
68
 
69
      this.messageP = messageP;
70
 
71
      checkAction = new ProxyAction(this, "check", "Auto Check", 'a');
72
      clearColumnAction = new ProxyAction(this, "clearColumn", "Clear Column(s)", 'l');
73
      clearAction = new ProxyAction(this, "clear", "Clear All", 'c');
74
      resetAction = new ProxyAction(this, "reset", "Reset", 'r');
75
      sendAction = new ProxyAction(this, "send", "Send", 's');
76
 
77
      sendAction.setEnabled(false);
78
 
79
      redColorPanels = new JSelectionPanel[AVRSystem.NUM_INTENSITIES];
80
      greenColorPanels = new JSelectionPanel[AVRSystem.NUM_INTENSITIES];
81
      blueColorPanels = new JSelectionPanel[AVRSystem.NUM_INTENSITIES];
82
 
83
      JPanel titleP = new JPanel();
84
 
85
      JLabel titleL = new JLabel("System Colors: ");
86
      titleL.setFont(titleL.getFont().deriveFont(16.0f));
87
 
88
      titleP.add(titleL);
89
 
90
      systemColorsL = new JLabel[8];
91
      for(int i = 0; i < systemColorsL.length; i++) {
92
         systemColorsL[i] = new JLabel();
93
         systemColorsL[i].setBorder(new LineBorder(Color.BLACK));
94
         systemColorsL[i].setOpaque(true);
95
         systemColorsL[i].setPreferredSize(new Dimension(20, 20));
96
 
97
         titleP.add(systemColorsL[i]);
98
      }
99
 
100
      // set the number of rows one less than the number of intensities because we
101
      // do not use the first row.
102
      JPanel centerP = new JPanel(new GridLayout(AVRSystem.NUM_INTENSITIES - 1, 1, 0, 5));
103
      centerP.setBorder(new EmptyBorder(0, 5, 5, 5));
104
 
105
      Box row;
106
 
107
      Dimension rowLabelSize = null;
108
      int intensityIncrement = (int)(256 / AVRSystem.NUM_INTENSITIES);
109
 
110
      for(int i = 0; i < AVRSystem.NUM_INTENSITIES; i++) {
111
         redColorPanels[i] = new JSelectionPanel(Color.RED);
112
         greenColorPanels[i] = new JSelectionPanel(Color.GREEN);
113
         blueColorPanels[i] = new JSelectionPanel(Color.BLUE);
114
 
115
         row = new Box(BoxLayout.X_AXIS);
116
 
117
         JLabel rowL = new JLabel("" + (i * intensityIncrement), JLabel.CENTER);
118
 
119
         // set all the labels to the same size
120
         if(rowLabelSize == null) {
121
            rowLabelSize = new Dimension(rowL.getPreferredSize().width * 4,
122
                                         rowL.getPreferredSize().height);
123
         }
124
 
125
         rowL.setPreferredSize(rowLabelSize);
126
         rowL.setMinimumSize(rowLabelSize);
127
 
128
         row.add(rowL);
129
         row.add(Box.createHorizontalStrut(5));
130
         row.add(redColorPanels[i]);
131
         row.add(Box.createHorizontalStrut(5));
132
         row.add(greenColorPanels[i]);
133
         row.add(Box.createHorizontalStrut(5));
134
         row.add(blueColorPanels[i]);
135
 
136
         // we do not use the first row
137
         if(i != 0) {
138
            centerP.add(row);
139
         }
140
      }
141
 
142
      Box southBox = new Box(BoxLayout.X_AXIS);
143
      southBox.setBorder(new CompoundBorder(new EtchedBorder(), new EmptyBorder(5, 5, 5, 5)));
144
 
145
      southBox.add(new JButton(checkAction));
146
      southBox.add(Box.createHorizontalGlue());
147
      southBox.add(new JButton(clearColumnAction));
148
      southBox.add(Box.createHorizontalStrut(5));
149
      southBox.add(new JButton(clearAction));
150
      southBox.add(Box.createHorizontalStrut(5));
151
      southBox.add(new JButton(resetAction));
152
      southBox.add(Box.createHorizontalStrut(5));
153
      southBox.add(new JButton(sendAction));
154
 
155
      add(titleP, BorderLayout.NORTH);
156
      add(centerP, BorderLayout.CENTER);
157
      add(southBox, BorderLayout.SOUTH);
158
 
159
      // load the current color map
160
      reset();
161
 
162
      AVRSystem.DEVICE.addConnectionListener(new ConnectionHandler());
163
   }
164
 
165
   private static String formatColorMapException(InvalidColorMapException icme) {
166
 
167
      StringBuffer builder = new StringBuffer("Invalid Color Map: ");
168
 
169
      int[] indicies = icme.getIndicies();
170
 
171
      builder.append("Indicies ");
172
 
173
      for(int i = 0; i < indicies.length; i++) {
174
 
175
         builder.append(indicies[i]);
176
 
177
         if((i + 1) < indicies.length) {
178
            builder.append(" and ");
179
         }
180
 
181
      }
182
 
183
      builder.append(" intersect at values Red: ")
184
             .append(icme.getRed())
185
             .append(" Green: ")
186
             .append(icme.getGreen())
187
             .append(" Blue: ")
188
             .append(icme.getBlue());
189
 
190
      return builder.toString();
191
 
192
   }
193
 
194
   public void check() {
195
 
196
      try {
197
         checkMap();
198
 
199
         JOptionPane.showMessageDialog(getRootPane(),
200
                                       "Color Map is valid.",
201
                                       "Color Map Validated",
202
                                       JOptionPane.INFORMATION_MESSAGE);
203
      } catch(InvalidColorMapException icme) {
204
         JOptionPane.showMessageDialog(getRootPane(),
205
                                       formatColorMapException(icme),
206
                                       "Check Failed",
207
                                       JOptionPane.ERROR_MESSAGE);
208
      }
209
 
210
   }
211
 
212
   public boolean isColumnClear(int column) {
213
 
214
      int value = 0;
215
 
216
      /* *********************************************
217
       * NOTE: This one loop is only checking hte
218
       * length of the red color panels but is also
219
       * looping over the green and blue ones!!!!
220
       **/
221
      for(int r = 0; r < redColorPanels.length; r++) {
222
         value |= redColorPanels[r].getValue() |
223
                  greenColorPanels[r].getValue() |
224
                  blueColorPanels[r].getValue();
225
      }
226
 
227
      return (value & (0x01 << (7 - column))) == 0;
228
   }
229
 
230
   /* *************************************
231
    * Copied from java.lang.Integer class
232
    * from the JDK 1.5 version.
233
    */
234
   private int bitCount(int i) {
235
      i = i - ((i >>> 1) & 0x55555555);
236
      i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
237
      i = (i + (i >>> 4)) & 0x0f0f0f0f;
238
      i = i + (i >>> 8);
239
      i = i + (i >>> 16);
240
      return i & 0x3f;
241
   }
242
 
243
   private void checkMap() throws InvalidColorMapException {
244
      for(int r = 0; r < redColorPanels.length; r++) {
245
         int red = redColorPanels[r].getValue();
246
         for(int g = 0; g < greenColorPanels.length; g++) {
247
            int green = greenColorPanels[g].getValue();
248
            for(int b = 0; b < blueColorPanels.length; b++) {
249
               int blue = blueColorPanels[b].getValue();
250
               int value = red & green & blue;
251
 
252
               // In JDk 1.5 the Integer class has the bitCount
253
               // method.  To be backward compatible, use the bitCount
254
               // method above.
255
//               if(value != 0 && (Integer.bitCount(value) > 1)) {
256
               if(value != 0 && (bitCount(value) > 1)) {
257
                  int[] indicies = new int[bitCount(value)];
258
                  int count = 0;
259
                  for(int i = 0; i < 8; i++) {
260
                     if((value & (0x80 >>> i)) != 0) {
261
                        indicies[count++] = (i + 1);
262
                     }
263
                  }
264
 
265
                  throw new InvalidColorMapException("Color Map is invalid.", indicies, r * 16, g * 16, b * 16);
266
               }
267
            }
268
         }
269
      }
270
 
271
   }
272
 
273
   public void clearColumn() {
274
 
275
      JPanel displayP = new JPanel(new BorderLayout());
276
 
277
      JPanel selectColP = new JPanel();
278
 
279
      JCheckBox[] colCB = new JCheckBox[8];
280
      for(int i = 0; i < colCB.length; i++) {
281
         colCB[i] = new JCheckBox((i + 1) + "");
282
         selectColP.add(colCB[i]);
283
      }
284
 
285
      displayP.add(new JLabel("Select Color Map Column:"), BorderLayout.NORTH);
286
      displayP.add(selectColP, BorderLayout.SOUTH);
287
 
288
      int option = JOptionPane.showConfirmDialog(getRootPane(),
289
                                                 displayP,
290
                                                 "Select Column(s) to clear:",
291
                                                 JOptionPane.OK_CANCEL_OPTION,
292
                                                 JOptionPane.QUESTION_MESSAGE);
293
 
294
      if(option == JOptionPane.OK_OPTION) {
295
 
296
         /* *********************************************
297
          * NOTE: This one loop is only checking hte
298
          * length of the red color panels but is also
299
          * looping over the green and blue ones!!!!
300
          **/
301
         for(int col = 0; col < 8; col++) {
302
            if(colCB[col].isSelected()) {
303
               for(int i = 0; i < redColorPanels.length; i++) {
304
                  redColorPanels[i].set(col, false);
305
                  greenColorPanels[i].set(col, false);
306
                  blueColorPanels[i].set(col, false);
307
               }
308
            }
309
         }
310
      }
311
   }
312
 
313
   public void clear() {
314
      /* *********************************************
315
       * NOTE: This one loop is only checking the
316
       * length of the red color panels but is also
317
       * looping over the green and blue ones!!!!
318
       **/
319
      for(int col = 0; col < redColorPanels.length; col++) {
320
         redColorPanels[col].setValue(0);
321
         greenColorPanels[col].setValue(0);
322
         blueColorPanels[col].setValue(0);
323
      }
324
   }
325
 
326
   public void reset() {
327
      int[][] colorMap = AVRSystem.DEVICE.getColorMap();
328
 
329
      /* *********************************************
330
       * NOTE: This one loop is only checking the
331
       * length of the red color panels but is also
332
       * looping over the green and blue ones!!!!
333
       **/
334
      for(int col = 0; col < redColorPanels.length; col++) {
335
         redColorPanels[col].setValue(colorMap[0][col]);
336
         greenColorPanels[col].setValue(colorMap[1][col]);
337
         blueColorPanels[col].setValue(colorMap[2][col]);
338
      }
339
 
340
      for(int i = 0; i < systemColorsL.length; i++) {
341
         systemColorsL[i].setBackground(AVRSystem.DEVICE.getMapColors()[i]);
342
      }
343
   }
344
 
345
   public void send() {
346
 
347
      try {
348
 
349
         checkMap();
350
 
351
         int[][] newColorMap = new int[3][AVRSystem.NUM_INTENSITIES];
352
         for(int r = 0; r < redColorPanels.length; r++) {
353
            newColorMap[0][r] = redColorPanels[r].getValue();
354
         }
355
         for(int g = 0; g < greenColorPanels.length; g++) {
356
            newColorMap[1][g] = greenColorPanels[g].getValue();
357
         }
358
         for(int b = 0; b < blueColorPanels.length; b++) {
359
            newColorMap[2][b] = blueColorPanels[b].getValue();
360
         }
361
 
362
         DataListener handler = new DataHandler(newColorMap);
363
         try {
364
 
365
            AVRSystem.DEVICE.addDataListener(handler);
366
            getRootPane().getGlassPane().setVisible(true);
367
            SwingUtilities.getRootPane(messageP).getGlassPane().setVisible(true);
368
            AVRSystem.DEVICE.sendSetColorMap(newColorMap[0], newColorMap[1], newColorMap[2]);
369
            messageP.append("Sent Color Map");
370
         } catch(IOException ioe) {
371
            AVRSystem.DEVICE.removeDataListener(handler);
372
            getRootPane().getGlassPane().setVisible(false);
373
            SwingUtilities.getRootPane(messageP).getGlassPane().setVisible(false);
374
            ioe.printStackTrace();
375
            AVRSystem.LOG.severe(ioe.getMessage());
376
         }
377
      } catch(InvalidColorMapException icme) {
378
         JOptionPane.showMessageDialog(getRootPane(),
379
                                       formatColorMapException(icme),
380
                                       "Check Failed",
381
                                       JOptionPane.ERROR_MESSAGE);
382
      }
383
   }
384
 
385
   public void setColor(int index, int color) {
386
 
387
      int red   = (color >>> 20) & 0x0F;
388
      int green = (color >>> 12) & 0x0F;
389
      int blue  = (color >>>  4) & 0x0F;
390
 
391
      /* *********************************************
392
       * NOTE: This one loop is only checking the
393
       * length of the red color panels but is also
394
       * looping over the green and blue ones!!!!
395
       **/
396
      for(int i = 0; i < redColorPanels.length; i++) {
397
         redColorPanels[i].set(index, i == red);
398
         greenColorPanels[i].set(index, i == green);
399
         blueColorPanels[i].set(index, i == blue);
400
      }
401
 
402
   }
403
 
404
   private final static class JSelectionPanel extends JPanel {
405
 
406
      private JCheckBox[] boxes;
407
 
408
      public JSelectionPanel(Color rowColor) {
409
         super();
410
         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
411
         setBackground(rowColor);
412
 
413
         boxes = new JCheckBox[8];
414
 
415
         add(Box.createHorizontalGlue());
416
         for(int i = 0; i < boxes.length; i++) {
417
            boxes[i] = new JCheckBox();
418
            boxes[i].setBackground(rowColor);
419
            if((i != 0) && ((i % 4) == 0)) {
420
               add(Box.createHorizontalStrut(5));
421
            }
422
            add(boxes[i]);
423
         }
424
         add(Box.createHorizontalGlue());
425
 
426
      }
427
 
428
      public void set(int index, boolean selected) {
429
         boxes[index].setSelected(selected);
430
      }
431
 
432
      public void setValue(int value) {
433
         for(int i = 0; i < boxes.length; i++) {
434
            if((value & (1 << (7 - i))) > 0) {
435
               boxes[i].setSelected(true);
436
            } else {
437
               boxes[i].setSelected(false);
438
            }
439
         }
440
      }
441
 
442
      public int getValue() {
443
         int value = 0;
444
 
445
         for(int i = 0; i < boxes.length; i++) {
446
            if(boxes[i].isSelected()) {
447
               value |= 1 << (7 - i);
448
               value &= 0xFF;
449
            }
450
         }
451
 
452
         return value;
453
      }
454
 
455
   }
456
 
457
   private final class DataHandler extends DataAdapter {
458
 
459
      private int[][] colorMap;
460
 
461
      public DataHandler(int[][] colorMap) {
462
         this.colorMap = colorMap;
463
      }
464
 
465
      public void ack() {
466
         AVRSystem.DEVICE.setColorMap(colorMap);
467
         reset();
468
         SwingUtilities.getRootPane(messageP).getGlassPane().setVisible(false);
469
         getRootPane().getGlassPane().setVisible(false);
470
         AVRSystem.DEVICE.removeDataListener(this);
471
      }
472
 
473
      public void nck() {
474
         getRootPane().getGlassPane().setVisible(false);
475
         SwingUtilities.getRootPane(messageP).getGlassPane().setVisible(false);
476
         AVRSystem.DEVICE.removeDataListener(this);
477
         JOptionPane.showMessageDialog(getRootPane(), "Set Color Map NCK Received", "NCK Received", JOptionPane.ERROR_MESSAGE);
478
      }
479
 
480
      public void responseTimerExpired() {
481
         getRootPane().getGlassPane().setVisible(false);
482
         SwingUtilities.getRootPane(messageP).getGlassPane().setVisible(false);
483
         AVRSystem.DEVICE.removeDataListener(this);
484
         JOptionPane.showMessageDialog(messageP.getRootPane(), "Response Timer Expired", "Timer Expired", JOptionPane.ERROR_MESSAGE);
485
      }
486
 
487
   }
488
 
489
   private final class ConnectionHandler implements ConnectionListener {
490
      public void connected(ConnectionEvent ce) {
491
         sendAction.setEnabled(true);
492
      }
493
 
494
      public void disconnected(ConnectionEvent ce) {
495
         sendAction.setEnabled(false);
496
      }
497
 
498
   }
499
 
500
}