|
@@ -0,0 +1,125 @@
|
|
1
|
+package kr.co.swh.lecture.opensource.project.drawing;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+import java.awt.BasicStroke;
|
|
5
|
+import java.awt.Color;
|
|
6
|
+import java.awt.Graphics;
|
|
7
|
+import java.awt.Graphics2D;
|
|
8
|
+import java.awt.Point;
|
|
9
|
+import java.awt.event.MouseAdapter;
|
|
10
|
+import java.awt.event.MouseEvent;
|
|
11
|
+import java.awt.event.MouseMotionAdapter;
|
|
12
|
+import java.awt.geom.Line2D;
|
|
13
|
+import java.io.BufferedWriter;
|
|
14
|
+import java.io.DataInputStream;
|
|
15
|
+import java.io.DataOutputStream;
|
|
16
|
+import java.io.IOException;
|
|
17
|
+import java.net.Socket;
|
|
18
|
+import java.net.UnknownHostException;
|
|
19
|
+import java.util.ArrayList;
|
|
20
|
+import java.util.List;
|
|
21
|
+
|
|
22
|
+import javax.swing.JFrame;
|
|
23
|
+import javax.swing.JPanel;
|
|
24
|
+
|
|
25
|
+import com.google.gson.Gson;
|
|
26
|
+import com.google.gson.reflect.TypeToken;
|
|
27
|
+
|
|
28
|
+public class LineDraw extends JPanel {
|
|
29
|
+ private List<List<Point>> point = new ArrayList<List<Point>>();
|
|
30
|
+ private List<List<Point>> drawingPoint = new ArrayList<List<Point>>();
|
|
31
|
+ DataOutputStream dos;
|
|
32
|
+
|
|
33
|
+ class ChattingClientInputThread extends Thread{
|
|
34
|
+ private Socket socket;
|
|
35
|
+
|
|
36
|
+ public ChattingClientInputThread(Socket socket) {
|
|
37
|
+ this.socket = socket;
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ public void run() {
|
|
41
|
+ DataInputStream dis = null;
|
|
42
|
+ try {
|
|
43
|
+ 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();
|
|
49
|
+ }catch(IOException e) {
|
|
50
|
+ e.printStackTrace();
|
|
51
|
+ }finally{
|
|
52
|
+ try{ dis.close(); }catch(Exception e) {}
|
|
53
|
+ }
|
|
54
|
+ }
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ public LineDraw(Socket socket) {
|
|
58
|
+ ChattingClientInputThread thread = new ChattingClientInputThread(socket);
|
|
59
|
+ thread.start();
|
|
60
|
+ try {
|
|
61
|
+ dos = new DataOutputStream(socket.getOutputStream());
|
|
62
|
+ } catch (IOException e1) {
|
|
63
|
+ // TODO Auto-generated catch block
|
|
64
|
+ e1.printStackTrace();
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ addMouseListener(new MouseAdapter() {
|
|
68
|
+ public void mousePressed(MouseEvent event) {
|
|
69
|
+ List<Point> list = new ArrayList<Point>();
|
|
70
|
+ point.add(list);
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ @Override
|
|
74
|
+ public void mouseClicked(MouseEvent e) {
|
|
75
|
+ // TODO Auto-generated method stub
|
|
76
|
+
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ });
|
|
80
|
+
|
|
81
|
+ addMouseMotionListener(new MouseMotionAdapter() {
|
|
82
|
+ public void mouseDragged(MouseEvent event) {
|
|
83
|
+ Point p = event.getPoint();
|
|
84
|
+ Gson gg = new Gson();
|
|
85
|
+ point.get(point.size()-1).add(event.getPoint());
|
|
86
|
+// repaint();
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+ try {
|
|
90
|
+ dos.writeUTF(gg.toJson(point));
|
|
91
|
+ } catch (IOException e) {
|
|
92
|
+ // TODO Auto-generated catch block
|
|
93
|
+ e.printStackTrace();
|
|
94
|
+ }
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ });
|
|
98
|
+
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ public void paintComponent(Graphics g) {
|
|
102
|
+ super.paintComponent(g);
|
|
103
|
+ Graphics2D g2 = (Graphics2D) g;
|
|
104
|
+ g2.setColor(new Color(0, 0, 128));
|
|
105
|
+ g2.setStroke(new BasicStroke(15f,
|
|
106
|
+ BasicStroke.CAP_ROUND,
|
|
107
|
+ BasicStroke.JOIN_ROUND));
|
|
108
|
+ for (int i = 0; i < drawingPoint.size(); i++) {
|
|
109
|
+ List<Point> l = drawingPoint.get(i);
|
|
110
|
+ for (int j = 1; j < l.size(); j++) {
|
|
111
|
+ g2.draw(new Line2D.Float(l.get(j-1), l.get(j)));
|
|
112
|
+ }
|
|
113
|
+ }
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ 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);
|
|
119
|
+ JFrame f = new JFrame();
|
|
120
|
+ f.add(new LineDraw(socket));
|
|
121
|
+ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
122
|
+ f.setSize(800, 600);
|
|
123
|
+ f.setVisible(true);
|
|
124
|
+ }
|
|
125
|
+}
|