Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming (Java), Unit 23

Similar presentations


Presentation on theme: "Object-Oriented Programming (Java), Unit 23"— Presentation transcript:

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

2 Serializability, File Choosers, and Text Areas and Scroll Bars

3 23.1 Serializability

4 ClickSave This example program adds two menu options to the example, one to save the current state of the cups in the application, and the other to reload them.

5 The Serializable interface
To be serializable, a class has to implement the Serializable interface. It possible to save and load complete objects of a class if it implements this interface. This interface does not require the implementation of any methods. Certain other requirements have to be met in order to implement the interface. Every object that it contains also has to implement this interface.

6 The Cup and Seed classes:
The SeedCup implements the Serializable interface. The SeedCup class has a Rectangle instance variable. The Rectangle class implements the Serializable interface. Rectangle2D.Double does not. The Seed class also implements the Serializable interface. None of the system supplied ellipse classes implement the Serialiable interface. Seeds do not have ellipses. The dots are transient visual representations.

7 The SaveListener: The user is prompted for the name of a file.
An ObjectOutputStream is constructed, and the objects of the application are written to it, one after the other. private class SaveListener implements ActionListener { public void actionPerformed(ActionEvent event) String inputString; inputString = JOptionPane.showInputDialog ("Enter file path name for saving.");

8 try { ObjectOutputStream objOut = new ObjectOutputStream (new FileOutputStream(inputString)); objOut.writeObject(myPanel.getMyCupA()); objOut.writeObject(myPanel.getMyCupB()); objOut.writeObject(myPanel.getWhichCupActive()); } catch(IOException e) System.out.println("Problem making or writing to an output stream."); System.out.println(e);

9 The LoadListener: The user is prompted for the name of a file.
The current state of the application is disposed of by removing the current panel. A new panel is created and added to the content pane. An ObjectInputStream is constructed. The file containing the SeedCup objects is read from the beginning. The objects are retrieved in the order in which they were saved and put into the application panel.

10 private class LoadListener implements ActionListener
{ public void actionPerformed(ActionEvent event) String inputString; inputString = JOptionPane.showInputDialog ("Enter file path name for loading."); try Container contentPane = getContentPane(); contentPane.remove(myPanel); myPanel = new ClickSavePanel(); contentPane.add(myPanel, "Center"); ObjectInputStream objIn = new ObjectInputStream( new FileInputStream(inputString)); myPanel.setMyCupA((SeedCup) objIn.readObject()); myPanel.setMyCupB((SeedCup) objIn.readObject()); myPanel.setWhichCupActive((SeedCup) objIn.readObject()); setVisible(true); } catch(IOException e)

11 23.2 File Choosers

12 ClickChooser: This example program makes use of a JFileChooser in the SaveListener and the LoadListener. The screenshot below shows what appears if the Save option is taken in the menu.

13 These lines of code appear in the ClickChooserFrame class and its constructor, respectively:
JFileChooser myChooser; myChooser = new JFileChooser();

14 The SaveListener and the LoadListener:
The call to setCurrentDirectory() will cause the chooser to open the home directory of the running application. The call to showSaveDialog() causes the chooser to be displayed. The call to getSelectedFile().getPath() obtains the full path name of the file which the user selects or enters.

15 Key parts of the SaveListener:
private class SaveListener implements ActionListener { public void actionPerformed(ActionEvent event) myChooser.setCurrentDirectory(new File(".")); myChooser.showSaveDialog(ClickChooserFrame.this); fileName = myChooser.getSelectedFile().getPath();

16 Key parts of the LoadListener:
private class LoadListener implements ActionListener { public void actionPerformed(ActionEvent event) myChooser.setCurrentDirectory(new File(".")); myChooser.showOpenDialog(ClickChooserFrame.this); fileName = myChooser.getSelectedFile().getPath();

17 Slow construction of instances of JFileChooser
There is an unpleasant interaction between the Windows operating system and instances of JFileChooser in Java. The construction of a JFileChooser will take a very long time if there are any zipped folders on the desktop of the machine you are running on. In order to get reasonable performance of the Java code, move any zipped folders into another, unzipped folder. This is very strange, and it's enough to make you cry, but at least there is an easy solution that doesn't involve elaborate changes to your code.

18 23.3 Text Areas and Scroll Bars

19 ClickTextScroll: This example uses a JTextArea and a JScrollPane to include a textual record of the sequence of actions in the application. Here is a screenshot of the application’s appearance after four mouse clicks on the alternately active cups, A and B:

20 The ClickTextScrollPanel:
This application has changes in the ClickTextScrollPanel class and its constructor. The panel has an instance variable, actionRecordArea, constructed in place. In the panel constructor an instance of JScrollPane is constructed and the actionRecordArea is passed to it as a parameter. The result of this construction, actionScrollPane, contains the actionRecordArea and has scroll bars.

21 class ClickTextScrollPanel extends JPanel
{ private JTextArea actionRecordArea = new JTextArea(6, 24); private JScrollPane actionScrollPane; private SeedCup myCupA; private SeedCup myCupB; private SeedCup whichCupIsActive; private int moveCount = 1; public ClickTextScrollPanel() myCupA = new SeedCup(4, 200, 200, 40, 40); myCupB = new SeedCup(0, 250, 200, 40, 40); whichCupIsActive = myCupA; actionRecordArea.setEditable(false); actionScrollPane = new JScrollPane(actionRecordArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); add(actionScrollPane, "North"); addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHandler()); }

22 The MouseHandler: When an action is taken and the mouseClicked() method is triggered, the text describing the action is appended to the actionRecordArea. private class MouseHandler extends MouseAdapter { public void mouseClicked(MouseEvent event) if(myCupA.getRectangle().contains(event.getPoint()) && whichCupIsActive == myCupA) myCupA.moveCupValueTo(myCupB); whichCupIsActive = myCupB; actionRecordArea.append("Move: " + moveCount + ". Cup: A.\n"); moveCount++; repaint(); }

23 else if(myCupB.getRectangle().contains
(event.getPoint()) && whichCupIsActive == myCupB) { myCupB.moveCupValueTo(myCupA); whichCupIsActive = myCupA; actionRecordArea.append("Move: " + moveCount + ". Cup: B.\n"); moveCount++; repaint(); } else setCursor(Cursor.getDefaultCursor());

24 Applications with multiple parts
As you do the assignments, your application will grow to have multiple parts. Getting them all to work consistently becomes a challenge. For example, the restart listener has to renew the game board. After adding a text area to the application, the restart listener should also renew it. Also, after adding a text area, loading and saving the current state require saving the contents of the text area as well as the state of the board.

25 Updating the text area:
When working with the contents of the text area, the setText() and getText() methods can be used. These methods are inherited by the JTextArea class from the JTextComponent class. setText() takes a String as a parameter. To clear a text area, all you have to do is pass in the empty String, “”. To set the text area with text retrieved with the load operation, call setText() with that text as a String. The getText() method returns a String. It can be called in order to obtain the text that needs to be saved when taking the Save option in the menu.

26 The End


Download ppt "Object-Oriented Programming (Java), Unit 23"

Similar presentations


Ads by Google