Created
May 31, 2014 19:42
-
-
Save jwtea/eed7822163cfbd8bd6d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package wiflyServer; | |
import javax.swing.JFrame; | |
import javax.swing.JButton; | |
import java.awt.BorderLayout; | |
import java.awt.event.ActionListener; | |
import java.awt.event.ActionEvent; | |
import java.io.IOException; | |
import java.net.Socket; | |
public class gui { | |
public Socket writerSoc; | |
public gui(Socket socket) //Constructor | |
{ | |
writerSoc = socket; | |
JFrame guiFrame = new JFrame(); //Create the JDrame Object | |
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
guiFrame.setTitle("Light"); | |
guiFrame.setSize(200,100); | |
guiFrame.setLocationRelativeTo(null); //POsition the Gui in the centre of the screen | |
JButton on = new JButton("on"); //Create the buttons | |
JButton off = new JButton("off"); | |
on.addActionListener(new ActionListener() //Add action listeners to the button | |
{ | |
public void actionPerformed(ActionEvent event) | |
{ | |
sendChar('1'); //Call the send char passing a 1 char | |
} | |
}); | |
off.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent event) | |
{ | |
sendChar('0'); | |
} | |
}); | |
guiFrame.add(on,BorderLayout.EAST); //position the buttons | |
guiFrame.add(off,BorderLayout.WEST); | |
guiFrame.setVisible(true); | |
} | |
public void sendChar(char x){ //Send char method handles the sending of the char to the stream | |
try { | |
System.out.println("Sending:"+x); | |
writerSoc.getOutputStream().write(x); //Send the char down the stream | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment