Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Homework 7: Chat
- Paul MacLean (MAC7537@calu.edu), Michael Gorse (GOR9632@calu.edu), Anthony Carrola (CAR3766@calu.edu)
- Group 8 (2^3)
- CET 350 - Technical Computer using Java
- */
- import java.io.*;
- import java.awt.*;
- import java.util.*;
- import java.util.concurrent.TimeUnit;
- import javax.imageio.ImageIO;
- import java.lang.*; //math stuff should be included in lang
- import java.awt.List;
- import java.awt.event.*;
- import java.awt.geom.Point2D;
- import java.net.Socket;
- import java.net.ServerSocket;
- import java.net.InetSocketAddress;
- import java.net.SocketTimeoutException;
- public class Chat implements Runnable, ComponentListener, WindowListener
- {
- private Frame chat = new Frame();
- //objects used on the frame
- private TextArea Dialog;
- private TextField messageBox;
- private Button sendButton;
- private Label hostLabel;
- private TextField hostBox;
- private Button hostChangeButton;
- private Button connectButton;
- private Label portLabel;
- private TextField portBox;
- private Button portChangeButton;
- private Button startButton;
- private TextArea output;
- //objects used for network communication
- private Socket client;
- private Socket server;
- private BufferedReader br;
- private InputStreamReader is;
- private PrintWriter pw;
- private Thread SteppingThread;
- private Menu Colors;
- private MenuBar MB;
- Chat()
- {
- initComponents();
- }
- //-------------------------------------------
- private void initComponents()
- {
- Dialog = new TextArea(1,1);
- messageBox = new TextField("");
- //MENU STUFFFF-------------------------------------
- MB = new MenuBar(); //main menu bar
- Colors = new Menu("Colors");
- MB.add(Colors);
- chat.setMenuBar(MB);
- //--------------------------------------------------------
- chat.setBounds(50, 50 , 800, 500); //OFFSET 32Y
- chat.setMinimumSize(new Dimension(700, 400));
- chat.setResizable(true);
- positionComponents();
- chat.addComponentListener(this);
- chat.addWindowListener(this);
- chat.setVisible(true);
- //Dialog.setVisible(true);
- start();
- run();
- }
- private void positionComponents() {
- //Gridbag stuff
- GridBagConstraints GBConstr = new GridBagConstraints(); // creates new grid bag constraints
- GridBagLayout GBLayout = new GridBagLayout(); // creates new grid bag layout
- int[] colWidth = {1,1,1,1,1,1,1,1,1,1,1,1,1}; //16
- int[] rowWidth = {1,1,1,1,1,1};
- double colWeight[] = {1,1,1,1,1,1,1,1,1,1,1,1,1}; //weight to cols
- double rowWeight[] = {1,1,1,1,1,1}; //weight to rows
- GBLayout.columnWeights = colWeight;
- GBLayout.rowWeights = rowWeight;
- GBLayout.columnWidths = colWidth;
- GBLayout.rowHeights = rowWidth;
- //Dialog TextArea
- GBConstr.weightx = 0;
- GBConstr.weighty = 0;
- GBConstr.gridwidth = 16;
- GBConstr.gridheight = 1;
- GBConstr.gridx = 0;
- GBConstr.gridy = 0;
- GBConstr.fill = GridBagConstraints.BOTH;
- GBLayout.setConstraints(Dialog, GBConstr);
- Dialog.setVisible(true);
- chat.add(Dialog);
- Dialog.disable();
- //message Box TextField
- GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridwidth = 2;
- GBConstr.gridheight = 1;
- GBConstr.gridx = 8;
- GBConstr.gridy = 4;
- GBConstr.anchor = GridBagConstraints.CENTER;
- GBConstr.fill = GridBagConstraints.BOTH;
- GBLayout.setConstraints(messageBox, GBConstr);
- messageBox.setVisible(true);
- chat.add(messageBox);
- /*
- //Restart Button
- /*GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridheight = 1;
- GBConstr.gridwidth = 1;
- GBConstr.gridx = 5;
- GBConstr.gridy = 4;
- GBConstr.fill = GridBagConstraints.BOTH;
- GBLayout.setConstraints(RestartButton, GBConstr);
- //AngleBar
- GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridwidth = 2;
- GBConstr.gridheight = 1;
- GBConstr.gridx = 10;
- GBConstr.gridy = 1;
- GBConstr.anchor = GridBagConstraints.CENTER;
- GBConstr.fill = GridBagConstraints.BOTH;
- GBLayout.setConstraints(AngleBar, GBConstr);
- //AngleLabel
- GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridwidth = 1;
- GBConstr.gridheight = 1;
- GBConstr.gridx = 8;
- GBConstr.gridy = 1;
- GBConstr.anchor = GridBagConstraints.CENTER;
- GBConstr.fill = GridBagConstraints.BOTH;
- GBLayout.setConstraints(AngleLabel, GBConstr);
- // Angle of Cannon Label
- GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridwidth = 1;
- GBConstr.gridheight = 1;
- GBConstr.gridx = 7;
- GBConstr.gridy = 1;
- GBConstr.anchor = GridBagConstraints.CENTER;
- GBConstr.fill = GridBagConstraints.BOTH;
- GBLayout.setConstraints(AngleofCannon, GBConstr);
- // Velocity of Cannon Label
- GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridwidth = 1;
- GBConstr.gridheight = 1;
- GBConstr.gridx = 7;
- GBConstr.gridy = 4;
- GBConstr.anchor = GridBagConstraints.CENTER;
- GBConstr.fill = GridBagConstraints.BOTH;
- GBLayout.setConstraints(VelocityNumberLabel, GBConstr);
- //Cannon Score Label (i.e. users)
- GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridwidth = 1;
- GBConstr.gridheight = 1;
- GBConstr.gridx = 1;
- GBConstr.gridy = 1;
- GBConstr.anchor = GridBagConstraints.CENTER;
- GBConstr.fill = GridBagConstraints.BOTH;
- GBLayout.setConstraints(CannScoreLabel, GBConstr);
- //BallScore Label
- GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridwidth = 1;
- GBConstr.gridheight = 1;
- GBConstr.gridx = 3;
- GBConstr.gridy = 1;
- GBConstr.anchor = GridBagConstraints.CENTER;
- GBConstr.fill = GridBagConstraints.BOTH;
- GBLayout.setConstraints(BallScoreLabel, GBConstr);
- //Cannon Score
- GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridwidth = 1;
- GBConstr.gridheight = 1;
- GBConstr.gridx = 1;
- GBConstr.gridy = 4;
- GBConstr.anchor = GridBagConstraints.CENTER;
- GBConstr.fill = GridBagConstraints.CENTER;
- GBLayout.setConstraints(CannScoreBox, GBConstr);
- //Ball Score
- GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridwidth = 1;
- GBConstr.gridheight = 1;
- GBConstr.gridx = 3;
- GBConstr.gridy = 4;
- GBConstr.anchor = GridBagConstraints.CENTER;
- GBConstr.fill = GridBagConstraints.CENTER;
- GBLayout.setConstraints(BallScoreBox, GBConstr);
- //Message box
- GBConstr.weightx = 1;
- GBConstr.weighty = 1;
- GBConstr.gridwidth = 4;
- GBConstr.gridheight = 2;
- GBConstr.gridx = 1;
- GBConstr.gridy = 6;
- GBConstr.anchor = GridBagConstraints.CENTER;
- GBConstr.fill = GridBagConstraints.BOTH;
- GBLayout.setConstraints(MsgBox, GBConstr);
- VelocBar.setValues(SCROLLER_DEFAULT, SCROLLER_THUMBOFFSET, SCROLLER_MIN, SCROLLER_MAX);
- VelocBar.setBlockIncrement(SCROLLER_BLOCKVAL);
- VelocBar.setUnitIncrement(2);
- VelocBar.setBackground(Color.ORANGE);
- VelocBar.addAdjustmentListener(this);
- VelocBar.setVisible(true);
- AngleBar.setValues(SCROLLER_DEFAULT, SCROLLER_THUMBOFFSET, SCROLLER_MIN, SCROLLER_MAX);
- AngleBar.setBlockIncrement(SCROLLER_BLOCKVAL);
- AngleBar.setUnitIncrement(2);
- AngleBar.setBackground(Color.ORANGE);
- AngleBar.addAdjustmentListener(this);
- AngleBar.setVisible(true);
- VelocLabel.setAlignment(Label.CENTER);
- VelocLabel.setVisible(true);
- //RestartButton.addActionListener(this);
- //RestartButton.setVisible(true);
- AngleLabel.setAlignment(Label.CENTER);
- AngleLabel.setVisible(true);
- AngleofCannon.setVisible(true);
- AngleofCannon.setEnabled(false);
- VelocityNumberLabel.setVisible(true);
- VelocityNumberLabel.setEnabled(false);
- CannScoreLabel.setVisible(true);
- CannScoreLabel.setForeground(Color.BLUE);
- BallScoreLabel.setVisible(true);
- BallScoreLabel.setForeground(Color.RED);
- CannScoreBox.setVisible(true);
- CannScoreBox.setEnabled(false);
- BallScoreBox.setVisible(true);
- BallScoreBox.setEnabled(false);
- MsgBox.setVisible(true);
- MsgBox.setForeground(Color.MAGENTA);
- */
- chat.setLayout(GBLayout);
- //control.add(messageBox);
- }
- public static void main(String[] args)
- {
- new Chat();
- }
- public void start()
- {
- if(SteppingThread == null)
- {
- SteppingThread = new Thread(this);
- SteppingThread.start();
- }
- }
- public void run()
- {
- SteppingThread.setPriority(Thread.MAX_PRIORITY);
- while(true) {
- try {
- Thread.sleep(1);
- } catch(InterruptedException e){}
- //canvas.processTimeStep();
- }
- }
- public void stop()
- {
- SteppingThread.setPriority(Thread.MIN_PRIORITY);
- //chat.removeWindowListener(this);
- System.exit(0);
- //chat.dispose();
- }
- public void windowClosed(WindowEvent e) {
- stop();
- }
- public void windowClosing(WindowEvent e) {
- stop();
- }
- public void windowActivated(WindowEvent e) {}
- public void windowDeactivated(WindowEvent e) {}
- public void windowDeiconified(WindowEvent e) {}
- public void windowIconified(WindowEvent e) {}
- public void windowOpened(WindowEvent e) {}
- public void actionPerformed(ActionEvent e)
- {
- }
- public void adjustmentValueChanged(AdjustmentEvent e)
- {
- }
- public void itemStateChanged (ItemEvent e)
- {
- }
- @Override
- public void componentHidden(ComponentEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void componentMoved(ComponentEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void componentResized(ComponentEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void componentShown(ComponentEvent e) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement