View
216
Download
2
Embed Size (px)
Citation preview
Lecture 24 1
CS110 Lecture 25Tuesday, 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
Lecture 24 2
Persistence
• Bank and Juno should remember state between invocations
read state from a file at startupwrite state back to file at exit
• Can imagine a text representation of the state
• Better: Java knows how to save whole Objects
Lecture 24 3
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
Lecture 24 4
Bank (version 9)
public class Bank implements Serializable
• java Bank –f bankFileName• if (bankFileName == null) { theBank = new Bank( bankName );
} else { theBank = readBank
( bankName, bankFileName ); }
Lecture 24 5
Read Bank instance from a fileprivate 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;
}
Lecture 24 6
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
Lecture 24 7
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
Lecture 24 8
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
Lecture 24 9
10/joi
Lecture 24 10
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
Lecture 24 11
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);
Lecture 24 12
Building window behavior
• Add a listener to the button43 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
Lecture 24 13
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 }
Lecture 24 14
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 }
Lecture 24 15
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 }
Lecture 24 16
Running the program• as an application • > java JOIPanelpublic 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);}
Lecture 24 17
Running the program• as an applet from a browser: file joi.html<html>
<body>
<applet code="JOIPanel.class" height=100 width=400>
</applet>
</body>
</html>
This file is written in html (“hypertext markup language”), not Java. Browsers understand html.
main method never runs. Execution starts with init.
Lecture 24 18
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
Lecture 24 19
Lecture 24 20
All together nowpublic class JOIApplet extends Applet implements ActionListener
{ ...
public void actionPerformed ( ActionEvent e )
{
currentMessage = currentMessage.equals(MESSAGE1) ? MESSAGE2 : MESSAGE1;
this.repaint();
}
Lecture 24 21
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
.
Lecture 24 22java Juno -e -g