Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
3.7 kB
2
Indexable
Never
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.io.*;

public class VisualClient {
	
	ServerSocket serSock;
	Socket connection;
	InputStream in;
	OutputStream out;
	BufferedReader read;
	PrintWriter write;
	Timer timer;
	JFrame frame;
	JPanel panel;
	JPanel addressPane;
	JPanel inputPane;
	JTextArea chat;
	JTextField addressText;
	JButton addressButton;
	JTextField inputText;
	JButton inputButton;
	String address;
	public static final int port = 32000;
	
	private class ListenAddress implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			address = addressText.getText();
			if (address.length() == 0)
				JOptionPane.showMessageDialog(panel, "Please input an address");
			else {
				try {
					connection = new Socket(address, port);
					in = connection.getInputStream();
					out = connection.getOutputStream();
					chat.setEnabled(true);
					inputButton.setEnabled(true);
					inputText.setEnabled(true);
					read = new BufferedReader(new InputStreamReader(in, "UTF-8"));
					write = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
					timer = new Timer(200, new ListenTime());
					timer.start();
					
				} catch (UnknownHostException e1) {
					System.out.println("Error! Server not found. " + e);
				} catch (IOException e1) {
					System.out.println("Error! " + e);
				}
			}
		}
		
	}
	
	private class ListenTime implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			try {
				if (read.ready()) {
					chat.append("Mai boi: " + read.readLine() + "\n");
				}
			} catch (IOException e1) {
				chat.append("Error! " + e);
			}
			
		}
		
	}
	
	private class ListenInput implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String message = inputText.getText();
			chat.append("Me: " + message + "\n");
			write.println(message);
			write.flush();
			inputText.setText("");
		}
		
	}
	
	private class ListenEnter implements KeyListener{

		@Override
		public void keyPressed(KeyEvent arg0) {
			if (arg0.getKeyCode() == KeyEvent.VK_ENTER)
				inputButton.doClick();
			
		}

		@Override
		public void keyReleased(KeyEvent arg0) {}

		@Override
		public void keyTyped(KeyEvent arg0) {}

		
	}
	
	public VisualClient() {
		
		frame = new JFrame("Chat!");
		panel = new JPanel();
		addressPane = new JPanel();
		inputPane = new JPanel();
		chat = new JTextArea();
		addressText = new JTextField();
		addressButton = new JButton("Go!");
		inputText = new JTextField();
		inputButton = new JButton("Send!");
		
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		addressText.setPreferredSize(new Dimension(200, 25));
		inputText.setPreferredSize(new Dimension(200, 25));
		chat.setPreferredSize(new Dimension(300, 300));
		chat.setBorder(BorderFactory.createEtchedBorder());
		frame.add(panel);
		panel.add(addressPane);
		addressPane.add(addressText);
		addressPane.add(addressButton);
		panel.add(chat);
		panel.add(inputPane);
		inputPane.add(inputText);
		inputPane.add(inputButton);
		chat.setEnabled(false);
		inputButton.setEnabled(false);
		inputText.setEnabled(false);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
		addressButton.addActionListener(new ListenAddress());
		inputButton.addActionListener(new ListenInput());
		inputText.addKeyListener(new ListenEnter());

	}
	


	public static void main(String[] args) {
		
		VisualClient test = new VisualClient();
	}

}