Step 1 Laying the Groundwork


Introduction

This chapter will cover some advanced GUI techniques, but more importantly, it will give you the confidence that you can do some pretty clever programming with the Swing API. The Swing API is powerful and flexible but its complexity can be intimidating. In the hands of an experienced Swing programmer, it can produce marvels, but it takes nothing short of a lot of experience to master. On the other hand, it is possible to write a great variety of useful programs with knowledge only of a small basic subset of its features. What I would like to convey to you in this chapter is the confidence that “if you can imagine it, you can program it with Swing”. To that end, this chapter will guide us through many GUI issues in an attempt to create a GUI interface that transcends the abilities of the standard Swing components.

At this point in your Java studies, I expect that you are able to independently explore issues that a chapter just touches on. For instance, one of the topics this chapter covers is creating offscreen images; but really all it does is show you a couple lines of code that create one type of offscreen image. If you would like to explore the issue deeper (and it does go deep) then those two lines will be your stepping stone to further discovery. With that in mind, let us begin our journey.

The Project

In this chapter, we will develop a small application that is a simple graphic tool a child might use to help reinforce the process of dressing himself. This was indeed the motivation for me when I devised the program. My one child had just turned 5. He knew how to dress himself, but I would often find myself trying to convince him that (for example) if he put his shirt on before he put on and snapped his pants, it would be easier to tuck it in because his pants wouldn’t already be snapped tight. After a while, the boredom of hearing me repeat myself set in and I began to think of a different kind of solution. My mind gravitated to programming and before I knew it, I had an idea for a small Java program that would have a picture of a little boy and a list of all the clothes he could put on. Somehow the program would allow the user to put clothes on the boy. The order in which the clothes were put on would be important, so that it would look funny if, for instance, he tried to put on the socks after he had already put on the shoes. I didn’t know exactly how the program’s interface would enable the order of clothes to be changed but I knew it was possible. As it turned out, Swing did not offer a component that could do it easily, so I set off on a step by step effort to develop a component that could. I made some mistakes and many discoveries and gradually achieved success. So that this chapter’s goal is clearly established, take time now to run the final version of the developed application and play with it.

On the supplemental CD-ROM, navigate to the Book_Code_Examples/Chapters_12-13-14-16 directory and execute the following command:

               java –cp classes chap14.gui5.MainFrame

Figure 14-1 shows the results of running the application. Select items in the list and click in the checkboxes to cause the boy to wear that article of clothing. Make sure you drag checked list items up and down to see the affect this has on the picture. That’s all there is to the program, but it brought a smile to my boys face! This chapter will follow the development of this program from start to finish. In the process we will develop a handy component that I call a DragList as well as a reusable customized renderer. But I’m getting ahead of myself. First things first.

image from book
Figure 14-1: This Chapter’s Completed Application

The Approach

We will approach our final goal through several steps using different packages to organize the code, reusing as much as we reasonably can. Each step will yield a running program. We will run it and determine what it does that is good, and what we need to add next. Having smaller goals like this helps to build confidence. Just like a climber on a mountain or a tree, it’s important to catch our breath every once in a while to appreciate where we are, how far we’ve come and how far there is to go.

This chapter’s program code is organized into six packages named gui0, gui1, gui2, gui3, gui4 and gui5, which correspond to six steps in our development process. All the classes for the first step will go into package gui0. Then for each additional step, if we can use any class from a previous step as is, we will use it by importing it (using the import statement). Any class from a previous step that needs modification will be completely rewritten and placed in the current step’s package. Any new class written for the current step will also be put into the current step’s package.

In the previous chapter, you may remember that ListeningMainFrame was repeatedly modified by extension. In this chapter, because class modifications will be more significant, using extension would be awkward at best. So, to sum up, classes that need no modification will be imported from the last package that defined or modified them, while modified and new classes will be part of the current package.

This approach was contrived for the chapter’s peculiar purpose of simultaneously minimizing code rewrites and maintaining code from previous steps. If you were developing on your own, you would probably want to keep all your code in one package and modify your classes directly. You might even have a system, as I do, where you archive classes that you are planning to modify just in case the modifications turn sour.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net