tobby48 4 年前
父节点
当前提交
2f62b09238

+ 0
- 46
src/main/java/kr/co/swh/lecture/opensource/project/drawing/ChattingClientOutputThread.java 查看文件

1
-package kr.co.swh.lecture.opensource.project.drawing;
2
-
3
-import java.io.DataOutputStream;
4
-import java.io.IOException;
5
-import java.net.Socket;
6
-import java.util.Scanner;
7
-
8
-/**
9
- * <pre>
10
- * kr.co.swh.lecture.network.chat.client 
11
- * ChattingClientOutputThread.java
12
- *
13
- * 설명 :클라이언트 출력 스레드
14
- * </pre>
15
- * 
16
- * @since : 2018. 12. 28.
17
- * @author : tobby48
18
- * @version : v1.0
19
- */
20
-public class ChattingClientOutputThread extends Thread{
21
-	private Socket socket;
22
-	private String nick;
23
-	
24
-	public ChattingClientOutputThread(Socket socket, String nick) {
25
-		this.socket = socket;
26
-		this.nick = String.format("[%s] ", nick);
27
-	}
28
-	
29
-	public void run() {
30
-		DataOutputStream dos = null;
31
-		Scanner scn = null;
32
-		try {
33
-			dos = new DataOutputStream(socket.getOutputStream());
34
-			scn = new Scanner(System.in);
35
-			String line;
36
-			while((line = scn.nextLine())!=null) {
37
-				dos.writeUTF(nick + line);
38
-			}
39
-			socket.close();
40
-		}catch(IOException e) {
41
-			e.printStackTrace();
42
-		}finally {
43
-			try{ dos.close(); scn.close(); }catch(Exception e) {}
44
-		}
45
-	}
46
-}

+ 46
- 17
src/main/java/kr/co/swh/lecture/opensource/project/drawing/LineDraw.java 查看文件

2
 
2
 
3
 
3
 
4
 import java.awt.BasicStroke;
4
 import java.awt.BasicStroke;
5
+import java.awt.BorderLayout;
5
 import java.awt.Color;
6
 import java.awt.Color;
6
 import java.awt.Graphics;
7
 import java.awt.Graphics;
7
 import java.awt.Graphics2D;
8
 import java.awt.Graphics2D;
8
 import java.awt.Point;
9
 import java.awt.Point;
10
+import java.awt.event.ActionEvent;
11
+import java.awt.event.ActionListener;
9
 import java.awt.event.MouseAdapter;
12
 import java.awt.event.MouseAdapter;
10
 import java.awt.event.MouseEvent;
13
 import java.awt.event.MouseEvent;
11
 import java.awt.event.MouseMotionAdapter;
14
 import java.awt.event.MouseMotionAdapter;
21
 
24
 
22
 import javax.swing.JFrame;
25
 import javax.swing.JFrame;
23
 import javax.swing.JPanel;
26
 import javax.swing.JPanel;
27
+import javax.swing.JScrollPane;
28
+import javax.swing.JTextArea;
29
+import javax.swing.JTextField;
24
 
30
 
25
 import com.google.gson.Gson;
31
 import com.google.gson.Gson;
26
 import com.google.gson.reflect.TypeToken;
32
 import com.google.gson.reflect.TypeToken;
27
 
33
 
28
 public class LineDraw extends JPanel {
34
 public class LineDraw extends JPanel {
35
+	/**
36
+	 * 
37
+	 */
38
+	private static final long serialVersionUID = 1L;
39
+	
29
 	private List<List<Point>> point = new ArrayList<List<Point>>();
40
 	private List<List<Point>> point = new ArrayList<List<Point>>();
30
 	private List<List<Point>> drawingPoint = new ArrayList<List<Point>>();
41
 	private List<List<Point>> drawingPoint = new ArrayList<List<Point>>();
31
-	DataOutputStream dos;
42
+	private DataOutputStream drawingOutputStream;
43
+	private DrawingInputThread drawingInputThread;
32
 	
44
 	
33
-	class ChattingClientInputThread extends Thread{
45
+	class DrawingInputThread extends Thread{
34
 		private Socket socket;
46
 		private Socket socket;
35
 
47
 
36
-		public ChattingClientInputThread(Socket socket) {
48
+		public DrawingInputThread(Socket socket) {
37
 			this.socket = socket;
49
 			this.socket = socket;
38
 		}
50
 		}
39
 
51
 
41
 			DataInputStream dis = null;
53
 			DataInputStream dis = null;
42
 			try {
54
 			try {
43
 				dis = new DataInputStream(socket.getInputStream());
55
 				dis = new DataInputStream(socket.getInputStream());
44
-				String line = dis.readUTF();
45
-				Gson gson = new Gson();
46
-				drawingPoint = gson.fromJson(line, new TypeToken<List<List<Point>>>(){}.getType());
47
-				System.out.println(drawingPoint);
48
-				repaint();
56
+				while(true) {
57
+					String line = dis.readUTF();
58
+					Gson gson = new Gson();
59
+					drawingPoint = gson.fromJson(line, new TypeToken<List<List<Point>>>(){}.getType());
60
+					System.out.println(drawingPoint);
61
+					repaint();
62
+				}
49
 			}catch(IOException e) {
63
 			}catch(IOException e) {
50
 				e.printStackTrace();
64
 				e.printStackTrace();
51
 			}finally{
65
 			}finally{
52
-				try{ dis.close(); }catch(Exception e) {}
66
+//				try{ dis.close(); }catch(Exception e) {}
53
 			}
67
 			}
54
 		}
68
 		}
55
 	}
69
 	}
