object-oriented programming (java), unit 28 kirk scott 1

80
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

Upload: quentin-manning

Post on 04-Jan-2016

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

1

Object-Oriented Programming (Java), Unit 28

Kirk Scott

Page 2: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

2

• Litla Dimun is a small island between the islands of Suouroy and Stora Dimun in the Faroe Islands. It is the smallest of the main 18 islands, being less than 100 hectares (250 acres) in area, and is the only one uninhabited. The island can be seen from the villages Hvalba and Sandvik.

• The southern third of the island is sheer cliff, with the rest rising to the mountain of Slaettirnir, which reaches 414 metres (1,358 ft). The island is only inhabited by feral sheep and seabirds. Getting ashore is difficult and can only be performed in perfect weather. The cliffs can be climbed with the aid of ropes placed by the owners of the sheep.

Page 3: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

3

Page 4: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

4

Page 5: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

5

Page 6: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

6

Page 7: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

7

Actions, Multicasting, and Text Areas

• 28.1 Actions and Multicasting• 28.2 Text Areas

Page 8: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

8

• 28.1 Actions and Multicasting

Page 9: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

9

MiscAction

• This is the key idea of this example:• Having more than one different event trigger

the same listener. • The example program illustrates this in the

following way:• Clicking the Swap button triggers the swap

listener• Pressing the key combination CTRL+S triggers

exactly the same listener code.

Page 10: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

10

• Keyboard input was initially given with the ClickKey example.

• You may have had some trouble getting both keystrokes and other actions to work using that approach.

• The approach given here is a more general solution.

Page 11: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

11

• It is your choice which approach to use in the final project.

• The point of these tail-end units in the course is to give you options for the project.

• In programming, you typically have choices to make.

• No path is ideal.• Each has its own pitfalls.

Page 12: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

12

• A screenshot of the application is shown below.

• There is no change in the appearance of this example from the previous example.

Page 13: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

13

Page 14: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

14

• This example illustrates the use of something called an action object

• This is attached to both the button and the keystroke combination.

• It is only necessary to make changes in the panel class in order to accomplish this.

Page 15: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

15

• The following UML diagram is not a diagram of the complete application.

• It shows the structure of the subset of the application which is new or different from previous examples.

• In other words, it shows how the action object, and instance of SwapAction, is tied to both a button and a keystroke.

• (It isn’t pretty)

Page 16: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

16

MiscActionPanel

KeyStroke

1

InputMap

1

JPanel ActionMap

12 1 2

String

1 1

JButton

1

SwapAction

1

1

AbstractAction

Page 17: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

17

• The code is shown for that part of the application where there are changes from the previous example or new elements.

• The parts of the MiscActionPanel() constructor which are different from the panel constructor in the previous example are given on the following overheads.

Page 18: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

18

• public MiscActionPanel()• {• …• /* Step 1. Create the swap action object. */• /* This is the listener for the application. */•  • Action swapActionObject = new

SwapAction("Swap");

Page 19: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

19

• /* Step 2. Create a button, giving it the swap action object. */

• JButton myButton = new JButton(swapActionObject); 

Page 20: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

20

• /* Step 3. Attach the button to the application as usual. */

• JPanel buttonPanel = new JPanel();• buttonPanel.add(myButton);• add(buttonPanel);

Page 21: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

21

• /* Step 4. This is part of the application that is focus related. */

• /* Get the input map belonging to the panel under construction. */

• /* Notice how this is analogous to getting a content pane. */

• /* A panel, by definition, has an input map associated with it. */

• InputMap inmap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

Page 22: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

22

• /* Step 5. This continues the part of the application which is focus related.

• /* You call the put() method on the input map. */• /* You give it two parameters: */• /* The first parameter is the keystroke combination of

interest. */• /* The second parameter is a symbolic name that will be

associated with this keystroke combination. */

• inmap.put(KeyStroke.getKeyStroke("ctrl S"), "registerswap");

Page 23: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

23

• /* Step 6. This is part of the application that is related to connecting the input map and the swap action object. */

• /* Get the action map belonging to the panel under construction. */

• /* Notice how this is analogous to getting a content pane. */

• /* A panel, by definition, has an action map associated with it. */

• ActionMap actmap = getActionMap();

Page 24: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

24

• /* Step 7. This is the last step, which glues things together. */

• /* You call the put() method on the action map. */• /* You give it two parameters: */• /* The first parameter is the symbolic name of the

input keystroke combination of interest. */• /* The second parameter is the swap action object. */• /* When the keystroke combination occurs, the action

object listener will be called. */

• actmap.put("registerswap", swapActionObject);• }

