6.11 Cannon Class

The Cannon class (Figs. 6.106.14) represents the cannon in the Cannon Game app. The cannon has a base and a barrel, and it can fire a cannonball.

6.11.1 Instance Variables and Constructor

The Cannon constructor (Fig. 6.10) has four parameters. It receives

  • the CannonView that this Cannon is in (view),

  • the radius of the Cannon’s base (baseRadius),

  • the length of the Cannon’s barrel (barrelLength) and

  • the width of the Cannon’s barrel (barrelWidth).

Line 25 sets the width of the Paint object’s stroke so that the barrel will be drawn with the given barrelWidth. Line 27 aligns the Cannon’s barrel to be initially parallel with the top and bottom edges of the screen. The Cannon class has a Point barrelEnd that’s used to draw the barrel, ...

Get Android How to Program, 3/e 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.