Download presentation
Presentation is loading. Please wait.
1
Lecture 241 CS110 Lecture 25 Tuesday, May 4, 2004 Announcements –final exam Thursday, May 20, 8:00 AM McCormack, Floor 01, Room 0608 (easier than last Tuesday’s!) –wise0 due tonight Agenda –Questions (WISE and otherwise) –Persistence –GUI programming –Interfaces
2
Lecture 242 Persistence Bank and Juno should remember state between invocations read state from a file at startup write state back to file at exit Can imagine a text representation of the state Better: Java knows how to save whole Objects
3
Lecture 243 Bank (version 9) Bank instance can be saved to a file java Bank –f bankFileName live demo … if –f bankFileName && file exists read Bank from that file else create new Bank() visit bank if –f bankFileName write Bank to that file
4
Lecture 244 Bank (version 9) public class Bank implements Serializable java Bank –f bankFileName if (bankFileName == null) { theBank = new Bank( bankName ); } else { theBank = readBank ( bankName, bankFileName ); }
5
Lecture 245 Read Bank instance from a file private static Bank readBank( String bankName, String bankFileName) { File file = new File( bankFileName ); if (!file.exists()) { return new Bank( bankName ); } ObjectInputStream inStream = null; try { inStream = new ObjectInputStream( new FileInputStream( file ) ); Bank bank = (Bank)inStream.readObject(); System.out.println( "Bank state read from file " + bankFileName); return bank; }
6
Lecture 246 Why read/write only Bank? BankAccount and Month are also Serializable Bank box-and-arrow picture shows that all objects of all types are pointed to (indirectly) by arrows starting in the Bank private transient Terminal atm; Terminal not saved when Bank is saved
7
Lecture 247 Serializable new Java keyword implements, as in public class Bank implements Serializable {...} Serializable is an interface, not a class Find out about interfaces in cs210 (and a little bit soon) Java 1.5 does a cleaner job with persistence
8
Lecture 248 GUI Programming Fun –some introductory courses start here –requires hardware support (PCs) –maybe more in CS110 in time GUI syntax: what windows look like - containers and components GUI semantics: how windows behave – event driven programming System does lots of work for you Learn the APIs, use software tools
9
Lecture 249 10/joi
10
Lecture 2410 GUI Syntax (what windows look like) AWT (Abstract Windowing Toolkit) Abstract class Container –subclasses: Panel, Window, Frame –methods: add, setLayout Abstract class Component –subclasses: Button, Checkbox, Textfield, Up to date Java GUI programmers use swing: –classes: JButton, JTextfield,… or they use GUI builders
11
Lecture 2411 Building window look and feel class JOIPanel extends Applet extends Panel init method creates a button and adds it to this Panel, sets font: 38 button = new Button( "Press Me" ); 39 this.add( button ); 40 font = new Font("Garamond", Font.BOLD, 48);
12
Lecture 2412 Building window behavior Add a listener to the button 43 button.addActionListener( new JOIButtonListener( this ) ); When button is pressed, system sends an actionPerformed message to the listener To see what happens, look at method actionPerformed in class JOIButtonListener
13
Lecture 2413 JOIButtonListener // constructor remembers the Panel 27 public JOIButtonListener ( JOIPanel panel ) 28 { 29 this.panel = panel; 30 } // send panel a changeMessage message now! 41 public void actionPerformed ( ActionEvent e ) 42 { 43 panel.changeMessage(); 44 }
14
Lecture 2414 Changing the message In JOIPanel: change the message and ask the system to repaint: 51 public void changeMessage() 52 { 53 currentMessage = 54 currentMessage.equals(MESSAGE1) ? MESSAGE2 : MESSAGE1; 55 this.repaint(); 56 }
15
Lecture 2415 Changing the message repaint( ) (which is really super.repaint( )) invokes paint( ) A Graphics object is like a programmable pen or paintbrush 67 public void paint(Graphics g) 68 { 69 g.setColor(Color.black); 70 g.setFont(font); 71 g.drawString(currentMessage, 40, 75); 72 }
16
Lecture 2416 Running the program as an application > java JOIPanel public static void main( String[] args ) { Terminal t = new Terminal(); Frame frame = new Frame(); JOIPanel panel = new JOIPanel(); panel.init(); frame.add(panel); frame.setSize(400,120); frame.show(); t.readLine("return to close window "); System.exit(0); }
17
Lecture 2417 Running the program as an applet from a browser: file joi.html This file is written in html (“hypertext markup language”), not Java. Browsers understand html. main method never runs. Execution starts with init.
18
Lecture 2418 Interfaces An interface is like a very abstract class –no fields –only abstract methods A class that implements an interface promises to implement its abstract methods public class JOIButtonListener implements ActionListener
19
Lecture 2419
20
Lecture 2420 All together now public class JOIApplet extends Applet implements ActionListener {... public void actionPerformed ( ActionEvent e ) { currentMessage = currentMessage.equals(MESSAGE1) ? MESSAGE2 : MESSAGE1; this.repaint(); }
21
Lecture 2421 Juno 10 has a GUI Juno CLI prompts for input when it wants it => the program is in control Juno GUI sits waiting for user input from anywhere mouse can go, then takes action => the user is in control (event driven) > java Juno –g.
22
Lecture 2422 java Juno -e -g
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.