|
| 1 | + |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import javax.swing.*; |
| 5 | +import java.awt.*; |
| 6 | +import java.awt.event.*; |
| 7 | +import java.awt.geom.*; |
| 8 | +import java.awt.image.*; |
| 9 | +import javax.swing.Timer; |
| 10 | +import java.util.*; |
| 11 | +import java.io.*; |
| 12 | +import java.net.*; |
| 13 | +import java.nio.*; |
| 14 | +public class ChatClient { |
| 15 | + |
| 16 | + |
| 17 | + public static void main(String[] args) { |
| 18 | + new ChatUser(); |
| 19 | + } |
| 20 | + |
| 21 | +} |
| 22 | + |
| 23 | +class ChatUser extends JFrame implements KeyListener, ActionListener{ |
| 24 | + |
| 25 | + int X = 400; |
| 26 | + int Y = 400; |
| 27 | + Dimension ScreenSize = GUI.getScreenSize(); |
| 28 | + |
| 29 | + |
| 30 | + Timer Stopwatch = new Timer(20, this); |
| 31 | + |
| 32 | + Link UpLink; |
| 33 | + String Label; |
| 34 | + |
| 35 | + int Offset = 11000; |
| 36 | + DataArray InputData = new DataArray(32, true); |
| 37 | + DataArray OutputData = new DataArray(128, true); |
| 38 | + boolean ClearChat; |
| 39 | + |
| 40 | + |
| 41 | + JTextArea MainTextArea = new JTextArea(""); |
| 42 | + JScrollPane MainTextPane = new JScrollPane(MainTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 43 | + |
| 44 | + JTextArea TypeTextArea = new JTextArea(""); |
| 45 | + JScrollPane TypeTextPane = new JScrollPane(TypeTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 46 | + |
| 47 | + JTextArea ListTextArea = new JTextArea(""); |
| 48 | + JScrollPane ListTextPane = new JScrollPane(ListTextArea, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 49 | + |
| 50 | + JButton SendButton = new JButton("SEND"); |
| 51 | + |
| 52 | + |
| 53 | + public void initComponents(){ |
| 54 | + Container C = this.getContentPane(); |
| 55 | + C.setBackground(Color.gray); |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + Insets I = this.getInsets(); |
| 60 | + System.out.println(I); |
| 61 | + X = X - I.left - I.right; |
| 62 | + Y = Y - I.top - I.bottom; |
| 63 | + |
| 64 | + int B = 10; |
| 65 | + int Boundary = 270; |
| 66 | + |
| 67 | + MainTextArea.setBounds(B, B, X - 2*B - (Y - Boundary), Boundary - 2*B); |
| 68 | + MainTextPane.setBounds(B, B, X - 2*B - (Y - Boundary), Boundary - 2*B); |
| 69 | + C.add(MainTextPane); |
| 70 | + |
| 71 | + |
| 72 | + TypeTextArea.setBounds(B, Boundary + B, X - 2*B - (Y - Boundary), Y - Boundary - 2*B); |
| 73 | + TypeTextPane.setBounds(B, Boundary + B, X - 2*B - (Y - Boundary), Y - Boundary - 2*B); |
| 74 | + TypeTextArea.addKeyListener(this); |
| 75 | + C.add(TypeTextPane); |
| 76 | + |
| 77 | + ListTextArea.setBounds(X - (Y - Boundary) + B, B, (Y - Boundary) - 2*B, Boundary - 2*B); |
| 78 | + ListTextPane.setBounds(X - (Y - Boundary) + B, B, (Y - Boundary) - 2*B, Boundary - 2*B); |
| 79 | + C.add(ListTextPane); |
| 80 | + |
| 81 | + SendButton.setBounds(X - (Y - Boundary) + B, Boundary + B, (Y - Boundary) - 2*B, (Y - Boundary) - 2*B); |
| 82 | + SendButton.addActionListener(this); |
| 83 | + C.add(SendButton); |
| 84 | + |
| 85 | + Font StandardFont = new Font("COURIER", Font.PLAIN, 12); |
| 86 | + MainTextArea.setFont(StandardFont); |
| 87 | + TypeTextArea.setFont(StandardFont); |
| 88 | + ListTextArea.setFont(StandardFont); |
| 89 | + |
| 90 | + } |
| 91 | + public void init(){ |
| 92 | + String TargetHost = JOptionPane.showInputDialog(null, "CHOOSE HOST"); |
| 93 | + InetAddress Target = null; |
| 94 | + |
| 95 | + try{ |
| 96 | + Target = InetAddress.getByName(TargetHost); |
| 97 | + }catch(Exception e){ |
| 98 | + System.out.println("ATTEMPT TO GET ADDRESS FAILED " + e); |
| 99 | + System.exit(0); |
| 100 | + } |
| 101 | + |
| 102 | + System.out.println("Attempting to connect to " + Target); |
| 103 | + |
| 104 | + for(int i = 1; i <= 5; i++){ |
| 105 | + Scanner MyScanner = new Scanner(Target, Offset, 4); |
| 106 | + Socket S = MyScanner.scan(5000); |
| 107 | + System.out.println("SCAN " + i + " COMPLETE"); |
| 108 | + System.out.println(S); |
| 109 | + if(S != null){ |
| 110 | + try{ |
| 111 | + UpLink = new Link(S); |
| 112 | + System.out.println("CONNECTING USING PORT " + S.getPort()); |
| 113 | + String MyLabel = null; |
| 114 | + do{ |
| 115 | + MyLabel = JOptionPane.showInputDialog(null, "SELECT LABEL"); |
| 116 | + }while(MyLabel == null || MyLabel.length() == 0); |
| 117 | + Label = MyLabel; |
| 118 | + |
| 119 | + OutputData.reset(); |
| 120 | + |
| 121 | + OutputData.writeString(Label); |
| 122 | + UpLink.Output.write(OutputData.getLengthBytes(), 0, 4); |
| 123 | + UpLink.Output.write(OutputData.Buffer, 0, OutputData.Count); |
| 124 | + UpLink.Output.flush(); |
| 125 | + OutputData.reset(); |
| 126 | + |
| 127 | + return; |
| 128 | + }catch(Exception e){ |
| 129 | + System.out.println("LINKAGE FAILED " + e); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + System.out.println("UNABLE TO CONNECT"); |
| 135 | + System.exit(0); |
| 136 | + |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + public ChatUser(){ |
| 143 | + //BASIC INITIALIZATION |
| 144 | + super("Chat User"); |
| 145 | + this.setSize(X, Y); |
| 146 | + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 147 | + this.setResizable(false); |
| 148 | + //SET CONTENT PANE |
| 149 | + Container contentArea = getContentPane(); |
| 150 | + contentArea.setLayout(null); |
| 151 | + //LAYOUT MANAGER |
| 152 | + //ADD ITEMS TO CONTENT PANE |
| 153 | + |
| 154 | + this.setLocation(ScreenSize.width / 2 - X / 2, ScreenSize.height / 2 - Y / 2); |
| 155 | + this.addKeyListener(this); |
| 156 | + |
| 157 | + // initSockets(); |
| 158 | + init(); |
| 159 | + Stopwatch.start(); |
| 160 | + |
| 161 | + //ADD CONTENT PANE AND PACK |
| 162 | + |
| 163 | + |
| 164 | + this.show(); |
| 165 | + initComponents(); |
| 166 | + this.setContentPane(contentArea); |
| 167 | + //this.pack(); |
| 168 | + } |
| 169 | + |
| 170 | + //EVENT TRIGGERS |
| 171 | + boolean CTRL = false; |
| 172 | + public void keyPressed(KeyEvent e){ |
| 173 | + System.out.println(e.getKeyCode()); |
| 174 | + if(e.getKeyCode() == 10){ |
| 175 | + if(CTRL){ |
| 176 | + TypeTextArea.append("\n"); |
| 177 | + |
| 178 | + }else{ |
| 179 | + sendOutput(); |
| 180 | + } |
| 181 | + } |
| 182 | + if(e.getKeyCode() == 17){ |
| 183 | + CTRL = true; |
| 184 | + } |
| 185 | + |
| 186 | + } |
| 187 | + public void keyReleased(KeyEvent e){ |
| 188 | + if(e.getKeyCode() == 17){ |
| 189 | + CTRL = !true; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | + public void keyTyped(KeyEvent e){ |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + public void printInput(){ |
| 200 | + try{ |
| 201 | + |
| 202 | + while(UpLink.Input.available() > 0){ |
| 203 | + |
| 204 | + System.out.println("RECIEVING"); |
| 205 | + |
| 206 | + InputData.reset(); |
| 207 | + UpLink.Input.read(InputData.Length, 0, 4); |
| 208 | + InputData.ensureCapacity(InputData.getLengthInt()); |
| 209 | + |
| 210 | + UpLink.Input.read(InputData.Buffer, 0, InputData.getLengthInt()); |
| 211 | + boolean Changed = InputData.readBoolean(); |
| 212 | + if(Changed){ |
| 213 | + |
| 214 | + int Count = InputData.readInt(); |
| 215 | + if(Count != 0){ |
| 216 | + ListTextArea.setText(""); |
| 217 | + } |
| 218 | + for(int i = 0; i < Count; i++){ |
| 219 | + ListTextArea.append(InputData.readString()); |
| 220 | + } |
| 221 | + |
| 222 | + MainTextArea.setText(InputData.readString()); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + }catch(IOException e){ |
| 227 | + System.out.println("IOEXCEPTION " + e); |
| 228 | + } |
| 229 | + } |
| 230 | + public void sendOutput(){ |
| 231 | + try{ |
| 232 | + OutputData.reset(); |
| 233 | + OutputData.writeString(TypeTextArea.getText()); |
| 234 | + |
| 235 | + UpLink.Output.write(OutputData.getLengthBytes(), 0, 4); |
| 236 | + UpLink.Output.write(OutputData.Buffer, 0, OutputData.Count); |
| 237 | + ClearChat = true; |
| 238 | + }catch(IOException e){ |
| 239 | + System.out.println(e); |
| 240 | + System.exit(0); |
| 241 | + } |
| 242 | + } |
| 243 | + public void actionPerformed(ActionEvent e){ |
| 244 | + |
| 245 | + if(e.getSource() == Stopwatch){ |
| 246 | + if(ClearChat){ |
| 247 | + TypeTextArea.setText(""); |
| 248 | + ClearChat = false; |
| 249 | + } |
| 250 | + |
| 251 | + // sendOutput(); |
| 252 | + printInput(); |
| 253 | + }else if(e.getSource() == SendButton){ |
| 254 | + sendOutput(); |
| 255 | + } |
| 256 | + |
| 257 | + } |
| 258 | + |
| 259 | +} |
| 260 | + |
| 261 | + |
0 commit comments