O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

21.1. Serializing the Sketch

The Sketcher program can be considered to be a practical application only if you can save sketches in a file and retrieve them later—in other words, you need to implement serialization for a SketchModel object and use that to make the File menu work. Ideally, you want to be able to write the model for a sketch to a file and be able to read it back at a later date and reconstruct exactly the same model object. One obvious way to do this is to use serialization because the primary purpose of serialization is the accurate storage and retrieval of objects, so that's what you'll implement in this chapter. In Chapter 23 you'll explore another possibility—saving sketches in Extensible Markup Language (XML).

You've seen how to serialize objects back in Chapter 12. All you have to do to serialize a sketch document in our Sketcher program is to apply what you learned there. Of course, quite a few classes are involved in a sketch document but it will be remarkably easy considering the potential complexity of a sketch—I promise!

Putting in place the graphical user interface (GUI) functionality for saving a sketch on disk and reading it back from a file will be significantly more work than implementing serialization for the model. The logic of opening and saving files so as not to lose anything accidentally can get rather convoluted. Before you get into that, there is a more fundamental point I should address—a sketch doesn't have a name. You should at least make ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required