56
 	
70
 	
57
 	public LineDraw(Socket socket) {
71
 	public LineDraw(Socket socket) {
58
-		ChattingClientInputThread thread = new ChattingClientInputThread(socket);
59
-		thread.start();
72
+		drawingInputThread = new DrawingInputThread(socket);
73
+		drawingInputThread.start();
60
 		try {
74
 		try {
61
-			dos = new DataOutputStream(socket.getOutputStream());
75
+			drawingOutputStream = new DataOutputStream(socket.getOutputStream());
62
 		} catch (IOException e1) {
76
 		} catch (IOException e1) {
63
 			// TODO Auto-generated catch block
77
 			// TODO Auto-generated catch block
64
 			e1.printStackTrace();
78
 			e1.printStackTrace();
65
 		}
79
 		}
80
+	}
81
+	
82
+	public void addListener() {
66
 		
83
 		
67
 		addMouseListener(new MouseAdapter() {
84
 		addMouseListener(new MouseAdapter() {
68
 			public void mousePressed(MouseEvent event) {
85
 			public void mousePressed(MouseEvent event) {
84
 				Gson gg = new Gson();
101
 				Gson gg = new Gson();
85
 				point.get(point.size()-1).add(event.getPoint());
102
 				point.get(point.size()-1).add(event.getPoint());
86
 //				repaint();
103
 //				repaint();
87
-				
88
-				
104
+				System.out.println("mouseDragged " + point.size());
89
 				try {
105
 				try {
90
-					dos.writeUTF(gg.toJson(point));
106
+					drawingOutputStream.writeUTF(gg.toJson(point));
91
 				} catch (IOException e) {
107
 				} catch (IOException e) {
92
 					// TODO Auto-generated catch block
108
 					// TODO Auto-generated catch block
93
 					e.printStackTrace();
109
 					e.printStackTrace();
94
 				}
110
 				}
111
+				System.out.println("mouseDragged " + point.size() + " write");
95
 			}
112
 			}
96
 			
113
 			
97
 		});
114
 		});
100
 
117
 
101
 	public void paintComponent(Graphics g) {
118
 	public void paintComponent(Graphics g) {
102
 		super.paintComponent(g);
119
 		super.paintComponent(g);
120
+		System.out.println("paintComponent " + drawingPoint.size());
103
 		Graphics2D g2 = (Graphics2D) g;
121
 		Graphics2D g2 = (Graphics2D) g;
104
 		g2.setColor(new Color(0, 0, 128));
122
 		g2.setColor(new Color(0, 0, 128));
105
 		g2.setStroke(new BasicStroke(15f,
123
 		g2.setStroke(new BasicStroke(15f,
114
 	}
132
 	}
115
 
133
 
116
 	public static void main(String[] args) throws UnknownHostException, IOException {
134
 	public static void main(String[] args) throws UnknownHostException, IOException {
117
-//		Thread.currentThread().getContextClassLoader().getResourceAsStream("db.pro")
118
 		Socket socket = new Socket("127.0.0.1", 9998);
135
 		Socket socket = new Socket("127.0.0.1", 9998);
119
 		JFrame f = new JFrame();
136
 		JFrame f = new JFrame();
120
-		f.add(new LineDraw(socket));
137
+		LineDraw panel = new LineDraw(socket);
138
+		panel.addListener();
139
+//		JTextField userText = new JTextField();
140
+//		userText.setEditable(false);
141
+//		userText.addActionListener(new ActionListener(){
142
+//			public void actionPerformed(ActionEvent e){
143
+//				userText.setText("");
144
+//			}
145
+//		});
146
+//		f.add(userText, BorderLayout.NORTH);
147
+//		JTextArea chatWindow=new JTextArea();
148
+//		f.add(new JScrollPane(chatWindow),BorderLayout.CENTER);
149
+		f.add(panel);
121
 		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
150
 		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
122
 		f.setSize(800, 600);
151
 		f.setSize(800, 600);
123
 		f.setVisible(true);
152
 		f.setVisible(true);

+ 1
- 0
src/main/java/kr/co/swh/lecture/opensource/project/drawing/ServerThread.java 查看文件

41
             while(true){
41
             while(true){
42
                 line = dis.readUTF();
42
                 line = dis.readUTF();
43
                 //	서버로 들어온 메세지는 접속된 모든 클라이언트에게 전송
43
                 //	서버로 들어온 메세지는 접속된 모든 클라이언트에게 전송
44
+                System.out.println(line);
44
                 server.broadcast(line);
45
                 server.broadcast(line);
45
             }
46
             }
46
         }catch(IOException e){
47
         }catch(IOException e){