NestingShape’s top left corner) that corresponds to a point in the original coordinate
system. This can be achieved using Painter’s translate()method. Once translated, all
drawing operations are performed relative to the new origin. Note that any translation should
be reversed after painting a NestingShape.
E.g. the following code sets the new origin (0, 0) to (60, 40). After this, a Paintercall like
painter.drawRect(10, 10, 40, 20) would cause the rectangle to be painted at
absolute position 70, 50. The second translate()call restores the origin to (0, 0).
painter.translate(60, 40);
// Code to paint children …
painter.translate(-60, -40);
Testing
From the CodeRunner quiz named Bounce II, complete the first two questions:
(3 marks) Bounce II NestingShape, which runs a series of tests on your NestingShapeclass.
(1 mark) Bounce II NestingShape (static analysis), which examines the source code for your
NestingShapeclass.
Your code will need to pass all tests.
Task 2: add provision for any Shape to display text
Further develop the Shapeclass hierarchy to allow text to be displayed when a shape is painted. Text
should be centered, horizontally and vertically, in a shape’s bounding box, but may extend beyond the
left and right sides. Graphicsclass’ drawString()method and FontMetricsclass may be
used to implement the core functionality for this task.
In designing the revised class structure, you must guarantee that if a shape is associated with text that
it will always be painted. This responsibility should not be left to developers of Shapesubclasses. To
elaborate, consider the intention to make your Shapeclass hierarchy available – only the bytecode
and not the source files – to other developers. The above guarantee should hold for any Shape
subclasses added by other developers in the future. Note that since such developers will not have the
source code for your class hierarchy they cannot edit your classes; all they can do is extend the classes
that you provide.
Specific requirements
You must solve this problem by applying the Template Method design pattern. Recall the
constraint that it must be possible to call paint(Painter)and move(int, int)on any
object that is an instance of class Shapeor its subclasses.
The mandatory hook method required for the Template Method solution must be named
doPaint()and take a Painterparameter.
Class Shape and all of its subclasses should provide a 7-argument constructor where the
arguments are x, y, deltaX, deltaY, width, height and text, where text is a String. This adds the
capability for text to be supplied when constructing a shape. If other constructors are used,
the resulting shape is assumed not to have text.
During painting of a shape, any text should be painted last – so that it is superimposed over
any solid figure or image that has been painted for the shape.