Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

jgs Previously

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 7 POM.XML

Slide 8

Slide 8 text

jgs Sockets CSC 357

Slide 9

Slide 9 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 9 TCP Socket

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

jgs Mult-Threading CSC 357

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

jgs Connecting the Dots Pong

Slide 17

Slide 17 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 17 Pong :: Timer, Socket, Listeners, UI

Slide 18

Slide 18 text

jgs To Be Continued…

Slide 19

Slide 19 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 19 Questions

Slide 20

Slide 20 text

jgs

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

jgs Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 22 Let’s Work

Slide 23

Slide 23 text

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.