I have a class that is responsible for creating Formation objects from Shape objects. Shapes are what the name says, shapes that are drawn on the canvas ( TriangleShape , RectangleShape , etc.).
Shapes are similar to shapes, but I plan to use them differently.
RectangleShape , for example, looks like this:
public class RectangleShape extends Shape { public RectangleShape() { this(0, 0, 0, 0); } public RectangleShape(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; this.nPoints = 4; } @Override public void drawShape(Graphics2D g) { Color color = g.getColor(); fillShape(g, new Color(g.getColor().getRed(), g.getColor().getGreen(), g.getColor().getBlue(), 16)); g.setColor(color); g.drawLine(x, y, x + width, y); g.drawLine(x, y, x, y + height); g.drawLine(x, y + height, x + width, y + height); g.drawLine(x + width, y, x + width, y + height); } @Override public String toString() { return "Rectangle"; } @Override public Shape createCopy() { return new RectangleShape(); } @Override public void fillShape(Graphics2D g) { xPoints = new int[] { x, x, x + width, x + width }; yPoints = new int[] { y, y + height, y + height, y }; g.fillPolygon(xPoints, yPoints, nPoints); } }
I save a list of all the drawn shapes declared as List<Shape> = new ArrayList<>(); .
My problem arises when I need to dynamically create a formation from a form. The first approach was to have a class with these methods:
public static TriangleFormation createFormationFrom(TriangleShape shape) { // my code here } public static RectangleFormation createFormationFrom(RectangleShape shape) { // my code here } public static PentagonFormation createFormationFrom(PentagonShape shape) { // my code here } public static HexagonFormation createFormationFrom(HexagonShape shape) { // my code here } public static OvalFormation createFormationFrom(OvalShape shape) { // my code here }
The problem is that when I retrieve a form from my list, it is of type Shape , and I cannot call any of these methods without dropping the shape into the appropriate class, and then it begs the question of using instanceOf .
Should I combine the form and the formula in one class, should I try to implement a visitor template, perhaps (if so, how will it be done in this case) or is there something else that I did not think about?