Page 25: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

25

• If you cut out the messy details, it’s these two lines of code which give the same listener to the button and the keystroke combination:

• JButton myButton = new JButton(swapActionObject);• …• actmap.put("registerswap", swapActionObject);

Page 26: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

26

• The class SwapAction is a new inner class of the MiscActionPanel class.

• The code for SwapAction is given on the overhead following the next one.

• This example is slightly different from previous examples.

• It includes a constructor.• The constructor includes a parameter.

Page 27: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

27

• These details are not of great practical consequence to the application.

• However, they do illustrate one thing.• We are creating a generic action, not a listener

for something that is predefined (like a particular mouse click).

• We are seeing the machinery that makes it possible to associate names and other information with something generic.

Page 28: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

28

• What is of consequence, as usual, is the actionPerformed() method.

• This is the code that is run when the listener is triggered, whether by a button click or the keystroke combination.

• The code is shown on the following overhead.

Page 29: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

29

• private class SwapAction extends AbstractAction• {• public SwapAction(String name)• {• putValue(Action.NAME, name);• putValue(Action.SHORT_DESCRIPTION, "Swap

register contents.");• }•  • public void actionPerformed(ActionEvent event)• {• registerA.swapRegisterContents(registerB);• }• }

Page 30: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

30

• This application, MiscAction, is a standalone example.

• The elements in it that support responsiveness to the keyboard are not retained in future examples.

• Keep in mind the point of the example:• More than one event can trigger the same action.• In other words, more than one different event

can cause the same actionPerformed() method to be executed.

Page 31: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

31

MiscMulti

• In the previous example, different events could trigger the same listener.

• The MiscMulti example illustrates the converse idea.

• There are many listeners for one event.• This structure in a program is known as

multicasting.

Page 32: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

32

• This example is one more step removed from practicality than the previous example

• It might be possible to dream up areas of the final project where you could apply this idea

• However, it would probably not be a good idea to do down that path

• This example is presented as background information that might be useful to you in the future

Page 33: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

33

• This application illustrates the idea of multicasting in the following way:

• There are two registers.• When each register is constructed, a button

listener will be constructed in it.

Page 34: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

34

• There is a single clear button.• The two button listeners, one for each register,

will be added to the button.• Each of the listeners listens for the same

event, the clicking of the clear button. • In short, one event triggers two listeners.

Page 35: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

35

• The visual result of clicking the clear button is that both of the text fields have their values set to 00000000.

• Not only should the text fields be cleared, but the state or model underlying them should also be changed

• The screenshot given on the following overhead shows the result of clicking the clear button.

Page 36: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

36

Page 37: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

37

• The following UML diagram is not a diagram of the complete application.

• It shows the structure of the subset of the application which is new or different from previous examples.

• It contains the class FieldButtonActionListener.• The class name is meant to suggest that this is

the listener for the button which affects the contents of the text fields.

Page 38: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

38

MiscMultiPanel

MiscMultiRegister

1

2

JTextField

1

TextFieldListener

1

12

JPanel

12

MiscMultiByte

1

JPanel

JButton

1

1

1

FieldButtonActionListener

1

«interface»ActionListener

Page 39: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

39

• On the following overhead the parts of the MiscMultiPanel() constructor which are different from the panel constructor in the previous example are given.

• One button is constructed. • A reference to that button is passed to each of

the registers when they are constructed.

Page 40: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

40

• public MiscMultiPanel()• {• JButton myButton2 = new JButton("Clear");•  • registerA = new MiscMultiRegister("11110000",

myButton2);• registerB = new MiscMultiRegister("00001111",

myButton2);•  • …•  • JPanel buttonPanel2 = new JPanel();• buttonPanel2.add(myButton2);• add(buttonPanel2);• }

Page 41: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

41

• The following overhead shows the MiscMultiRegister() constructor.

• In it an action listener is created and added to the button which is passed in as a parameter.

• Keep in mind that two registers are constructed in the application, so construction of a listener happens twice, once for each register.

• However, in both cases it is the same button that is passed in that has the listener added to it.

Page 42: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

42

• public MiscMultiRegister(String stringIn, JButton buttonIn)• {• registerByte = new MiscMultiByte(stringIn);• myField = new JTextField(stringIn, 8);• TextFieldListener myListener = new TextFieldListener();• myField.addActionListener(myListener);•  • FieldButtonActionListener myActionListener = new

FieldButtonActionListener();• buttonIn.addActionListener(myActionListener);• }

Page 43: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

43

• The code for the listener class for the clear button is shown on the next overhead.

• It is an inner class of the MiscMultiRegister class.

• All aspects of the code will be explained in detail on the overheads following this complete listing of the class.

Page 44: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

44

• private class FieldButtonActionListener implements ActionListener

• {• public void actionPerformed(ActionEvent event)• {• registerByte.setByteToThisString(MiscMultiByte.junk);• myField.setText(registerByte.getStringFromByte());• }• }

Page 45: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

45

• Each time a register is constructed, a listener is constructed.

• When the button is clicked, both listeners are triggered.

• Each listener makes calls on registerByte and myField.

• Those are the objects that belong to a register.

Page 46: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

46

• registerByte is part of the model of the application.

• myField is part of the view of the application.• Each of the instances of the register class have

a registerByte and a myField instance variable.• The two listeners refer, respectively, to these

instance variables of the two registers.

Page 47: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

47

• The listener code contains this call:• registerByte.setByteToThisString(MiscMultiByte.junk);

• For the one listener, this applies to the registerByte instance variable of the instance of the outer class, MiscMultiRegister, which that listener was constructed in.

• For the other listener, this applies to the registerByte of the MiscMultiRegister which it was constructed in.

Page 48: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

48

• Similarly, the listener code also contains this call:

• myField.setText(registerByte.getStringFromByte());

• Just like with the registerByte, for one of the listeners, this applies to the myField instance variable of the register it was constructed in.

• For the other listener, it applies to the myField instance variable of the register it was constructed in.

Page 49: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

49

• Incidentally, the first line of the listener code makes use of this static variable:

• MiscMultiByte.junk.• This shows the declaration of the variable junk in

the MiscMultiByte class. • This provides the value in the listener which clears

the register by setting their contents to all 0’s.• public static final String junk = "00000000";

Page 50: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

50

• MiscMulti is a standalone example. • The elements in it that support a clear button

with multicasting are not retained in future examples.

Page 51: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

51

28.2 Text Areas

• The last unit reviewed the idea of registers on a chip and introduced them into the example applications.

• This section reviews the concept of memory on a chip and introduces a representation of it into the example applications.

Page 52: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

52

• Just like the previous two examples, this example is intended to provide food for thought.

• You already know the minimum needed to work with text areas.

• This is just another example that may or may not be helpful when thinking about doing the final project.

Page 53: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

53

• In a computer, memory is the place where programs and data are stored for use by the CPU.

• Memory is measured in bytes, not bits. • It may also be measured in words, where a

word is some constant multiple of bytes.

Page 54: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

54

• Memory is addressable. • This means that in order, beginning at 0, the

bytes in memory are numbered.• It would also be possible to do addressing

based on numbering the words in memory.• Either way, addressing makes it possible for

machine language code to refer to locations in the program or to the locations which contain the values of program variables.

Page 55: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

55

• In the coming example programs, the contents of memory are held in an array of words where each word object contains 4 bytes.

• In other words, a word object will serve as a wrapper for an array of 4 instances of a byte object.

• The bytes in memory are the same as the bytes in registers—they are sequences of 8 of the characters 0 and 1.

Page 56: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

56

• Memory is represented visually using a JTextArea which has scroll bars.

• A programming project based on this example would require addressability of memory.

• The next two example programs have elements which touch on this.

Page 57: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

57

• The first illustrates loading a program into memory.

• The second illustrates moving a byte to location 0 in memory.

• From that it is possible to infer how to move values to any location in memory.

Page 58: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

58

MiscMemory

• This program illustrates the implementation of machine memory using a class, and representing it visually using a text area with scroll bars.

• The functionality of loading a file into the memory is implemented through a menu option.

Page 59: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

59

• There is no explicit handling of focus in this example.

• The automatic traversal of components using the tab key is in effect.

• Focus is not a major issue in this example, but it comes up for the following reason:

• A text area can receive text if it’s editable.• What happens when you type keystrokes depends

on whether the cursor is in the text area or not.

Page 60: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

60

• The text area doesn’t have focus by default.• However, once the user clicks on the text area

and places the cursor there, the text area does have focus.

• Until it loses focus, all keystrokes will be interpreted as text being entered into it.

• The keystrokes will not be interpreted as commands.

Page 61: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

61

• A key listener implemented as an inner class of a panel can be added to a text area.

• The text area will automatically receive keystrokes as input, but it is also possible to define actions for them at the same time in the listener code.

• This is not done in this example, but it is worth knowing that this is possible.

Page 62: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

62

• A screenshot of the application is shown on the overhead following the next one.

• It shows the application with the file sumtenV1.txt loaded into it.

• This is the example machine language program that can be run in a complete machine simulation

• The memory can hold 256 words. • sumtenV1.txt is only 14 words long.

Page 63: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

63

• The application is written so that each word in memory appears on a separate line in the text area.

• It is also written so that the memory following the loaded program is filled with 0’s.

• When the machine language program is initially loaded, the text area scrolls to the bottom.

• The screenshot shows the area after manually scrolling back up to the top.

• It also shows the two menu options for this example.

Page 64: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

64

Page 65: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

65

• The UML diagram for this application is relatively complete, but it does not show every last detail.

• The purpose of the diagram is to give an overall impression of the application which emphasizes the key points.

• Notice that the MiscMemoryMemory class is shown as having an array, where this is given as another class labeled (Array) in the diagram.

Page 66: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

66

• In the code, the array containing the memory is not constructed using the syntax new Arrary().

• It is constructed using the [] notation. • Even though the name of the Array class doesn’t

appear in the code, the array is shown in the UML diagram in order to bring out the fact that an array of words is used to implement memory.

• It is this array of words that is used to fill the text area that represents memory.

Page 67: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

67

MiscMemory

MiscMemoryFrame JFileChooser

1

1

ContentPane JMenuBar

MiscMemoryPanel JMenu

1 1

1 1

MiscMemoryMemory JScrollPane

(Array) JTextArea

1 1

1 11

JMenuItem

LoadProgramListener

1

1

MiscMemoryWord

256

Page 68: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

68

• The code for MiscMemory is not listed here because it would take up eight pages.

• It is much more practical to download the code separately or to look at it online or through an editor.

• The UML diagram provides a guide to the places to look for important aspects of the implementation.

Page 69: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

69

• At this final stage, the example programs are cumulative.

• All of the elements of this example are retained in the next example.

• The example application is approaching its final state.

Page 70: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

70

MiscRegAndMem

• The previous program didn’t have any registers in it.

• It only had the new construct, memory.• This program puts together registers and

memory. • It combines features of the MiscLabel and

MiscMemory examples.

Page 71: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

71

• This example also implements an interaction between the registers and memory.

• Clicking the swap button causes the values in the registers to be swapped.

• It also causes the value in register A to be placed in offset 0, that is, address 0, of memory.

Page 72: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

72

• When the application is started the text area is completely empty.

• The registers are initialized as follows:• Register A contains 11110000.• Register B contains 00001111.• The application still has a menu option to load

a file like sumtenV1.txt into memory. • The screenshot does not illustrate this.

Page 73: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

73

• The screenshot is given on the next overhead.• It illustrates the functionality of clicking the

Swap button in this version of the application.• The values of registers A and B are exchanged.• The value of register A is put at address 0 of

memory.• The rest of memory is filled with 0’s.

Page 74: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

74

Page 75: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

75

• This example has the most extensive layout characteristics of any example so far.

• A fairly complete UML diagram is given because it is easier to see the overall structure of the application by looking at a diagram than by looking at code.

Page 76: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

76

• Note that all of the visual elements of the application appear in panels.

• These panels are added to the content pane.• Some of these panels have layouts of their

own.• The main panel has a grid layout.• The UML diagram is given on the next

overhead.

Page 77: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

77

MiscRegAndMem

MiscRegAndMemFrame

JFileChooser

1

1

ContentPane

JMenuBar

MiscRegAndMemMemoryPanel

JMenu

1

1

1

1

MiscRegAndMemMemory JScrollPane

(Array) JTextArea

1 1

1 11

JMenuItem

LoadProgramListener

1

1

MiscRegAndMemRegisterPanel

1GridLayout

1

JPanelJPanel

2 2

JLabel

1

MiscMemAndRegRegister

2

JTextField

MiscRegAndMemByte TextFieldListener

1 1

1

JPanel

JPanel

JButton

MiscButtonListener

1

1

1

1

BorderLayout

1

1

MiscRegAndMemWord

256

Page 78: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

78

• For all practical purposes at this point the example application is structurally complete.

• As with the last example, the code for this example is not shown because it’s too long.

• It is much more practical to download the code separately or to look at it online or through an editor.

• The UML diagram provides a guide to the places to look for important aspects of implementation.

Page 79: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

79

The End

Page 80: Object-Oriented Programming (Java), Unit 28 Kirk Scott 1

80

• If I start doing unit 29 again, consider the handling of the different versions of Wari and consider what this random note was for:

• See the note at the end of the unit 29 powerpoint overheads about the three versions of WariV12

• In the next unit a new menu option will be added which will cause multiple copies of the frame to be created.

• However, the contents of each of the frames will be the same and will include the functionality illustrated up to this point.