From b4dbd2cfa724476162fa6d35941a5d7cdc9c9524 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 18 Feb 2019 08:10:10 -0500 Subject: Adds all assignments not previously in a git repo --- CS2452/GUI_network/Gui.java | 108 +++++++++++++++++++++++++++++++++++ CS2452/GUI_network/MGui.java | 92 +++++++++++++++++++++++++++++ CS2452/GUI_network/SocketReader.java | 49 ++++++++++++++++ CS2452/GUI_network/SocketWriter.java | 56 ++++++++++++++++++ 4 files changed, 305 insertions(+) create mode 100644 CS2452/GUI_network/Gui.java create mode 100644 CS2452/GUI_network/MGui.java create mode 100644 CS2452/GUI_network/SocketReader.java create mode 100644 CS2452/GUI_network/SocketWriter.java (limited to 'CS2452/GUI_network') diff --git a/CS2452/GUI_network/Gui.java b/CS2452/GUI_network/Gui.java new file mode 100644 index 0000000..2187030 --- /dev/null +++ b/CS2452/GUI_network/Gui.java @@ -0,0 +1,108 @@ +package assign6; + +import java.awt.*; +import java.awt.event.*; +import java.awt.image.*; +import java.io.*; +import java.util.concurrent.Semaphore; + +import javax.imageio.ImageIO; +import javax.swing.*; + +public class Gui extends JFrame implements ActionListener, MouseListener +{ + JPanel panel; + JLabel label; + BufferedImage image; + Color color; + Semaphore write; + boolean sc; + + + public Gui(String filename, Color color, Semaphore write, boolean sc) + { + this.color = color; + this.sc = sc; + + panel = new JPanel(new GridLayout(1,1)); + panel.setBackground(color); + try + { + image = ImageIO.read(new File(filename)); + } catch (IOException e) + { + e.printStackTrace(); + } + label = new JLabel(new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(500, 500, Image.SCALE_DEFAULT))); + + panel.add(label); + panel.addMouseListener(this); + this.add(panel); + + this.write = write; + + } + + public void actionPerformed(ActionEvent e) + { + + } + + public void mouseClicked(MouseEvent e) + { + write.release(); + changeImage(); + } + + + public void mouseEntered(MouseEvent e) + { + + } + + public void mouseExited(MouseEvent e) + { + + } + + public void mousePressed(MouseEvent e) + { + + } + + public void mouseReleased(MouseEvent e) + { + + } + + public void changeImage() + { + + String filename; + Color color; + if(sc) + { + filename = "img2.jpg"; + color = Color.GREEN; + } + else + { + filename = "img1.jpg"; + color = Color.BLUE; + } + + try + { + image = ImageIO.read(new File(filename)); + } catch (IOException e) + { + e.printStackTrace(); + } + label.setIcon(new ImageIcon(new ImageIcon(image).getImage().getScaledInstance(500, 500, Image.SCALE_DEFAULT))); + + panel.setBackground(color); + panel.repaint(); + + sc = !sc; + } +} \ No newline at end of file diff --git a/CS2452/GUI_network/MGui.java b/CS2452/GUI_network/MGui.java new file mode 100644 index 0000000..a0879c6 --- /dev/null +++ b/CS2452/GUI_network/MGui.java @@ -0,0 +1,92 @@ +package assign6; + +import java.net.*; +import java.util.concurrent.Semaphore; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +import java.io.*; + +public class MGui +{ + + public static void main(String[] args) + { + Gui g = null; + Semaphore write = new Semaphore(1); + try + { + write.acquire(); + } catch (InterruptedException e1) + { + e1.printStackTrace(); + } + ServerSocket ss; + Socket s = null; + + if (args[0].compareTo("server") == 0) + { + try + { + ss = new ServerSocket(Integer.parseInt(args[1])); + s = ss.accept(); + } catch (NumberFormatException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + g = new Gui("img1.jpg",Color.BLUE, write,true); + g.setSize(1000, 1000); + g.setLocation(32, 32); + g.setVisible(true); + g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + SocketWriter sw = new SocketWriter(s,write); + + + } + else if (args[0].compareTo("client") == 0) + { + + InetAddress address = null; + int port = 0; + + try + { + address = InetAddress.getByName(args[1]); + port = Integer.parseInt(args[2]); + + } catch (UnknownHostException e) + { + e.printStackTrace(); + } + + try + { + s = new Socket(address, port); + } catch (IOException e) + { + e.printStackTrace(); + } + + g = new Gui("img2.jpg",Color.GREEN,write,false); + g.setSize(1000, 1000); + g.setLocation(500, 500); + g.setVisible(true); + g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + SocketWriter sw = new SocketWriter(s,write); + + } + + SocketReader sr = new SocketReader(s,g); + + } + +} diff --git a/CS2452/GUI_network/SocketReader.java b/CS2452/GUI_network/SocketReader.java new file mode 100644 index 0000000..dacdaf1 --- /dev/null +++ b/CS2452/GUI_network/SocketReader.java @@ -0,0 +1,49 @@ +package assign6; + +import java.net.*; + +import javax.swing.JFrame; + +import java.io.*; + +public class SocketReader extends Thread +{ + BufferedReader br; + Gui g; + Thread t; + + public SocketReader(Socket s, Gui g) + { + try + { + br = new BufferedReader(new InputStreamReader(s.getInputStream())); + } catch (IOException e) + { + e.printStackTrace(); + } + + this.g = g; + + t = new Thread(this); + t.start(); + } + + public void run() + { + String str = null; + while (true) + { + try + { + str = br.readLine(); + } catch (IOException e) + { + e.printStackTrace(); + } + if (str != null) + g.changeImage(); + } + + } + +} \ No newline at end of file diff --git a/CS2452/GUI_network/SocketWriter.java b/CS2452/GUI_network/SocketWriter.java new file mode 100644 index 0000000..1d882ab --- /dev/null +++ b/CS2452/GUI_network/SocketWriter.java @@ -0,0 +1,56 @@ +package assign6; + +import java.io.*; +import java.net.*; +import java.util.concurrent.Semaphore; + +public class SocketWriter extends Thread +{ + Thread t; + BufferedWriter bw; + Semaphore write; + boolean sc; + + public SocketWriter(Socket s, Semaphore write) + { + try{ + this.bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); + } + catch(IOException e) + { + e.printStackTrace(); + } + + this.write = write; + + + + t = new Thread(this); + t.start(); + } + + public void run() + { + + while(true) + { + try + { + write.acquire(); + } catch (InterruptedException e1) + { + e1.printStackTrace(); + } + try + { + bw.newLine(); + bw.flush(); + } catch (IOException e) + { + e.printStackTrace(); + } + } + + } + +} -- cgit v1.1