Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.85 KB | None | 0 0
  1. /*
  2. * Homework 7: Chat
  3. Paul MacLean (MAC7537@calu.edu), Michael Gorse (GOR9632@calu.edu), Anthony Carrola (CAR3766@calu.edu)
  4. Group 8 (2^3)
  5. CET 350 - Technical Computer using Java
  6. */
  7.  
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.util.*;
  11. import java.util.concurrent.TimeUnit;
  12. import javax.imageio.ImageIO;
  13.  
  14. import java.lang.*; //math stuff should be included in lang
  15. import java.awt.List;
  16. import java.awt.event.*;
  17. import java.awt.geom.Point2D;
  18.  
  19. import java.net.Socket;
  20. import java.net.ServerSocket;
  21. import java.net.InetSocketAddress;
  22. import java.net.SocketTimeoutException;
  23.  
  24.  
  25. public class Chat implements Runnable, ComponentListener, WindowListener
  26. {
  27.  
  28. private Frame chat = new Frame();
  29.  
  30. //objects used on the frame
  31. private TextArea Dialog;
  32.  
  33. private TextField messageBox;
  34. private Button sendButton;
  35.  
  36. private Label hostLabel;
  37. private TextField hostBox;
  38. private Button hostChangeButton;
  39. private Button connectButton;
  40.  
  41. private Label portLabel;
  42. private TextField portBox;
  43. private Button portChangeButton;
  44. private Button startButton;
  45.  
  46. private TextArea output;
  47.  
  48. //objects used for network communication
  49. private Socket client;
  50. private Socket server;
  51.  
  52. private BufferedReader br;
  53. private InputStreamReader is;
  54. private PrintWriter pw;
  55.  
  56. private Thread SteppingThread;
  57.  
  58. private Menu Colors;
  59. private MenuBar MB;
  60.  
  61.  
  62. Chat()
  63. {
  64. initComponents();
  65. }
  66.  
  67. //-------------------------------------------
  68.  
  69. private void initComponents()
  70. {
  71.  
  72. Dialog = new TextArea(1,1);
  73.  
  74.  
  75. messageBox = new TextField("");
  76.  
  77. //MENU STUFFFF-------------------------------------
  78. MB = new MenuBar(); //main menu bar
  79.  
  80. Colors = new Menu("Colors");
  81.  
  82.  
  83.  
  84. MB.add(Colors);
  85. chat.setMenuBar(MB);
  86.  
  87.  
  88. //--------------------------------------------------------
  89.  
  90. chat.setBounds(50, 50 , 800, 500); //OFFSET 32Y
  91. chat.setMinimumSize(new Dimension(700, 400));
  92. chat.setResizable(true);
  93.  
  94.  
  95. positionComponents();
  96.  
  97.  
  98. chat.addComponentListener(this);
  99. chat.addWindowListener(this);
  100.  
  101. chat.setVisible(true);
  102.  
  103. //Dialog.setVisible(true);
  104.  
  105.  
  106. start();
  107. run();
  108.  
  109.  
  110. }
  111.  
  112. private void positionComponents() {
  113.  
  114.  
  115. //Gridbag stuff
  116. GridBagConstraints GBConstr = new GridBagConstraints(); // creates new grid bag constraints
  117. GridBagLayout GBLayout = new GridBagLayout(); // creates new grid bag layout
  118.  
  119. int[] colWidth = {1,1,1,1,1,1,1,1,1,1,1,1,1}; //16
  120. int[] rowWidth = {1,1,1,1,1,1};
  121.  
  122. double colWeight[] = {1,1,1,1,1,1,1,1,1,1,1,1,1}; //weight to cols
  123. double rowWeight[] = {1,1,1,1,1,1}; //weight to rows
  124.  
  125. GBLayout.columnWeights = colWeight;
  126. GBLayout.rowWeights = rowWeight;
  127. GBLayout.columnWidths = colWidth;
  128. GBLayout.rowHeights = rowWidth;
  129.  
  130. //Dialog TextArea
  131. GBConstr.weightx = 0;
  132. GBConstr.weighty = 0;
  133. GBConstr.gridwidth = 16;
  134. GBConstr.gridheight = 1;
  135. GBConstr.gridx = 0;
  136. GBConstr.gridy = 0;
  137. GBConstr.fill = GridBagConstraints.BOTH;
  138. GBLayout.setConstraints(Dialog, GBConstr);
  139.  
  140. Dialog.setVisible(true);
  141. chat.add(Dialog);
  142. Dialog.disable();
  143.  
  144.  
  145. //message Box TextField
  146.  
  147. GBConstr.weightx = 1;
  148. GBConstr.weighty = 1;
  149. GBConstr.gridwidth = 2;
  150. GBConstr.gridheight = 1;
  151. GBConstr.gridx = 8;
  152. GBConstr.gridy = 4;
  153. GBConstr.anchor = GridBagConstraints.CENTER;
  154. GBConstr.fill = GridBagConstraints.BOTH;
  155. GBLayout.setConstraints(messageBox, GBConstr);
  156.  
  157. messageBox.setVisible(true);
  158. chat.add(messageBox);
  159.  
  160. /*
  161. //Restart Button
  162. /*GBConstr.weightx = 1;
  163. GBConstr.weighty = 1;
  164. GBConstr.gridheight = 1;
  165. GBConstr.gridwidth = 1;
  166. GBConstr.gridx = 5;
  167. GBConstr.gridy = 4;
  168. GBConstr.fill = GridBagConstraints.BOTH;
  169. GBLayout.setConstraints(RestartButton, GBConstr);
  170.  
  171. //AngleBar
  172. GBConstr.weightx = 1;
  173. GBConstr.weighty = 1;
  174. GBConstr.gridwidth = 2;
  175. GBConstr.gridheight = 1;
  176. GBConstr.gridx = 10;
  177. GBConstr.gridy = 1;
  178. GBConstr.anchor = GridBagConstraints.CENTER;
  179. GBConstr.fill = GridBagConstraints.BOTH;
  180. GBLayout.setConstraints(AngleBar, GBConstr);
  181.  
  182. //AngleLabel
  183. GBConstr.weightx = 1;
  184. GBConstr.weighty = 1;
  185. GBConstr.gridwidth = 1;
  186. GBConstr.gridheight = 1;
  187. GBConstr.gridx = 8;
  188. GBConstr.gridy = 1;
  189. GBConstr.anchor = GridBagConstraints.CENTER;
  190. GBConstr.fill = GridBagConstraints.BOTH;
  191. GBLayout.setConstraints(AngleLabel, GBConstr);
  192.  
  193. // Angle of Cannon Label
  194. GBConstr.weightx = 1;
  195. GBConstr.weighty = 1;
  196. GBConstr.gridwidth = 1;
  197. GBConstr.gridheight = 1;
  198. GBConstr.gridx = 7;
  199. GBConstr.gridy = 1;
  200. GBConstr.anchor = GridBagConstraints.CENTER;
  201. GBConstr.fill = GridBagConstraints.BOTH;
  202. GBLayout.setConstraints(AngleofCannon, GBConstr);
  203.  
  204. // Velocity of Cannon Label
  205. GBConstr.weightx = 1;
  206. GBConstr.weighty = 1;
  207. GBConstr.gridwidth = 1;
  208. GBConstr.gridheight = 1;
  209. GBConstr.gridx = 7;
  210. GBConstr.gridy = 4;
  211. GBConstr.anchor = GridBagConstraints.CENTER;
  212. GBConstr.fill = GridBagConstraints.BOTH;
  213. GBLayout.setConstraints(VelocityNumberLabel, GBConstr);
  214.  
  215.  
  216. //Cannon Score Label (i.e. users)
  217. GBConstr.weightx = 1;
  218. GBConstr.weighty = 1;
  219. GBConstr.gridwidth = 1;
  220. GBConstr.gridheight = 1;
  221. GBConstr.gridx = 1;
  222. GBConstr.gridy = 1;
  223. GBConstr.anchor = GridBagConstraints.CENTER;
  224. GBConstr.fill = GridBagConstraints.BOTH;
  225. GBLayout.setConstraints(CannScoreLabel, GBConstr);
  226.  
  227.  
  228. //BallScore Label
  229. GBConstr.weightx = 1;
  230. GBConstr.weighty = 1;
  231. GBConstr.gridwidth = 1;
  232. GBConstr.gridheight = 1;
  233. GBConstr.gridx = 3;
  234. GBConstr.gridy = 1;
  235. GBConstr.anchor = GridBagConstraints.CENTER;
  236. GBConstr.fill = GridBagConstraints.BOTH;
  237. GBLayout.setConstraints(BallScoreLabel, GBConstr);
  238.  
  239. //Cannon Score
  240. GBConstr.weightx = 1;
  241. GBConstr.weighty = 1;
  242. GBConstr.gridwidth = 1;
  243. GBConstr.gridheight = 1;
  244. GBConstr.gridx = 1;
  245. GBConstr.gridy = 4;
  246. GBConstr.anchor = GridBagConstraints.CENTER;
  247. GBConstr.fill = GridBagConstraints.CENTER;
  248. GBLayout.setConstraints(CannScoreBox, GBConstr);
  249.  
  250.  
  251. //Ball Score
  252. GBConstr.weightx = 1;
  253. GBConstr.weighty = 1;
  254. GBConstr.gridwidth = 1;
  255. GBConstr.gridheight = 1;
  256. GBConstr.gridx = 3;
  257. GBConstr.gridy = 4;
  258. GBConstr.anchor = GridBagConstraints.CENTER;
  259. GBConstr.fill = GridBagConstraints.CENTER;
  260. GBLayout.setConstraints(BallScoreBox, GBConstr);
  261.  
  262.  
  263. //Message box
  264. GBConstr.weightx = 1;
  265. GBConstr.weighty = 1;
  266. GBConstr.gridwidth = 4;
  267. GBConstr.gridheight = 2;
  268. GBConstr.gridx = 1;
  269. GBConstr.gridy = 6;
  270. GBConstr.anchor = GridBagConstraints.CENTER;
  271. GBConstr.fill = GridBagConstraints.BOTH;
  272. GBLayout.setConstraints(MsgBox, GBConstr);
  273.  
  274.  
  275.  
  276. VelocBar.setValues(SCROLLER_DEFAULT, SCROLLER_THUMBOFFSET, SCROLLER_MIN, SCROLLER_MAX);
  277. VelocBar.setBlockIncrement(SCROLLER_BLOCKVAL);
  278. VelocBar.setUnitIncrement(2);
  279. VelocBar.setBackground(Color.ORANGE);
  280. VelocBar.addAdjustmentListener(this);
  281. VelocBar.setVisible(true);
  282.  
  283. AngleBar.setValues(SCROLLER_DEFAULT, SCROLLER_THUMBOFFSET, SCROLLER_MIN, SCROLLER_MAX);
  284. AngleBar.setBlockIncrement(SCROLLER_BLOCKVAL);
  285. AngleBar.setUnitIncrement(2);
  286. AngleBar.setBackground(Color.ORANGE);
  287. AngleBar.addAdjustmentListener(this);
  288. AngleBar.setVisible(true);
  289.  
  290. VelocLabel.setAlignment(Label.CENTER);
  291. VelocLabel.setVisible(true);
  292.  
  293.  
  294. //RestartButton.addActionListener(this);
  295. //RestartButton.setVisible(true);
  296.  
  297. AngleLabel.setAlignment(Label.CENTER);
  298. AngleLabel.setVisible(true);
  299.  
  300. AngleofCannon.setVisible(true);
  301. AngleofCannon.setEnabled(false);
  302.  
  303. VelocityNumberLabel.setVisible(true);
  304. VelocityNumberLabel.setEnabled(false);
  305.  
  306. CannScoreLabel.setVisible(true);
  307. CannScoreLabel.setForeground(Color.BLUE);
  308.  
  309. BallScoreLabel.setVisible(true);
  310. BallScoreLabel.setForeground(Color.RED);
  311.  
  312. CannScoreBox.setVisible(true);
  313. CannScoreBox.setEnabled(false);
  314.  
  315. BallScoreBox.setVisible(true);
  316. BallScoreBox.setEnabled(false);
  317.  
  318. MsgBox.setVisible(true);
  319. MsgBox.setForeground(Color.MAGENTA);
  320.  
  321. */
  322. chat.setLayout(GBLayout);
  323. //control.add(messageBox);
  324. }
  325.  
  326. public static void main(String[] args)
  327. {
  328. new Chat();
  329. }
  330. public void start()
  331. {
  332. if(SteppingThread == null)
  333. {
  334. SteppingThread = new Thread(this);
  335. SteppingThread.start();
  336. }
  337. }
  338.  
  339. public void run()
  340. {
  341. SteppingThread.setPriority(Thread.MAX_PRIORITY);
  342.  
  343. while(true) {
  344. try {
  345. Thread.sleep(1);
  346. } catch(InterruptedException e){}
  347.  
  348. //canvas.processTimeStep();
  349. }
  350. }
  351. public void stop()
  352. {
  353. SteppingThread.setPriority(Thread.MIN_PRIORITY);
  354. //chat.removeWindowListener(this);
  355. System.exit(0);
  356. //chat.dispose();
  357. }
  358. public void windowClosed(WindowEvent e) {
  359. stop();
  360. }
  361. public void windowClosing(WindowEvent e) {
  362. stop();
  363. }
  364. public void windowActivated(WindowEvent e) {}
  365. public void windowDeactivated(WindowEvent e) {}
  366. public void windowDeiconified(WindowEvent e) {}
  367. public void windowIconified(WindowEvent e) {}
  368. public void windowOpened(WindowEvent e) {}
  369.  
  370. public void actionPerformed(ActionEvent e)
  371. {
  372.  
  373. }
  374.  
  375. public void adjustmentValueChanged(AdjustmentEvent e)
  376. {
  377.  
  378. }
  379. public void itemStateChanged (ItemEvent e)
  380. {
  381.  
  382. }
  383.  
  384. @Override
  385. public void componentHidden(ComponentEvent e) {
  386. // TODO Auto-generated method stub
  387.  
  388. }
  389.  
  390. @Override
  391. public void componentMoved(ComponentEvent e) {
  392. // TODO Auto-generated method stub
  393.  
  394. }
  395.  
  396. @Override
  397. public void componentResized(ComponentEvent e) {
  398. // TODO Auto-generated method stub
  399.  
  400. }
  401.  
  402. @Override
  403. public void componentShown(ComponentEvent e) {
  404. // TODO Auto-generated method stub
  405.  
  406. }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement