001    package aima.basic.vaccum;
002    
003    import java.awt.GridLayout;
004    
005    import javax.swing.ImageIcon;
006    import javax.swing.JButton;
007    import javax.swing.JPanel;
008    
009    /**
010     * @author Ravi Mohan
011     * 
012     */
013    public class TveDemo extends javax.swing.JFrame {
014            private javax.swing.JMenuItem aboutMenuItem;
015    
016            private javax.swing.JMenuItem openMenuItem;
017    
018            private javax.swing.JMenu fileMenu;
019    
020            private javax.swing.JMenuBar menuBar;
021    
022            private javax.swing.JPanel cleanerPanel;
023    
024            private javax.swing.JMenuItem exitMenuItem;
025    
026            private javax.swing.JMenu helpMenu;
027    
028            private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_exitMenuItemActionPerformed
029                    System.exit(0);
030            }
031    
032            private void exitForm(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_exitForm
033                    System.exit(0);
034            }
035    
036            private void initComponents() {
037                    cleanerPanel = new CleanerPanel();
038                    menuBar = new javax.swing.JMenuBar();
039                    fileMenu = new javax.swing.JMenu();
040                    exitMenuItem = new javax.swing.JMenuItem();
041                    helpMenu = new javax.swing.JMenu();
042                    aboutMenuItem = new javax.swing.JMenuItem();
043    
044                    addWindowListener(new java.awt.event.WindowAdapter() {
045                            @Override
046                            public void windowClosing(java.awt.event.WindowEvent evt) {
047                                    exitForm(evt);
048                            }
049                    });
050    
051                    getContentPane().add(cleanerPanel, java.awt.BorderLayout.CENTER);
052    
053                    fileMenu.setText("File");
054                    exitMenuItem.setText("Exit");
055                    exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
056                            public void actionPerformed(java.awt.event.ActionEvent evt) {
057                                    exitMenuItemActionPerformed(evt);
058                            }
059                    });
060    
061                    fileMenu.add(exitMenuItem);
062                    menuBar.add(fileMenu);
063                    helpMenu.setText("Help");
064                    aboutMenuItem.setText("About");
065                    helpMenu.add(aboutMenuItem);
066                    menuBar.add(helpMenu);
067                    setJMenuBar(menuBar);
068    
069                    pack();
070            }
071    
072            public static void main(String args[]) {
073                    new TveDemo().show();
074            }
075    
076            public class CleanerPanel extends JPanel {
077    
078                    JButton pieces[] = new JButton[2];
079    
080                    private final ImageIcon STATUS_A = new ImageIcon(TveDemo.class
081                                    .getResource("clean.gif"));
082    
083                    private final ImageIcon STATUS_B = new ImageIcon(TveDemo.class
084                                    .getResource("dirty.gif"));
085    
086                    private final ImageIcon STATUS_C = new ImageIcon(TveDemo.class
087                                    .getResource("cleaner_clean.gif"));
088    
089                    private final ImageIcon STATUS_D = new ImageIcon(TveDemo.class
090                                    .getResource("cleaner_dirty.gif"));
091    
092                    public CleanerPanel() {
093                            setLayout(new GridLayout(1, 2));
094                            init();
095                    }
096    
097                    private void init() {
098                            for (int i = 0; i < pieces.length; i++) {
099                                    pieces[i] = new JButton(STATUS_D);
100                                    this.add(pieces[i]);
101                            }
102                    }
103    
104                    public void updateCells(int[] status) {
105                            for (int i = 0; i < pieces.length; i++) {
106                                    switch (status[i]) {
107                                    case 0:
108                                            pieces[i].setIcon(STATUS_A);
109                                            break;
110                                    case 1:
111                                            pieces[i].setIcon(STATUS_B);
112                                            break;
113                                    case 2:
114                                            pieces[i].setIcon(STATUS_C);
115                                            break;
116                                    case 3:
117                                            pieces[i].setIcon(STATUS_D);
118                                            break;
119                                    }
120                            }
121                    }
122    
123            }
124    
125    }