7.12 Graphical Objects
Now that we know how to design our own classes, we can separate a graphical object from an application. For example, we can define a sprite in its own Sprite class and write a client application that will instantiate and draw Sprite objects. This will allow us to encapsulate the sprite’s data and the code for drawing the sprite within the Sprite class. It also promotes reuse of the Sprite class by other programmers who might want to create Sprite objects for different applications.
The Sprite class is shown in Example 7.16. We start by defining the instance variables. We need the starting (x, y) coordinate to draw the sprite, so we define two int instance variables to hold those values (lines 9–10). The sX variable is ...
Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.