java - How to make an On/off 'signal lamp' using DataInputStream? -
i'd make program can send signal through network using datainputstream have trouble algorithm.
the program simple actually. need making 2 guis. 1 color button (red, yellow, , blue button), 1 3 shapes changes color when click button.
so here client (signal buttons) code
connect.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ try{ string str = tf.gettext(); s = new socket(str,54321); out = s.getoutputstream(); dos = new dataoutputstream(out); system.out.println("connection succeeded"); }catch(exception ex){ system.out.println("connection error"); } } }); jbutton disconnect = new jbutton ("切断"); disconnect.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ try{ dos.close(); out.close(); s.close(); system.out.println("connection closed"); }catch(exception ex){ system.err.println("error -" +ex.tostring()); } } });
and here server (signal lamps). problem. sorry if code not efficient.
import javax.swing.jpanel; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.net.*; import java.io.*; public class lamp extends jframe{ serversocket ss = null; socket s = null; inputstream in = null; datainputstream dis = null; string iro; int s1,s2,s3; private container contentpane; public lamp(string title){ super(title); addwindowlistener(new windowadapter(){ public void windowclosing(windowevent we){ system.exit(0); } }); setsize(670,300); setresizable(false); setvisible(true); } public void paint(graphics g){ g.setcolor(color.blue); g.drawoval(10,50,200,200); g.setcolor(color.yellow); g.drawoval(220,50,200,200); g.setcolor(color.red); g.drawoval(430,50,200,200); try{ ss = new serversocket(54321); s = ss.accept(); in = s.getinputstream(); dis = new datainputstream(in); iro = dis.readutf(); s1 = 0; s2 = 0; s3 = 0; if(iro=="1"){ if(s1==0){ s1=1; g.setcolor(color.blue); g.filloval(10,50,200,200); system.out.println("received blue"); }else{ s1=0; g.setcolor(color.blue); g.filloval(10,50,200,200); system.out.println("received blue"); } } //its same statement yellow , red }catch(exception e){ system.out.println("error"); } } public static void main(string []args){ new lamp("lamp"); } }
the result
error error error error error
any idea?
Comments
Post a Comment