290 likes | 297 Views
Explore Java socket programming, GUI development using AWT and Swing, and get started with Git version control system for Java projects.
E N D
Tutorial Haifeng Gu
Topics • Java IDE • Socket Programming • GUI • Version Control System -- Git
Java IDE • Java Tutorial 1: Hello Java! Getting Started With Eclipse! https://www.youtube.com/watch?v=mMu-JlBrYXo • Make sure the JDK (Java SDK) has been installed
Socket Programming Java socket programming provides facility to share data between different computing devices. A socket is an endpoint between two way communication. (Server and Client) Overview
Socket Programming Socket Class Socket socket = new Socket(“127.0.0.1”, 5000) public InputStream getInputStream() public OutputStream getOutputStream() public synchronized void close() ServerSocket Class public Socket accept() public synchronized void close() Client Server
Establish a Socket Connection Communication Closing the connection Socket socket = new Socket(“127.0.0.1”, 5000) To communicate over a socket connection, streams are used to both input and output the data. The socket connection is closed explicitly once the message to server is sent. A socket connection means the two machines have information about each other’s network location (IP Address) and TCP port. public InputStream getInputStream() public OutputStream getOutputStream() public synchronized void close()
socket demo Feature1: Client keeps reading input from user and sends to the server until “Over” is typed. Server prints out what the client sends to it. Feature2: Client will write first to the server then server will receive and print the text. Then server will send the message back to the client and the client will receive and print the text.
GUI Java APIs for graphic programming: • AWT(Abstract Windowing Toolkit) • Swing • JavaFX Graphical User Interface
GUI • AWT API was introduced in JDK 1.0. • Swing API, a much more comprehensive set of graphics libraries that enhances the AWT, was introduced as part of Java Foundation Classes (JFC) after the release of JDK 1.1. JFC consists of Swing, Java2D, Accessibility, Internationalization, and Pluggable Look-and-Feel Support APIs. JFC has been integrated into core Java since JDK 1.2. • The latest JavaFX, which was integrated into JDK 8, is meant to replace Swing.
AWT java.awt package contains the core AWT graphics classes: GUI Component classes, such as Button, TextField, and Label. GUI Container classes, such as Frame and Panel Customgraphic classes, such as Graphic, Color and Font java.awt.event package supports event handling: Event classes, such as ActionEvent, MouseEvent, KeyEvent and WindowEvent Event Listener Interface, such as ActionListener, MouseListener, MouseMotionListener Event Listener Adapter classes, such as MouseAdapter, KeyAdapter, and WindowAdapter
Awt demo It has a top-level container Frame, which contains three components - a Label "Counter", a non-editable TextField to display the current count, and a "Count" Button. The TextField shall display count of 0 initially. Each time you click the button, the counter's value increases by 1.
Swing If you understood the AWT programming (in particular, container/component and event-handling), switching over to Swing is straight-forward. Compared with the AWT component classes (in package java.awt), Swing component classes (in package javax.swing) begin with a prefix "J", e.g., JButton, JTextField, JLabel, JPanel, JFrame, or JApplet.
Writing Swing Applications In summary, to write a Swing application, you have: • Use the Swing components with prefix "J" in package javax.swing, e.g., JFrame, JButton, JTextField, JLabel, etc. • A top-level container (typically JFrame) is needed. The JComponents should not be added directly onto the top-level container. They shall be added onto the content-pane of the top-level container. You can retrieve a reference to the content-pane by invoking method getContentPane() from the top-level container. • Swing applications uses AWT event-handling classes, e.g., ActionEvent/ActionListener, MouseEvent/MouseListener, etc. • Run the constructor in the Event Dispatcher Thread (instead of Main thread) for thread safety, as shown in the following program template.
Swing demo Let's convert the earlier AWT application example into Swing. The display is shown below. Note the differences in look and feel between the AWT GUI components and Swing's.
Demo http://javalin-websocket-example.herokuapp.com/
Version Control System -- Git • Git official document: (References, Videos, etc) https://git-scm.com/doc
Git basics https://git-scm.com/book/en/v1/Getting-Started-Git-Basics
Git initialize local workspace $mkdir learngit $ cd learngit $ pwd /Users/michael/learngit $ gitinit Initialized empty Git repository in /Users/michael/learngit/.git/
Add a new file and commit to Git repository $git add readme.txt [and other files] $ git commit –m “Wrote a readme file.” 1 file changed, 2 insertions(+) create mode 100644 readme.txt • Option “-m” is used to record the main info of the commit. • “1 file changed” indicatesa file changed in repository by this commit. • “2 insertions(+)” means 2 lines inserted in the changed file.
Watch Git Status $git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: readme.txt (This tells you the file has been modified and haven’t been committed.) no changes added to commit (use "git add" and/or "git commit -a")
Discard Modifications in Local Workspace $git checkout -- fileName
Discard Modifications in Stage Area • This operation also called Unstage. $git reset HEAD readme.txt Unstaged changes after reset: M readme.txt
List Git Commit Logs $gitlog commit e475afc93c209a690c39c13a46716e8fa000c366 Author: NameX <UserName@gmail.com> add distributed commit eaadf4e385e865d25c48e7cagc8395c3f7dfaef0 Author: NameX <UserName@gmail.com> Wrote a readme file. (This is the info given by $git commit –m “Wrote a readme file.”)
Git Roll Back $gitreset –hard HEAD^ HEAD is now at eaadf4eWrote a readme file. • “HEAD^” indicates the previous commit. $git reset –hard commit_id • “commit_id” can be a specific hash-code listed in git logs.
Upload to Github • Github is a remote repository where you can save your projects publicly or pribately. • Create a project on githubwebsit with your account. • Use the following command to direct the local project to the remote repository (i.e., on github) $git remote set-urlorigin git@github.com:USERNAME/REPOSITORY.git • In local project top folder, use the following command to upload $git push origin
Download from Github • In a folder, use the following command $ git clone https://github.com/YourUName/ProjName
Useful resources for Git • Gitofficial document: (References, Videos, etc) https://git-scm.com/doc • Git Tutorial https://www.youtube.com/watch?v=SWYqp7iY_Tc
Tips • Server can be deployed on Apache Tomcat locally to simulate Internet. • jQuery