import
java.net.*; import javax.swing.*; import java.awt.Color; import java.awt.event.*; import java.io.*; public class RemVehicleClientG { private static final int PORT = 8082; private String name; private Gui gui; JPanel pan; PrintWriter out; BufferedReader in; JButton bridgeg,lr,rl,end, car; Socket socket; RemVehicleClientG(String name,BufferedReader in, PrintWriter out, Socket socket){ this.name=name; this.in=in; this.out=out; gui = new Gui(); } //////// for GUI ---------------------------------- public class Gui extends JFrame{ private static final long serialVersionUID = 1L; int w=800,h=400; Gui(){ setBounds(50, 50, w, h); pan = new JPanel(); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); add(pan); pan.setLayout(null); setTitle(name); bridgeg=new JButton("bridge"); bridgeg.setBounds(w/10+50, h/3+70, w*2/3-90, 40); bridgeg.setBackground(Color.green); pan.add(bridgeg); lr=new JButton("l->r"); lr.setBounds(20, 10, 80, 40); lr.setBackground(Color.yellow); lr.addActionListener(new LtoR()); rl=new JButton("l<-r"); rl.setBounds(w-120, 10, 80, 40); rl.setBackground(Color.green); rl.addActionListener(new RtoL()); end = new JButton("end"); end.setBounds(w/2-40, 10, 80, 40); end.setBackground(Color.red); pan.add(lr); pan.add(rl); pan.add(end); end.addActionListener(new End()); setVisible(true); } } class End implements ActionListener{ public void actionPerformed(ActionEvent e) { gui.dispose(); finish(); } } class L extends Thread{ //left to right passing thread public void run() { lr.setEnabled(false); rl.setEnabled(false); end.setEnabled(false); car=new JButton("before"); car.setBounds(10, 100, 80, 40); car.setBackground(Color.green); pan.add(car); pan.revalidate(); pan.repaint(); out.println("Left"); try { System.out.println(in.readLine()); car.setText("waiting"); car.setBounds(100, 100, 80, 40); bridgeg.setBackground(Color.red); pan.revalidate(); pan.repaint(); System.out.println(in.readLine()); bridgeg.setBackground(Color.green); car.setText("on the"); car.setBounds(300, 100, 80, 40); pan.revalidate(); pan.repaint(); System.out.println(in.readLine()); car.setText("leave"); car.setBounds(600, 100, 80, 40); pan.revalidate(); pan.repaint(); Thread.sleep(3000); pan.remove(car); pan.revalidate(); pan.repaint(); } catch(Exception ex) { System.out.println("There is a problem with the server!"); } lr.setEnabled(true); rl.setEnabled(true); end.setEnabled(true); } } class R extends Thread{ public void run() { lr.setEnabled(false); rl.setEnabled(false); end.setEnabled(false); car=new JButton("before"); car.setBounds(600, 100, 80, 40); car.setBackground(Color.yellow); pan.add(car); pan.revalidate(); pan.repaint(); out.println("Right"); try { System.out.println(in.readLine()); bridgeg.setBackground(Color.red); car.setText("waiting"); car.setBounds(500, 100, 80, 40); pan.revalidate(); pan.repaint(); System.out.println(in.readLine()); bridgeg.setBackground(Color.green); car.setText("on the"); car.setBounds(300, 100, 80, 40); pan.revalidate(); pan.repaint(); System.out.println(in.readLine()); car.setText("leave"); car.setBounds(10, 100, 80, 40); pan.revalidate(); pan.repaint(); Thread.sleep(3000); pan.remove(car); pan.revalidate(); pan.repaint(); } catch(Exception ex) { System.out.println("There is a problem with the server!"); } lr.setEnabled(true); rl.setEnabled(true); end.setEnabled(true); } } class LtoR implements ActionListener{ public void actionPerformed(ActionEvent e) { (new L()).start(); } } class RtoL implements ActionListener{ public void actionPerformed(ActionEvent e) { (new R()).start(); } } public static void main(String[] args)throws IOException { String server=null; try{ server = "127.0.0.1"; InetAddress addr = InetAddress.getByName(server); System.out.println("addr = " + addr); Socket socket = new Socket(addr, PORT); System.out.println("socket = " + socket); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); BufferedReader sin = new BufferedReader( new InputStreamReader(System.in)); // Output is automatically flushed // by PrintWriter: PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())),true); System.out.print("Vehicle name please:"); String name = sin.readLine(); out.println(name); new RemVehicleClientG(name, in,out,socket); } catch(ConnectException cex) { System.out.println ("Server "+server+" is not running on port "+PORT); } } void finish() { try { out.println("END"); System.out.println("closing..."); socket.close(); } catch (Exception ex) {} } } |