Project

General

Profile

BlindHelp.java

in progress - Catarina Ricardo, 22/05/2019 01:55

Download (8.23 KB)

 
1
import java.awt.EventQueue;
2
import javax.swing.JFrame;
3
import javax.swing.JTextField;
4
import java.awt.BorderLayout;
5
import javax.swing.JLabel;
6
import java.awt.SystemColor;
7
import java.awt.Font;
8
import javax.swing.JProgressBar;
9
import java.awt.Color;
10
import javax.swing.JButton;
11
import javax.swing.JComboBox;
12
import javax.swing.JMenu;
13
import javax.swing.JPanel;
14
import javax.swing.JList;
15
import javax.swing.AbstractListModel;
16
import javax.swing.JTextPane;
17
import com.fazecast.jSerialComm.SerialPort;
18
import javax.swing.Box;
19
import java.awt.Canvas;
20
import java.awt.Graphics;
21
import java.awt.event.ActionListener;
22
import java.util.Scanner;
23
import java.awt.event.ActionEvent;
24
import gnu.io.*;
25

    
26
public class BlindHelp {
27

    
28
        private static JFrame BlindHelp;
29
        private static JTextField Below_Values;
30
        private static JTextField Above_Values;
31
        private static JTextField Ground_Values;
32
        static SerialPort chosenPort;
33
        private static JButton connectButton;
34
        private static JComboBox<String> portList;
35
        static int x = 0;
36
                
37
        //when a value is less than required the vibration motors are activated
38
        public void vibrationMotors(JTextField values) {
39
                int n = Integer.parseInt(values.getText());
40
                Canvas off = new Canvas();
41
                Canvas on = new Canvas();
42
                on.setBounds(372, 89, 23, 23);
43
                off.setBounds(372, 89, 23, 23);
44
                if (n <= 10) //setting maximum range to 1 meter
45
                        on.setBackground(Color.GREEN); 
46
                else 
47
                        off.setBackground(Color.RED);
48
                
49
                BlindHelp.getContentPane().add(on);
50
        }
51
        
52
        
53
        /**
54
         * Launch the application.
55
         */
56
        public static void main(String[] args) {
57
                
58
                SensorGraph.main(args);
59
                EventQueue.invokeLater(new Runnable() {
60
                        
61
                        @SuppressWarnings("static-access")
62
                        public void run() {
63
                                try {
64
                                        BlindHelp window = new BlindHelp();
65
                                        window.BlindHelp.setVisible(true);
66
                                                                                                                
67
                                } catch (Exception e) {
68
                                        e.printStackTrace();
69
                                }
70
                                
71
                        }
72
                });
73
        }
74

    
75
        /**
76
         * Create the application.
77
         */
78
        public BlindHelp() {
79
                initialize();
80
                
81
        }
82

    
83
        /**
84
         * Initialize the contents of the frame.
85
         */
86
        private void initialize() {
87
                
88
                // create and configure the window
89
                BlindHelp = new JFrame();
90
                BlindHelp.setBackground(SystemColor.activeCaption);
91
                BlindHelp.setTitle("Blind Help");
92
                BlindHelp.setResizable(false);
93
                BlindHelp.setBounds(100, 100, 450, 300);
94
                BlindHelp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
95
                BlindHelp.getContentPane().setLayout(null);
96
                
97
                // create a drop-down box and connect button, then place them at the top of the window
98
                JComboBox<String> portList = new JComboBox<String>();
99
                portList.setBounds(28, 21, 69, 20);
100
                BlindHelp.getContentPane().add(portList);
101
                
102
                JButton connectButton = new JButton("Connect");
103
                connectButton.setBounds(137, 21, 102, 20);
104
                BlindHelp.getContentPane().add(connectButton);
105
                                                
106
                // populate the drop-down box
107
                SerialPort[] portNames = SerialPort.getCommPorts();
108
                for(int i = 0; i < portNames.length; i++)
109
                        portList.addItem(portNames[i].getSystemPortName());
110
                
111
                JTextPane txtpnBelowTheWaist = new JTextPane();
112
                txtpnBelowTheWaist.setEditable(false);
113
                txtpnBelowTheWaist.setText("Below the waist");
114
                txtpnBelowTheWaist.setBounds(28, 120, 99, 20);
115
                BlindHelp.getContentPane().add(txtpnBelowTheWaist);
116
                
117
                JTextPane txtpnAboveTheWaist = new JTextPane();
118
                txtpnAboveTheWaist.setEditable(false);
119
                txtpnAboveTheWaist.setText("Above the waist");
120
                txtpnAboveTheWaist.setBounds(28, 153, 99, 20);
121
                BlindHelp.getContentPane().add(txtpnAboveTheWaist);
122
                
123
                JLabel lblObstaclesDetection = new JLabel("Obstacles Detection");
124
                lblObstaclesDetection.setFont(new Font("Tahoma", Font.BOLD, 11));
125
                lblObstaclesDetection.setBounds(28, 64, 118, 14);
126
                BlindHelp.getContentPane().add(lblObstaclesDetection);
127
                
128
                Box verticalBox = Box.createVerticalBox();
129
                verticalBox.setBounds(190, 183, 16, -10);
130
                BlindHelp.getContentPane().add(verticalBox);
131
                
132
                JTextPane txtpnStick = new JTextPane();
133
                txtpnStick.setText("Ground");
134
                txtpnStick.setEditable(false);
135
                txtpnStick.setBounds(299, 89, 52, 20);
136
                BlindHelp.getContentPane().add(txtpnStick);
137
                                                
138
                Below_Values = new JTextField();
139
                Below_Values.setBackground(SystemColor.text);
140
                Below_Values.setEditable(false);
141
                Below_Values.setColumns(10);
142
                Below_Values.setBounds(180, 120, 59, 20);
143
                BlindHelp.getContentPane().add(Below_Values);
144
                
145
                Above_Values = new JTextField();
146
                Above_Values.setEditable(false);
147
                Above_Values.setColumns(10);
148
                Above_Values.setBackground(Color.WHITE);
149
                Above_Values.setBounds(180, 153, 59, 20);
150
                BlindHelp.getContentPane().add(Above_Values);
151
                
152
                JLabel lblBuzzers = new JLabel("Buzzers");
153
                lblBuzzers.setFont(new Font("Tahoma", Font.BOLD, 11));
154
                lblBuzzers.setBounds(299, 64, 52, 14);
155
                BlindHelp.getContentPane().add(lblBuzzers);
156
                
157
                JTextPane txtpnUp = new JTextPane();
158
                txtpnUp.setEditable(false);
159
                txtpnUp.setText("Up");
160
                txtpnUp.setBounds(299, 120, 37, 20);
161
                BlindHelp.getContentPane().add(txtpnUp);
162
                
163
                JTextPane txtpnDown = new JTextPane();
164
                txtpnDown.setText("Down");
165
                txtpnDown.setEditable(false);
166
                txtpnDown.setBounds(299, 153, 37, 20);
167
                BlindHelp.getContentPane().add(txtpnDown);
168
                
169
                JTextPane txtpnGroundSensor = new JTextPane();
170
                txtpnGroundSensor.setText("Ground Sensor");
171
                txtpnGroundSensor.setEditable(false);
172
                txtpnGroundSensor.setBounds(28, 89, 99, 20);
173
                BlindHelp.getContentPane().add(txtpnGroundSensor);
174
                
175
                Ground_Values = new JTextField();
176
                Ground_Values.setEditable(false);
177
                Ground_Values.setColumns(10);
178
                Ground_Values.setBackground(Color.WHITE);
179
                Ground_Values.setBounds(180, 89, 59, 20);
180
                BlindHelp.getContentPane().add(Ground_Values);
181
                
182
                JLabel lblDistance = new JLabel("Distance (meters)");
183
                lblDistance.setFont(new Font("Tahoma", Font.BOLD, 11));
184
                lblDistance.setBounds(171, 64, 118, 14);
185
                BlindHelp.getContentPane().add(lblDistance);
186
                
187
                // show the window
188
                BlindHelp.setVisible(true);
189
                
190
                // configure the connect button and use another thread to listen for data
191
                                connectButton.addActionListener(new ActionListener(){
192
                                        @Override public void actionPerformed(ActionEvent arg0) {
193
                                                if(connectButton.getText().equals("Connect")) {
194
                                                        // attempt to connect to the serial port
195
                                                        chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());
196
                                                        chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
197
                                                        if(chosenPort.openPort()) {
198
                                                                connectButton.setText("Disconnect");
199
                                                                portList.setEnabled(false);
200
                                                        }
201
                                                        
202
                                                        // create a new thread that listens for incoming text and populates the graph
203
                                                        Thread thread = new Thread(){
204
                                                                @Override public void run() {
205
                                                                        Scanner scanner = new Scanner(chosenPort.getInputStream());
206
                                                                        while(scanner.hasNextLine()) {
207
                                                                                try {
208
                                                                                        while(scanner.hasNextLine())
209
                                                                                        {
210
                                                                                                while(scanner.hasNextInt())
211
                                                                                                {
212
                                                                                                        int angle = scanner.nextInt();
213
                                                                                                        int up = scanner.nextInt();
214
                                                                                                        int middle = scanner.nextInt();
215
                                                                                                        int down = scanner.nextInt();
216
                                                                                                        System.out.println(angle + "angle");
217
                                                                                                        System.out.println(up+ "up");
218
                                                                                                        System.out.println(middle +" middle");
219
                                                                                                        System.out.println(down +" down");
220
                                                                                                        Below_Values.setText(Integer.toString(middle));
221
                                                                                                        vibrationMotors(Below_Values);
222
                                                                                                        Above_Values.setText(Integer.toString(up));
223
                                                                                                        vibrationMotors(Above_Values);
224
                                                                                                        Ground_Values.setText(Integer.toString(down));
225
                                                                                                        vibrationMotors(Ground_Values);
226
                                                                                                        
227
                                                                                                }
228
                                                                                        }
229
                                                                                        BlindHelp.repaint();
230
                                                                                        
231
                                                                                } catch(Exception e) {}
232
                                                                        }
233
                                                                        scanner.close();
234
                                                                }
235
                                                        };
236
                                                        thread.start();
237
                                                        
238
                                                        } else { 
239
                                                                
240
                                                        // disconnect from the serial port
241
                                                        chosenPort.closePort();
242
                                                        portList.setEnabled(true);
243
                                                        connectButton.setText("Connect");
244
                                                        x = 0;
245
                                                }
246
                                        }
247
                                });                
248
                                
249
                JButton btnExit = new JButton("Exit");
250
                btnExit.addActionListener(new ActionListener() {
251
                        public void actionPerformed(ActionEvent arg0) {
252
                                System.exit(0);
253
                        }
254
                });
255
                btnExit.setBounds(372, 183, 59, 23);
256
                BlindHelp.getContentPane().add(btnExit);
257
                
258
        }
259
}