-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathballcontrol.java
More file actions
126 lines (103 loc) · 3.38 KB
/
ballcontrol.java
File metadata and controls
126 lines (103 loc) · 3.38 KB
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
// Simple test of the network to demonstrate the importance
// of formatting network data traffic
// Students will need to determine the appropriate message format
// to send to the server to make any of the four balls move
public class ballcontrol implements ActionListener{
JFrame theframe;
JGraphics thepanel;
Timer thetimer;
String strNetText;
// Super Socket Master
SuperSocketMaster ssm;
public void actionPerformed(ActionEvent evt){
if(evt.getSource() == thetimer){
thepanel.repaint();
}else if(evt.getSource() == ssm){
strNetText = ssm.readText();
if(strNetText.equals("bbu")){
thepanel.intbby -=3;
}else if(strNetText.equals("bbd")){
thepanel.intbby +=3;
}else if(strNetText.equals("bbl")){
thepanel.intbbx -=3;
}else if(strNetText.equals("bbr")){
thepanel.intbbx +=3;
}else if(strNetText.equals("rbu")){
thepanel.intrby -=3;
}else if(strNetText.equals("rbd")){
thepanel.intrby +=3;
}else if(strNetText.equals("rbl")){
thepanel.intrbx -=3;
}else if(strNetText.equals("rbr")){
thepanel.intrbx +=3;
}else if(strNetText.equals("gbu")){
thepanel.intgby -=3;
}else if(strNetText.equals("gbd")){
thepanel.intgby +=3;
}else if(strNetText.equals("gbl")){
thepanel.intgbx -=3;
}else if(strNetText.equals("gbr")){
thepanel.intgbx +=3;
}else if(strNetText.equals("cbu")){
thepanel.intcby -=3;
}else if(strNetText.equals("cbd")){
thepanel.intcby +=3;
}else if(strNetText.equals("cbl")){
thepanel.intcbx -=3;
}else if(strNetText.equals("cbr")){
thepanel.intcbx +=3;
}
}
}
public ballcontrol(){
theframe = new JFrame();
thepanel = new JGraphics();
thepanel.setLayout(null);
thepanel.setPreferredSize(new Dimension(600, 600));
theframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theframe.setContentPane(thepanel);
theframe.pack();
theframe.setVisible(true);
thetimer = new Timer(1000/60, this);
thetimer.start();
// SuperSocketMaster server mode
ssm = new SuperSocketMaster(6112, this);
ssm.connect();
}
// main method
public static void main(String[] args){
ballcontrol bc = new ballcontrol();
}
// Should have created this in a separate file... oh well
private class JGraphics extends JPanel{
// Ball variables
int intbbx = 50;
int intbby = 50;
int intrbx = 50;
int intrby = 500;
int intgbx = 500;
int intgby = 50;
int intcbx = 500;
int intcby = 500;
// Repainting screen based on network input
// and how it changes the ball variables
public void paintComponent(Graphics g){
g.clearRect(0, 0, 600, 600);
g.setColor(Color.BLUE);
g.fillOval(intbbx, intbby, 50, 50);
g.setColor(Color.RED);
g.fillOval(intrbx, intrby, 50, 50);
g.setColor(Color.GREEN);
g.fillOval(intgbx, intgby, 50, 50);
g.setColor(Color.CYAN);
g.fillOval(intcbx, intcby, 50, 50);
}
public JGraphics(){
super();
}
}
}