Object-Oriented Programming (Java), Unit 23

Slides:



Advertisements
Similar presentations
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Advertisements

Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
1 Object-Oriented Programming (Java), Unit 20 Kirk Scott.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
1 Object-Oriented Programming (Java), Unit 22 Kirk Scott.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
1 Object-Oriented Programming (Java), Unit 23 Kirk Scott.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
Object-Oriented Programming (Java), Unit 29 Kirk Scott 1.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
1 Object-Oriented Programming (Java), Unit 23 Kirk Scott.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
1 Object-Oriented Programming (Java), Unit 17 Kirk Scott.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Concurrent Programming and Threads Threads Blocking a User Interface.
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
1 Object-Oriented Programming (Java), Unit 24 Stand In Final Project Description Kirk Scott.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Object-Oriented Programming (Java), Unit 29 Kirk Scott 1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
UID – Event Handling and Listeners Boriana Koleva
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1.
A brief introduction to javadoc and doxygen. What’s in a program file? 1. Comments 2. Code.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Creating a GUI Class An example of class design using inheritance and interfaces.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Field Trip #34 Creating and Reading Zip Files in Java By Keith Lynn.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CompSci 280 S Introduction to Software Development
Object-Oriented Programming (Java), Unit 22
Graphical User Interface (pronounced "gooey")
Chapter 13: Advanced GUIs and Graphics
Introduction to javadoc
Object-Oriented Software Engineering
Digital Image Processing
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Introduction to javadoc
Java Programming with BlueJ Objectives
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

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

Serializability, File Choosers, and Text Areas and Scroll Bars

23.1 Serializability

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.

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.

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.

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.");

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);

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.

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) …

23.2 File Choosers

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.

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

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.

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();

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();

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.

23.3 Text Areas and Scroll Bars

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:

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.

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()); }

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(); }

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());

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.

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.

The End