tobby48 4 anni fa
parent
commit
2f62b09238

+ 0
- 46
src/main/java/kr/co/swh/lecture/opensource/project/drawing/ChattingClientOutputThread.java Vedi File

@@ -1,46 +0,0 @@
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 Vedi File

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

+ 1
- 0
src/main/java/kr/co/swh/lecture/opensource/project/drawing/ServerThread.java Vedi File

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