Java Reference
In-Depth Information
Drawing in a Window
In this chapter we will look at how you can draw using the Java 2D facilities that are part of JFC. We will see
how to draw in an applet and in an application. We will investigate how we can combine the event-handling
capability that we learned about in the previous chapter with the drawing facilities we'll explore in this
chapter, so that we can implement an interactive Graphical User Interface for creating a sketch.
By the end of this chapter you will have learned:
What components are available for creating a GUI.
How coordinates are defined for drawing on a component.
How you implement drawing on a component.
How to structure the components in a window for drawing.
What kinds of shapes you can draw on a component.
How you implement mouse listener methods to enable interactive drawing operations.
Using the Model/View Architecture
We need to develop an idea of how we're going to manage the data for a sketch in the Sketcher
program before we start drawing a sketch, because this will affect where and how we handle events. We
already have a class that defines an application window, SketchFrame , but this class would not be a
very sensible place to store the underlying data that defines a sketch. For one thing, we'll want to save a
sketch in a file, and serialization is the easiest way to do that. If we're going to use serialization to store a
sketch, we won't want all the stuff in the implementation of the SketchFrame class muddled up with
the data relating to the sketch we have created.
For another, it will make the program easier to implement if we separate out the basic data defining a
sketch from the definition of the GUI. This will be along the lines of the MVC architecture that we first
mentioned in Chapter 15, a variant of which is used in the definition of Swing components.
Search WWH ::




Custom Search