Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC309 Lecture 15

CSC309 Lecture 15

Software Engineering II
More Tools
(202405)

Javier Gonzalez-Sanchez

February 14, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs CSC 309 Software Engineering II Lecture 15: More Tools

    Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227 Office Hours: By appointment
  2. jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 |

    3 6 Model (Weather) and set up! Actions, Releases … statistics
  3. jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 |

    4 § The Robot class in the Java AWT is useful for GUI test automation and self- running demos. § Robot enables users to control the mouse and keyboard devices. Robot Class in Java
  4. jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 |

    5 § JLayeredPane is a container in Java Swing that enables to overlay and manage (positions and visibility) for multiple components in a single container. § https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html JLayered Pane Class in Java
  5. jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 |

    6 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class AnimatedPanel extends JPanel implements ActionListener{ private Timer timer; private int x1, y1; public AnimatedPanel() { super(); timer = new Timer(50, this); timer.start(); } public void actionPerformed(ActionEvent e) { moveCircle(); repaint(); } Timer
  6. jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 |

    10 import java.net.*; public class Server { public static void main(String[] args) { final int PORT = 12345; try { ServerSocket serverSocket = new ServerSocket(PORT)); System.out.println("Server started. Waiting for a client..."); Socket clientSocket = serverSocket.accept(); System.out.println("Client connected: " + clientSocket); BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Client: " + inputLine); out.println("Server received: " + inputLine); } } catch (IOException e) { e.printStackTrace(); } } } Server
  7. jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 |

    11 public class Client { public static void main(String[] args) { final String SERVER_ADDRESS = "localhost"; final int PORT = 12345; try { Socket socket = new Socket(SERVER_ADDRESS, PORT); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Connected to server."); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("Server: " + in.readLine()); } } catch (UnknownHostException e) { System.err.println("Unknown host: " + SERVER_ADDRESS); System.exit(1); } catch (IOException e) { System.err.println("Error connecting to server"); e.printStackTrace(); System.exit(1); } } } Client
  8. jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 |

    13 import java.net.*; public class Server implements Runnable{ public void run() { final int PORT = 12345; try { ServerSocket serverSocket = new ServerSocket(PORT)); System.out.println("Server started. Waiting for a client..."); Socket clientSocket = serverSocket.accept(); System.out.println("Client connected: " + clientSocket); BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Client: " + inputLine); out.println("Server received: " + inputLine); } } catch (IOException e) { e.printStackTrace(); } } } Server Runnable
  9. jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 |

    14 public class Client implements Runnable{ public void run() { final String SERVER_ADDRESS = "localhost"; final int PORT = 12345; try { Socket socket = new Socket(SERVER_ADDRESS, PORT); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Connected to server."); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("Server: " + in.readLine()); } } catch (UnknownHostException e) { System.err.println("Unknown host: " + SERVER_ADDRESS); System.exit(1); } catch (IOException e) { System.err.println("Error connecting to server"); e.printStackTrace(); System.exit(1); } } } Runnable Client
  10. jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 |

    15 public static void main(String[] args) { Client server = new Server(port); Thread t1= new Thread(server); t1.start (); Client client = new Client(address, port); Thread t2= new Thread(client); t2.start (); } Multi-Threading
  11. jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 |

    17 Pong :: Timer, Socket, Listeners, UI
  12. jgs

  13. jgs CSC 309 Software Engineering II Lab 14: Work in

    your Sprint Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227 Office Hours: By appointment
  14. jgs CSC 309 Software Engineering II Javier Gonzalez-Sanchez, Ph.D. [email protected]

    Winter 2023 Copyright. These slides can only be used as study material for the class CSC308 at Cal Poly. They cannot be distributed or used for another purpose.