CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University March 10, 2008 / Lecture 8
Outline Course Status Course Information & Schedule Assignments & Final Project Networking IP Networking Intro Simple Sockets in Java Demo GUI Java GUI Intro Events Layouts Swing Controls Wrap-up Wrap-up The End
Course Information Instructor Andy Mroczkowski uamroczk@cs.drexel.edu Office Location: UC 147 Office Hours: Monday 6-7 & evenings by appointment Course CS 190 - Java Web: www.cs.drexel.edu/ uamroczk/cs190 Book: Thinking in Java, 4th Edition Previous edition available for free at wwww.mindview.net/books/tij/ Course materials adapted from Nadya Belov Many examples from TiJ, 4th Ed.
Schedule 1 Jan 7 Jan 14 Jan 21 Jan 28 Feb 4 Feb 11 Feb 18 Feb 25 Mar 3 Mar 10 Mar 19 Course Overview, Introduction to Java No Class No Class (University Holiday) Object Oriented Concepts, Control Statements Access Control, Intialization & Cleanup, Reuse, Interfaces, Containers, Exceptions, Polymorphism, Midterm Review Midterm, Inner-Classes Type Information, Generics, Arrays Files & I/O, Threading GUI, Special Topics (Networking, Reflection) Final Project 1 Also subject to change
Assignment 5 Questions?
Final Project Collaboration OK but you must let me know Group of 3 Max Extra credit Multiple players (Live or automated) Saving Scores (File I/O) Betting Networking... or your idea GUI?
Final Project Collaboration OK but you must let me know Group of 3 Max Extra credit Multiple players (Live or automated) Saving Scores (File I/O) Betting Networking... or your idea GUI?
Final Project Collaboration OK but you must let me know Group of 3 Max Extra credit Multiple players (Live or automated) Saving Scores (File I/O) Betting Networking... or your idea GUI?
Final Project Questions?
Final Project - Tips Random generator = new Random ( ) ; i n t index1 = generator. n e x t I n t ( decksize ) ; i n t index2 = generator. n e x t I n t ( decksize ) ;
Final Project - Tips public class BlackjackHand extends Hand
Networking (A quick introduction)
Networking (A quick introduction)
IP Networking Addressing Host Port Data transmission Send Receive
IP Networking Addressing Host Port Data transmission Send Receive
IP Networking Addressing Host Port Data transmission Send Receive
IP Networking Addressing Host Port Data transmission Send Receive
IP Networking Addressing Host Port Data transmission Send Receive
IP Hosts Host Post office www.google.com 192.168.20.10 127.0.0.1 Port Mailbox ftp - 21 http - 80 less than 1024 priveledged greater than 1024 unpriveledged
IP Hosts Host Post office www.google.com 192.168.20.10 127.0.0.1 Port Mailbox ftp - 21 http - 80 less than 1024 priveledged greater than 1024 unpriveledged
IP Hosts Host Post office www.google.com 192.168.20.10 127.0.0.1 Port Mailbox ftp - 21 http - 80 less than 1024 priveledged greater than 1024 unpriveledged
IP Hosts Host Post office www.google.com 192.168.20.10 127.0.0.1 Port Mailbox ftp - 21 http - 80 less than 1024 priveledged greater than 1024 unpriveledged
IP Hosts Host Post office www.google.com 192.168.20.10 127.0.0.1 Port Mailbox ftp - 21 http - 80 less than 1024 priveledged greater than 1024 unpriveledged
IP Hosts Host Post office www.google.com 192.168.20.10 127.0.0.1 Port Mailbox ftp - 21 http - 80 less than 1024 priveledged greater than 1024 unpriveledged
Data transmission IP Sockets Data endpoints Two types Stream (TCP) Datagram (UDP)
Data transmission IP Sockets Data endpoints Two types Stream (TCP) Datagram (UDP)
Data transmission IP Sockets Data endpoints Two types Stream (TCP) Datagram (UDP)
Data transmission IP Sockets Data endpoints Two types Stream (TCP) Datagram (UDP)
Simple Sockets in Java Socket and ServerSocket classes ServerSocket Listen for and accept connections Can have handle many incoming connections Socket Initiate client connections Read and write data streams
Simple Sockets in Java Socket and ServerSocket classes ServerSocket Listen for and accept connections Can have handle many incoming connections Socket Initiate client connections Read and write data streams
Simple Sockets in Java Socket and ServerSocket classes ServerSocket Listen for and accept connections Can have handle many incoming connections Socket Initiate client connections Read and write data streams
Simple Sockets in Java Socket and ServerSocket classes ServerSocket Listen for and accept connections Can have handle many incoming connections Socket Initiate client connections Read and write data streams
Simple Sockets in Java Socket and ServerSocket classes ServerSocket Listen for and accept connections Can have handle many incoming connections Socket Initiate client connections Read and write data streams
Create a ServerSocket ServerSocket server = new ServerSocket ( ( i n t l i s t e n _ p o r t ) ;
Listening for connections ServerSocket server = new ServerSocket ( ( i n t l i s t e n _ p o r t ) ; Socket s = server. accept ( ) ;
Create a client Socket Socket c l i e n t = new Socket ( S t r i n g server, i n t p o r t ) ;
Sending and Receiving data / / Readin InputStream i n = socket. getinputstream ( ) ; / / W r i t i n OutputStream out = socket. getoutputstream ( ) ; Now we can read/write just like they was files!
Demo
Questions?
GUI ALL examlpes from Thinking in Java, 4th ed.
Java GUI - History In the beginning, there was AWT And it was a dark, dark time. Then the geeks said, let there be Swing. And so it was. Some of the elders tell stories about "Applets", but no one has seen one in years. They are thought to be extinct.
Java GUI - History In the beginning, there was AWT And it was a dark, dark time. Then the geeks said, let there be Swing. And so it was. Some of the elders tell stories about "Applets", but no one has seen one in years. They are thought to be extinct.
Java GUI - History In the beginning, there was AWT And it was a dark, dark time. Then the geeks said, let there be Swing. And so it was. Some of the elders tell stories about "Applets", but no one has seen one in years. They are thought to be extinct.
Java GUI - History In the beginning, there was AWT And it was a dark, dark time. Then the geeks said, let there be Swing. And so it was. Some of the elders tell stories about "Applets", but no one has seen one in years. They are thought to be extinct.
Java GUI - History In the beginning, there was AWT And it was a dark, dark time. Then the geeks said, let there be Swing. And so it was. Some of the elders tell stories about "Applets", but no one has seen one in years. They are thought to be extinct.
Swing Basics Objects just like everything else Most things inherit from java.awt.component Everything is done programatically Though there are visual tools that can generate code for you Strives for consistency
Swing Basics Objects just like everything else Most things inherit from java.awt.component Everything is done programatically Though there are visual tools that can generate code for you Strives for consistency
Swing Basics Objects just like everything else Most things inherit from java.awt.component Everything is done programatically Though there are visual tools that can generate code for you Strives for consistency
Swing Basics Objects just like everything else Most things inherit from java.awt.component Everything is done programatically Though there are visual tools that can generate code for you Strives for consistency
Swing Basics Objects just like everything else Most things inherit from java.awt.component Everything is done programatically Though there are visual tools that can generate code for you Strives for consistency
Swing Basics - JFrame import javax. swing. ; public class HelloSwing { public s t a t i c void main ( String [ ] args ) { JFrame frame = new JFrame ( " Hello Swing " ) ; frame. setdefaultcloseoperation ( JFrame.EXIT_ON_CLOSE ) ; frame. setsize ( 300, 100 ) ; frame. setvisible ( true ) ; } }
Swing Basics - JLabel import javax. swing. ; import java. u t i l. concurrent. ; public class HelloLabel { public s t a t i c void main ( String [ ] args ) throws Exception { JFrame frame = new JFrame ( " Hello Swing " ) ; JLabel label = new JLabel ( "A Label " ) ; frame. add ( label ) ; frame. setdefaultcloseoperation ( JFrame.EXIT_ON_CLOSE ) ; frame. setsize ( 300, 100 ) ; frame. setvisible ( true ) ; TimeUnit.SECONDS. sleep ( 1 ) ; label. settext ( " Hey! This i s d i f f e r e n t " ) ; } }
Swing Demo Display Helper import javax. swing. ; public class SwingConsole { public s t a t i c void run ( f i n a l JFrame f, f i n a l i n t width, f i n a l i n t height ) { SwingUtilities. invokelater ( new Runnable ( ) { public void run ( ) { f. s e t T i t l e ( f. getclass ( ). getsimplename ( ) ) ; f. setdefaultcloseoperation ( JFrame.EXIT_ON_CLOSE ) ; f. setsize ( width, height ) ; f. s e t V i s i b l e ( true ) ; } } ) ; } }
JButton import javax. swing. ; import java. awt. ; public class Button1 extends JFrame { private JButton b1 = new JButton ( " Button 1 " ) ; private JButton b2 = new JButton ( " Button 2 " ) ; public Button1 ( ) { setlayout ( new FlowLayout ( ) ) ; / / Note Layout Manager add ( b1 ) ; add ( b2 ) ; } } public s t a t i c void main ( String [ ] args ) { SwingConsole. run ( new Button1 ( ), 200, 100 ) ; }
Events Ok, we can create controls. How do we make them do something?
import javax. swing. ; import java. awt. ; import java. awt. event. ; public class Button2 extends JFrame { private JButton b1 = new JButton ( " Button 1 " ) ; private JButton b2 = new JButton ( " Button 2 " ) ; private JTextField t x t = new JTextField ( 10 ) ; class B u t t o n L i s t e n e r implements A c t i o n L i s t e n e r { public void actionperformed ( ActionEvent e ) { S t r i n g name = ( ( JButton ) e. getsource ( ) ). gettext ( ) ; t x t. settext ( name ) ; } } private ButtonListener bl = new ButtonListener ( ) ; public Button2 ( ) { b1. addactionlistener ( bl ) ; b2. addactionlistener ( bl ) ; setlayout ( new FlowLayout ( ) ) ; add ( b1 ) ; add ( b2 ) ; add ( t x t ) ; } } public s t a t i c void main ( String [ ] args ) { SwingConsole. run ( new Button2 ( ), 200, 150 ) ; }
import javax. swing. ; import java. awt. ; import java. awt. event. ; public class Button2b extends JFrame { private JButton b1 = new JButton ( " Button 1 " ) ; private JButton b2 = new JButton ( " Button 2 " ) ; private JTextField t x t = new JTextField ( 10 ) ; private ActionListener bl = new ActionListener ( ) { public void actionperformed ( ActionEvent e ) { S t r i n g name = ( ( JButton ) e. getsource ( ) ). gettext ( ) ; t x t. settext ( name ) ; } } ; public Button2b ( ) { b1. addactionlistener ( bl ) ; b2. addactionlistener ( bl ) ; setlayout ( new FlowLayout ( ) ) ; add ( b1 ) ; add ( b2 ) ; add ( t x t ) ; } } public s t a t i c void main ( String [ ] args ) { SwingConsole. run ( new Button2 ( ), 200, 150 ) ; }
Events ActionEvent AdjustmentEvent ComponentEvent ContainerEvent FocusEvent KeyEvent MouseEvent WindowEvent ItemEvent TextEvent JButton, JList, JTextField JScrollBar Component and its derivatives Container and its derivatives Component and its derivatives Component and its derivatives Component and its derivatives Window and its derivatives JCheckBox, JComboBox, JList JTextArea, JTextField
Event Demo
Layouts Java can automatically position or layout components Several different LayoutManagers Absolute positioning still available, but hard to do by hand Designer/code generation tools
Layouts Java can automatically position or layout components Several different LayoutManagers Absolute positioning still available, but hard to do by hand Designer/code generation tools
Layouts BorderLayout North South East West FlowLayout "flows" in GridLayout Rows by Columns GridBagLayout Control Freak BoxLayout Simplified GridBagLayout
Layout Demos
Swing Controls Buttons Icons TextFields Borders TextPane CheckBoxes RadioButtons ComboBoxes List TabbedPane MessageBoxes Menus Drawing Dialogs HTML Progress
Wrap-up
What We Covered Object Oriented Concepts Information Hiding Encapsulation Re-use Operators Control Statements Conditionals Loops Java coding style Access Control Scope Packages Visibility
Initialization & Cleanup Creating objects Constructors finalize Re-use Composition Delegation Inheritance Abstract Classes Interfaces Collections ArrayList HashMap Iterator What We Covered (cont.)
What We Covered (cont.) Exceptions Inner Classes Anonymous Inner Classes Type Information RTTI Class objects Arrays Generics I/O Files Streams
What We Covered (cont.) Basic threads Basic networkins GUI Swing Components Layouts Events
What we didn t cover Enumerated Types Containers in depth Annotations In Thinking in Java or on the web
Thank you and Good Night!