1 / 29

Tutorial

Explore Java socket programming, GUI development using AWT and Swing, and get started with Git version control system for Java projects.

sherylb
Download Presentation

Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial Haifeng Gu

  2. Topics • Java IDE • Socket Programming • GUI • Version Control System -- Git

  3. 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

  4. 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

  5. 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

  6. 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()

  7. 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.

  8. GUI Java APIs for graphic programming: • AWT(Abstract Windowing Toolkit) • Swing • JavaFX Graphical User Interface

  9. 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.

  10. 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

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. Demo http://javalin-websocket-example.herokuapp.com/

  16. Version Control System -- Git • Git official document: (References, Videos, etc) https://git-scm.com/doc

  17. Git basics https://git-scm.com/book/en/v1/Getting-Started-Git-Basics

  18. Git initialize local workspace $mkdir learngit $ cd learngit $ pwd /Users/michael/learngit $ gitinit Initialized empty Git repository in /Users/michael/learngit/.git/

  19. 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.

  20. 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")

  21. Discard Modifications in Local Workspace $git checkout -- fileName

  22. Discard Modifications in Stage Area • This operation also called Unstage. $git reset HEAD readme.txt Unstaged changes after reset: M readme.txt

  23. 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.”)

  24. 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.

  25. 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

  26. Download from Github • In a folder, use the following command $ git clone https://github.com/YourUName/ProjName

  27. Useful resources for Git • Gitofficial document: (References, Videos, etc) https://git-scm.com/doc • Git Tutorial https://www.youtube.com/watch?v=SWYqp7iY_Tc

  28. Tips • Server can be deployed on Apache Tomcat locally to simulate Internet. • jQuery

  29. Thank you

More Related