하나의 프로그램이었던 공 굴리기 프로젝트를 두 개로 나누어서 소켓을 이용한 통신으로 만들어봤다.
소스코드
rollingBallClinet 프로젝트
SendingMessageThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package ball.client;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class SendingMessageThread extends Thread{
private Socket rollingBallSocket;
private String orderMessage;
private int selectedColorMessage;
private String selectedBallGroupNameMessage;
private String newBallGroupNameMessage;
private boolean isWindowClosed;
@Override
public void run() {
super.run();
try {
BufferedWriter sendingMessageBuffer = new BufferedWriter(new OutputStreamWriter(rollingBallSocket.getOutputStream()));
while(true) {
if(orderMessage==null)
Thread.sleep(1);
else if(orderMessage!=null) {
sendingMessageBuffer.write(orderMessage+" "+selectedColorMessage+" "
+newBallGroupNameMessage+" "+selectedBallGroupNameMessage);
sendingMessageBuffer.newLine();
sendingMessageBuffer.flush();
orderMessage=null;
}
if(this.isWindowClosed) {
break;
}
}
sendingMessageBuffer.close();
rollingBallSocket.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void setWindowClosed(boolean isClosed) {
this.isWindowClosed = isClosed;
}
public void setSocket(Socket socket) {
this.rollingBallSocket = socket;
}
public void setOrderMessage(String orderMessage) {
this.orderMessage = orderMessage;
}
public void setSelectedColorMessage(int selectedColorMessage) {
this.selectedColorMessage = selectedColorMessage;
}
public void setSelectedBallGroupMessage(String selectedBallGroupNameMessage) {
this.selectedBallGroupNameMessage = selectedBallGroupNameMessage;
}
public void setNewBallGroupNameMessage(String newBallGroupNameMessage) {
this.newBallGroupNameMessage = newBallGroupNameMessage;
}
}
|
cs |
ButtonActionListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package ball.controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonActionListener implements ActionListener{
private Core core;
public ButtonActionListener(Core core) {
this.core = core;
}
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if(actionCommand == "Play")
core.clickedPlay();
else if(actionCommand == "Stop")
core.clickedStop();
else if(actionCommand == "Ball +")
core.clickedAddBall();
else if(actionCommand == "Ball -")
core.clickedDeleteBall();
else if(actionCommand == "Group 생성")
core.clickedCreateBallGroup();
else if(actionCommand == "Group 삭제")
core.clickedDeleteBallGroup();
}
}
|
cs |
Core.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package ball.controller;
import java.net.Socket;
import ball.client.SendingMessageThread;
import ball.view.RollingBallView;
public class Core {
private RollingBallView rollingBallView;
private SendingMessageThread sendingThread;
public static final int WIDTH = 480;
public static final int HEIGHT = 480;
public Core() {
rollingBallView = new RollingBallView();
this.init();
this.start();
}
private void init() {
rollingBallView.registerButtonListener(new ButtonActionListener(this));
rollingBallView.registerWindowListener(new WindowClosingListener(this));
}
private void start() {
try {
Socket rollingBallSocket = new Socket("10.0.13.47",8884);
sendingThread = new SendingMessageThread();
sendingThread.setSocket(rollingBallSocket);
sendingThread.start();
}catch (Exception e) {
e.printStackTrace();
}
}
public void clickedPlay() {
if(rollingBallView.getGroupCount()!=0) {
sendingThread.setSelectedBallGroupMessage(rollingBallView.getSelectedGroup());
sendingThread.setOrderMessage("play");
}
}
public void clickedStop() {
if(rollingBallView.getGroupCount()!=0) {
sendingThread.setSelectedBallGroupMessage(rollingBallView.getSelectedGroup());
sendingThread.setOrderMessage("stop");
}
}
public void clickedAddBall() {
if(rollingBallView.getGroupCount()!=0) {
sendingThread.setSelectedBallGroupMessage(rollingBallView.getSelectedGroup());
sendingThread.setOrderMessage("addBall");
}else
System.out.println("그룹을 만드세요");
}
public void clickedDeleteBall() {
if(rollingBallView.getGroupCount()!=0) {
sendingThread.setSelectedBallGroupMessage(rollingBallView.getSelectedGroup());
sendingThread.setOrderMessage("deleteBall");
}
}
public void clickedCreateBallGroup() {
sendingThread.setSelectedColorMessage(rollingBallView.getSelectedGroupColor().getRGB());
sendingThread.setNewBallGroupNameMessage(rollingBallView.getNewBallGroupName());
sendingThread.setOrderMessage("createBallGroup");
rollingBallView.addGroup(rollingBallView.getNewBallGroupName());
}
public void clickedDeleteBallGroup() {
if(rollingBallView.getGroupCount()!=0) {
sendingThread.setSelectedBallGroupMessage(rollingBallView.getSelectedGroup());
sendingThread.setOrderMessage("deleteBallGroup");
rollingBallView.removeGroup();
}else
System.out.println("삭제할 그룹이 없습니다");
}
public void clickedWindowClose() {
if(sendingThread!=null)
sendingThread.setWindowClosed(true);
}
public static void main(String[] args) {
new Core();
}
}
|
cs |
WindowClosingListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package ball.controller;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowClosingListener implements WindowListener{
private Core core;
WindowClosingListener(Core core){
this.core = core;
}
@Override
public void windowClosing(WindowEvent e) {
core.clickedWindowClose();
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
|
cs |
BallControlComponet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package ball.view;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import ball.controller.ButtonActionListener;
public class BallControlComponent {
private JButton playButton;
private JButton stopButton;
public BallControlComponent() {
playButton = new JButton("Play");
stopButton = new JButton("Stop");
}
public void registerButtonActionListener(ButtonActionListener buttonActionListener) {
playButton.addActionListener(buttonActionListener);
stopButton.addActionListener(buttonActionListener);
}
public JPanel getPanel() {
return initBallControlPanel();
}
private JPanel initBallControlPanel() {
JPanel ballControllPanel = new JPanel();
ballControllPanel.setLayout(new FlowLayout());
ballControllPanel.add(playButton);
ballControllPanel.add(stopButton);
return ballControllPanel;
}
}
|
cs |
BallCreationComponent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package ball.view;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import ball.controller.ButtonActionListener;
public class BallCreationComponent {
private JButton ballAddButton;
private JButton ballDeleteButton;
private JComboBox<String> ballColorComboBox;
BallCreationComponent(){
ballAddButton = new JButton("Ball +");
ballDeleteButton = new JButton("Ball -");
ballColorComboBox = new JComboBox<String>();
}
public void registerButtonActionListener(ButtonActionListener buttonActionListener) {
ballAddButton.addActionListener(buttonActionListener);
ballDeleteButton.addActionListener(buttonActionListener);
}
public JPanel getPanel() {
return initBallCreationPanel();
}
private JPanel initBallCreationPanel() {
JPanel ballCreationPanel = new JPanel();
ballCreationPanel.setLayout(new FlowLayout());
ballCreationPanel.add(new JLabel("볼 생성===>"));
ballCreationPanel.add(ballColorComboBox);
ballCreationPanel.add(ballAddButton);
ballCreationPanel.add(ballDeleteButton);
return ballCreationPanel;
}
public String getSelectedComboBoxItem() {
return ballColorComboBox.getSelectedItem().toString();
}
public void addComboBoxItem(String ballGroupName) {
if(ballColorComboBox.getItemCount()==0)
ballColorComboBox.addItem(ballGroupName);
else {
boolean isSameName = false;
for(int i=0; i<ballColorComboBox.getItemCount(); i++) {
if(ballGroupName.equals(ballColorComboBox.getItemAt(i).toString())) {
isSameName = true;
}
}
if(!isSameName)
ballColorComboBox.addItem(ballGroupName);
}
}
public int getBallColorComboBoxItemCount() {
return ballColorComboBox.getItemCount();
}
public void removeComoboBoxItem() {
ballColorComboBox.removeItem(ballColorComboBox.getSelectedItem());
}
}
|
cs |
BallGroupCreationComponent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package ball.view;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import ball.controller.ButtonActionListener;
public class BallCreationComponent {
private JButton ballAddButton;
private JButton ballDeleteButton;
private JComboBox<String> ballColorComboBox;
BallCreationComponent(){
ballAddButton = new JButton("Ball +");
ballDeleteButton = new JButton("Ball -");
ballColorComboBox = new JComboBox<String>();
}
public void registerButtonActionListener(ButtonActionListener buttonActionListener) {
ballAddButton.addActionListener(buttonActionListener);
ballDeleteButton.addActionListener(buttonActionListener);
}
public JPanel getPanel() {
return initBallCreationPanel();
}
private JPanel initBallCreationPanel() {
JPanel ballCreationPanel = new JPanel();
ballCreationPanel.setLayout(new FlowLayout());
ballCreationPanel.add(new JLabel("볼 생성===>"));
ballCreationPanel.add(ballColorComboBox);
ballCreationPanel.add(ballAddButton);
ballCreationPanel.add(ballDeleteButton);
return ballCreationPanel;
}
public String getSelectedComboBoxItem() {
return ballColorComboBox.getSelectedItem().toString();
}
public void addComboBoxItem(String ballGroupName) {
if(ballColorComboBox.getItemCount()==0)
ballColorComboBox.addItem(ballGroupName);
else {
boolean isSameName = false;
for(int i=0; i<ballColorComboBox.getItemCount(); i++) {
if(ballGroupName.equals(ballColorComboBox.getItemAt(i).toString())) {
isSameName = true;
}
}
if(!isSameName)
ballColorComboBox.addItem(ballGroupName);
}
}
public int getBallColorComboBoxItemCount() {
return ballColorComboBox.getItemCount();
}
public void removeComoboBoxItem() {
ballColorComboBox.removeItem(ballColorComboBox.getSelectedItem());
}
}
|
cs |
RollingBallView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package ball.view;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import ball.controller.ButtonActionListener;
import ball.controller.WindowClosingListener;
public class RollingBallView extends JFrame{
private JFrame frame;
private BallControlComponent ballControlComponent;
private BallCreationComponent ballCreationComponent;
private BallGroupCreationComponent ballGroupCreationComponent;
public RollingBallView() {
frame = new JFrame("공 굴리기");
ballControlComponent = new BallControlComponent();
ballCreationComponent = new BallCreationComponent();
ballGroupCreationComponent = new BallGroupCreationComponent();
initFrame();
}
private void initFrame(){
JPanel boxPanel = initBoxPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(boxPanel,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private JPanel initBoxPanel() {
JPanel boxPanel = new JPanel();
boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.Y_AXIS));
boxPanel.add(ballControlComponent.getPanel());
boxPanel.add(ballCreationComponent.getPanel());
boxPanel.add(ballGroupCreationComponent.getPanel());
return boxPanel;
}
public Color getSelectedGroupColor() {
return ballGroupCreationComponent.getSelectedGroupColor();
}
public void registerButtonListener(ButtonActionListener buttonActionListener) {
ballControlComponent.registerButtonActionListener(buttonActionListener);
ballCreationComponent.registerButtonActionListener(buttonActionListener);
ballGroupCreationComponent.registerButtonActionListener(buttonActionListener);
}
public String getSelectedGroup() {
return ballCreationComponent.getSelectedComboBoxItem();
}
public String getNewBallGroupName() {
return ballGroupCreationComponent.getBallGroupNameFromText();
}
public void addGroup(String newBallGroupName) {
ballCreationComponent.addComboBoxItem(newBallGroupName);
}
public void removeGroup() {
ballCreationComponent.removeComoboBoxItem();
}
public int getGroupCount() {
return ballCreationComponent.getBallColorComboBoxItemCount();
}
public void registerWindowListener(WindowClosingListener windowClosingListener) {
frame.addWindowListener(windowClosingListener);
}
}
|
cs |
rollingBallServer프로젝트
ReceivedMessageThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package ball.server;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import ball.controller.Core;
public class ReceivedMessageThread extends Thread{
private Socket buttonViewSocket;
private Core core;
@Override
public void run() {
super.run();
try {
BufferedReader receivedMessageBuffer = new BufferedReader(new InputStreamReader(buttonViewSocket.getInputStream()));
String receivedMessage;
String[] interpretedMessage;
String order;
String selectedColorString;
String ballGroupName;
String selectedBallGroup;
while(true) {
receivedMessage = receivedMessageBuffer.readLine();
if(receivedMessage==null)
break;
interpretedMessage = receivedMessage.split(" ");
order = interpretedMessage[0];
selectedColorString = interpretedMessage[1];
ballGroupName = interpretedMessage[2];
selectedBallGroup = interpretedMessage[3];
Color selectedColor = Color.getColor(null, Integer.parseInt(selectedColorString));
if(order.equals("play")) {
core.clickedPlay(selectedBallGroup);
}
else if(order.equals("stop")) {
core.clickedStop(selectedBallGroup);
}
else if(order.equals("addBall")) {
core.clickedAddBall(selectedBallGroup);
}
else if(order.equals("deleteBall")) {
core.clickedDeleteBall(selectedBallGroup);
}
else if(order.equals("createBallGroup")) {
core.clickedCreateBallGroup(selectedColor,ballGroupName);
}
else if(order.equals("deleteBallGroup")) {
core.clickedDeleteBallGroup(selectedBallGroup);
}
else
System.out.println("order error");
}
receivedMessageBuffer.close();
buttonViewSocket.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void setSocket(Socket buttonViewSocket) {
this.buttonViewSocket = buttonViewSocket;
}
public void setCore(Core core) {
this.core = core;
}
}
|
cs |
Core.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
package ball.controller;
import java.awt.Color;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Set;
import ball.data.Ball;
import ball.data.BallGroup;
import ball.data.BallGroupManager;
import ball.server.ReceivedMessageThread;
import ball.view.RollingBallView;
public class Core {
private RollingBallView rollingBallView;
private BallGroupManager ballGroupManager;
public static final int WIDTH = 780;
public static final int HEIGHT = 480;
public Core() {
rollingBallView = new RollingBallView();
ballGroupManager = new BallGroupManager();
this.setBallManager();
this.initSocket();
this.start();
}
private void initSocket() {
System.out.println("draw 대기중");
try {
ServerSocket serverSocket = new ServerSocket(8884);
Socket remoteControllerSocket = serverSocket.accept();
ReceivedMessageThread receivedMessageThread = new ReceivedMessageThread();
receivedMessageThread.setSocket(remoteControllerSocket);
receivedMessageThread.setCore(this);
receivedMessageThread.start();
}catch (Exception e) {
e.printStackTrace();
}
}
private void setBallManager() {
rollingBallView.setBallManager(ballGroupManager);
}
private void start() {
while(true) {
this.moveBallGroups();
this.draw();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void moveBallGroups() {
Set<String> ballGroupKeys = ballGroupManager.getBallGroupKeySet();
for(String ballGroupKey : ballGroupKeys) {
BallGroup ballGroup = ballGroupManager.getBallGroupByName(ballGroupKey);
if(ballGroup.isPlay())
moveBallGroup(ballGroup);
}
}
private void moveBallGroup(BallGroup ballGroup) {
Ball[] balls = ballGroup.getBalls();
for(int i=0; i<balls.length;i++) {
Ball ball = balls[i];
moveBall(ball);
reflectBall(ball);
}
}
private void moveBall(Ball ball) {
if(ball.isRight())
ball.setBallPosX(ball.getBallPosX() + 1);
else
ball.setBallPosX(ball.getBallPosX() - 1);
if(ball.isUp())
ball.setBallPosY(ball.getBallPosY() + 1);
else
ball.setBallPosY(ball.getBallPosY() - 1);
}
private void reflectBall(Ball ball) {
if(ball.getBallPosX() - 10 <=0 || ball.getBallPosX() + 10>= WIDTH)
ball.setRight(!ball.isRight());
if(ball.getBallPosY() - 10 <=0 || ball.getBallPosY() + 10>= HEIGHT)
ball.setUp(!ball.isUp());
}
private void draw() {
rollingBallView.draw();
}
public void clickedPlay(String selectedBallGroupName) {
this.setPlay(true,selectedBallGroupName);
}
public void clickedStop(String selectedBallGroupName) {
this.setPlay(false,selectedBallGroupName);
}
public void clickedAddBall(String selectedBallGroupName) {
this.addBall(selectedBallGroupName);
}
public void clickedDeleteBall(String selectedBallGroupName) {
this.deleteBall(selectedBallGroupName);
}
public void clickedCreateBallGroup(Color selectedColor, String newBallGroupName) {
this.CreateBallGroup(selectedColor,newBallGroupName);
}
public void clickedDeleteBallGroup(String selectedBallGroupName) {
this.DeleteBallGroup(selectedBallGroupName);
}
private void setPlay(boolean isPlay, String selectedBallGroupName) {
if(ballGroupManager.getBallGroupsSize()!=0) {
BallGroup selectedBallGroup = ballGroupManager.getBallGroupByName(selectedBallGroupName);
selectedBallGroup.setPlay(isPlay);
}
}
private void addBall(String selectedBallGroupName) {
if(ballGroupManager.getBallGroupsSize()!=0) {
BallGroup selectedBallGroup = ballGroupManager.getBallGroupByName(selectedBallGroupName);
ballGroupManager.addBallToGroup(selectedBallGroup);
}
}
private void deleteBall(String selectedBallGroupName) {
if(ballGroupManager.getBallGroupsSize()!=0) {
BallGroup selectedBallGroup = ballGroupManager.getBallGroupByName(selectedBallGroupName);
ballGroupManager.removeBallfromGroup(selectedBallGroup);
}
}
private void CreateBallGroup(Color selectedColor, String newBallGroupName) {
ballGroupManager.addBallGroup(newBallGroupName, selectedColor);
}
private void DeleteBallGroup(String selectedBallGroupName) {
if(ballGroupManager.getBallGroupsSize()!=0) {
ballGroupManager.removeBallGroup(selectedBallGroupName);
}
}
public static void main(String[] args) {
new Core();
}
}
|
cs |
Ball.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package ball.data;
import java.util.Random;
import ball.controller.Core;
public class Ball {
private int PosX;
private int PosY;
private boolean isRight;
private boolean isUp;
private boolean isPlay;
public boolean isPlay() {
return isPlay;
}
public void setPlay(boolean isPlay) {
this.isPlay = isPlay;
}
public Ball() {
Random r = new Random();
this.isRight = true;
this.isUp = true;
this.PosX = 10 + r.nextInt(Core.WIDTH-50);
this.PosY = 10 + r.nextInt(Core.HEIGHT-50);
}
public int getBallPosX() {
return PosX;
}
public void setBallPosX(int ballPosX) {
this.PosX = ballPosX;
}
public int getBallPosY() {
return PosY;
}
public void setBallPosY(int ballPosY) {
this.PosY = ballPosY;
}
public boolean isRight() {
return isRight;
}
public void setRight(boolean isRight) {
this.isRight = isRight;
}
public boolean isUp() {
return isUp;
}
public void setUp(boolean isUp) {
this.isUp = isUp;
}
}
|
cs |
BallGroup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package ball.data;
import java.awt.Color;
import java.util.LinkedList;
public class BallGroup {
private LinkedList<Ball> ballList;
private Color color;
private boolean isPlay;
public BallGroup(Color color) {
ballList = new LinkedList<Ball>();
this.color = color;
this.isPlay = false;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public boolean isPlay() {
return isPlay;
}
public void setPlay(boolean isPlay) {
this.isPlay = isPlay;
}
public int getBallCount() {
return ballList.size();
}
public void addBall(Ball ball) {
ballList.add(ball);
}
public void removeBall() {
ballList.remove();
}
public Ball[] getBalls(){
return ballList.toArray(new Ball[ballList.size()]);
}
}
|
cs |
BallGroupManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package ball.data;
import java.awt.Color;
import java.util.LinkedHashMap;
import java.util.Set;
public class BallGroupManager {
private LinkedHashMap<String, BallGroup> ballGroups;
public BallGroupManager() {
ballGroups = new LinkedHashMap<String, BallGroup>();
}
public void addBallGroup(String ballGroupKey, Color color) {
BallGroup ballGroup = new BallGroup(color);
ballGroups.put(ballGroupKey, ballGroup);
}
public BallGroup getBallGroupByName(String selectedBallGroupName) {
return ballGroups.get(selectedBallGroupName);
}
public void addBallToGroup(BallGroup ballGroup) {
Ball ball = new Ball();
ballGroup.addBall(ball);
}
public void removeBallfromGroup(BallGroup ballGroup) {
if(ballGroup.getBallCount()!=0)
ballGroup.removeBall();
}
public void removeBallGroup(String selectedBallGroupName) {
if(ballGroups.size()!=0)
ballGroups.remove(selectedBallGroupName);
}
public Ball[] getBallsFromGroup(BallGroup ballGroup) {
return ballGroup.getBalls();
}
public Set<String> getBallGroupKeySet() {
return ballGroups.keySet();
}
public int getBallCount(BallGroup ballGroup) {
return ballGroup.getBallCount();
}
public int getBallGroupsSize() {
return ballGroups.size();
}
}
|
cs |
BallDrawComponet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package ball.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Set;
import javax.swing.JComponent;
import ball.controller.Core;
import ball.data.Ball;
import ball.data.BallGroup;
import ball.data.BallGroupManager;
public class BallDrawComponent extends JComponent {
BallGroupManager ballGroupManager;
public Dimension getPreferredSize() {
return new Dimension(Core.WIDTH,Core.HEIGHT);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, Core.WIDTH, Core.HEIGHT);
Set<String> ballGroupKeys = ballGroupManager.getBallGroupKeySet();
for(String ballGroupKey : ballGroupKeys) {
BallGroup ballGroup = ballGroupManager.getBallGroupByName(ballGroupKey);
Ball[] balls = ballGroup.getBalls();
for(int i=0; i<ballGroup.getBallCount(); i++) {
g.setColor(ballGroup.getColor());
g.fillOval(balls[i].getBallPosX()-10, balls[i].getBallPosY()-10, 20, 20);
}
}
}
public void setBallGroupManager(BallGroupManager ballGroupManager) {
this.ballGroupManager = ballGroupManager;
}
}
|
cs |
RollingBallView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package ball.view;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import ball.data.BallGroupManager;
public class RollingBallView extends JFrame{
private JFrame frame;
private BallDrawComponent ballDrawComponent;
public RollingBallView() {
frame = new JFrame("공 굴리기");
ballDrawComponent = new BallDrawComponent();
initFrame();
}
private void initFrame(){
JPanel boxPanel = initBoxPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(boxPanel,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private JPanel initBoxPanel() {
JPanel boxPanel = new JPanel();
boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.Y_AXIS));
boxPanel.add(ballDrawComponent);
return boxPanel;
}
public void setBallManager(BallGroupManager ballGroupManager) {
ballDrawComponent.setBallGroupManager(ballGroupManager);
}
public void draw() {
ballDrawComponent.repaint();
}
}
|
cs |
스트림은 데이터의 통로 역할이라고 생각하면 편한다.
InputStream ====> byte단위로 읽는다
(Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input. )
InputStramReader ====> char단위로 읽는다.
(An InputStreamReader is a bridge from byte streams to character streams)
BufferedReader ====> data를 모았다가 newLine이 발생되면 보낸다.
(Reads text from a character-input stream )
서버와 통신을 할 때 서브 스레드를 만들어서 통신을 했다.
통신을 하다보니 nullpointException이 제일 골치아팠다. 이것 때문에 디버깅을 2시간은 한 것 같다..
통신 사용법
Server는
socket을 이용해 line을 받는다.
라인은 order + selectedColor + newBallGroupName + selectedBallGroupName으로 되어있는데
selectedColor는 int 나머지는 String 타입이다
order : "Play", "Stop", "Ball +", "Ball -", "Group 생성", "Group 삭제" (String 값)
selectedColor : 새로 만들 그룹의 색 RGB형식의 Color
newBallGroupName : 새로 만들 그룹의 이름 (String)
selectedBallGroupName : Play, Stop Ball+ Ball - 등을 실행할 그룹 (String)
각 메세지들은 " " space로 구분해서 받는다.
ex) "Play -63633 redballs blueballs"
위의 스트링중 필요한 Message만 받아서 사용함.
'Java > 통신' 카테고리의 다른 글
activeMQ를 이용한 JMS (0) | 2021.01.26 |
---|---|
JAXB 통신 example (0) | 2021.01.21 |