File: /Users/shatabdi/Education/GitHub/se450_project/src/model/shape/DrawShape.java

1     //This class will draw and highlight a shape
2     package model.shape;
3     
4     import java.awt.BasicStroke;
5     import java.awt.Graphics;
6     import java.awt.Graphics2D;
7     import java.awt.Point;
8     import java.awt.Rectangle;
9     import java.awt.Shape;
10     import java.awt.Stroke;
11     import java.awt.geom.AffineTransform;
12     import java.util.ArrayList;
13     import java.util.List;
14     
15     import controller.interfaces.IUndoable;
16     import model.GeometricShape;
17     import model.commands.CommandHistory;
18     import model.factory.ShapeFactory;
19     import model.interfaces.IShape;
20     import model.strategy.ShapeShadingStrategy;
21     import model.strategy.ShapeTypeStrategy;
22     import view.Enum.ShapeColor;
23     import view.Enum.ShapeShadingType;
24     import view.Enum.ShapeType;
25     import view.adapter.ColorAdapter;
26     import view.interfaces.PaintCanvasBase;
27     
28     public class DrawShape implements IShape, IUndoable {
29     	
30     	ShapeColor highlightColor = ShapeColor.YELLOW; //color to highlight
31     	BasicStroke highlightStroke = new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 9.8f,
32     			new float[] { 9 }, 0.0f); //stroke to highlight
33     	
34     	private PaintCanvasBase canvasBase;
35     	private GeometricShape geometricShape;
36     	private Graphics2D graphics2d;
37     	Shape shape = null;
38     	ShapeTypeStrategy shapeTypeStrategy = null;
39     	BasicStroke stroke = new BasicStroke(5.0f);
40     
41     	public DrawShape(DrawShape ds, PaintCanvasBase canvasBase) {
42     		GeometricShape geomShape = ds.getShapeProperty();
43     
44     		this.geometricShape = new GeometricShape(geomShape.getStartPoint(), geomShape.getEndPoint());
45     
46     		geometricShape.setShapeType(geomShape.getShapeType()).setShadingType(geomShape.getShadingType())
47     				.setPrimaryColor(ColorAdapter.getColor(geomShape.getPrimaryColor()))
48     				.setSecondaryColor(geomShape.getSecondaryColor());
49     
50     		this.canvasBase = canvasBase;
51     		this.graphics2d = canvasBase.getGraphics2D();
52     		createShape();
53     	}
54     
55     	public DrawShape(PaintCanvasBase canvasBase, GeometricShape geometricShape) {
56     		this.canvasBase = canvasBase;
57     		this.graphics2d = canvasBase.getGraphics2D();
58     		this.geometricShape = geometricShape;
59     
60     		createShape();
61     	}
62     
63     	@Override
64     	public void addX(int x) {
65     		// TODO Auto-generated method stub
66     
67     	}
68     
69     	@Override
70     	public void addY(int y) {
71     		// TODO Auto-generated method stub
72     
73     	}
74     
75     	@Override
76     	public Boolean contain(Point startpoint) {
77     		// TODO Auto-generated method stub
78     		return null;
79     	}
80     
81     	@Override
82     	public IShape copyShape() {
83     		return new DrawShape(this, canvasBase);
84     	}
85     
86     	@Override
87     	public void create() {
88     		CommandHistory.shapesSelected.clear();
89     		paintShapeOnCanvas();
90     		CommandHistory.shapeCollection.add(this);
91     	}
92     
93     	public void createShape() {
94     
95     		shape = ShapeFactory.createShape(geometricShape);
96     	}
97     
98     	@Override
99     	public void deleteShape() {
100     		CommandHistory.shapeCollection.remove(this);
101     	}
102     
103     	public void draw() {
104     		create();
105     		canvasBase.repaint();
106     	}
107     
108     	@Override
109     	public void draw(Graphics g) {
110     		// TODO Auto-generated method stub
111     
112     	}
113     
114     	public Shape getBoundingBox() {
115     		return shape.getBounds();
116     	}
117     
118     	@Override
119     	public Point getEndPoint() {
120     		// TODO Auto-generated method stub
121     		return null;
122     	}
123     
124     	public List<IShape> getNodeList() {
125     		List<IShape> newList = new ArrayList<>();
126     		newList.add(this);
127     		return newList;
128     	}
129     
130     	@Override
131     	public ShapeColor getPrimaryColor() {
132     		// TODO Auto-generated method stub
133     		return null;
134     	}
135     
136     	@Override
137     	public ShapeColor getSecondaryColor() {
138     		// TODO Auto-generated method stub
139     		return null;
140     	}
141     
142     	@Override
143     	public ShapeShadingType getShadingType() {
144     		// TODO Auto-generated method stub
145     		return null;
146     	}
147     
148     	public Shape getShape() {
149     		return shape;
150     	}
151     
152     	public GeometricShape getShapeProperty() {
153     		return geometricShape;
154     	}
155     
156     	@Override
157     	public ShapeType getShapeType() {
158     		// TODO Auto-generated method stub
159     		return null;
160     	}
161     
162     	@Override
163     	public Point getStartPoint() {
164     		// TODO Auto-generated method stub
165     		return null;
166     	}
167     
168     	@Override
169     	public Stroke getStroke() {
170     		// TODO Auto-generated method stub
171     		return null;
172     	}
173     
174     	public void highlightShape() {
175     
176     		ShapeShadingStrategy shapeShade = ShapeFactory.combine(ShapeShadingType.OUTLINE, highlightColor,
177     				ShapeColor.GREEN, shape, graphics2d, highlightStroke);
178     
179     		shapeShade.draw(graphics2d);
180     	}
181     
182     	@Override
183     	public void moveShape(int X, int Y) {
184     
185     		AffineTransform transform = new AffineTransform();
186     		transform.translate(X, Y);
187     		Shape offsetShape = transform.createTransformedShape(this.getShape());
188     		this.setShape(offsetShape);
189     
190     	}
191     
192     	public void paintShapeOnCanvas() {
193     
194     		ShapeShadingType shapeShadingType = geometricShape.getShadingType();
195     		ShapeColor primaryColor = geometricShape.getPrimaryColor();
196     		ShapeColor secondaryColor = geometricShape.getSecondaryColor();
197     		ShapeShadingStrategy shapeShade = ShapeFactory.combine(shapeShadingType, primaryColor, secondaryColor, shape,
198     				graphics2d, stroke);
199     		shapeShade.draw(graphics2d);
200     	}
201     
202     	@Override
203     	public IShape pasteShape() {
204     		AffineTransform transform = new AffineTransform();
205     
206     		DrawShape shape = new DrawShape(this, canvasBase);
207     		int xCoordCopiedShape = shape.getShapeProperty().getShapeXcoord();
208     		int width = shape.getShapeProperty().getWidth();
209     		int yCoordCopiedShape = shape.getShapeProperty().getShapeYcoord();
210     		int height = shape.getShapeProperty().getHeight();
211     
212     		// ** paste the copied shape next to the original
213     
214     		int posX = xCoordCopiedShape + width + 1;
215     		int posY = yCoordCopiedShape + 1;
216     
217     		// reset posX and posY if they go beyond screen
218     		if ((posX > 900) || (posY > 300)) {
219     			posX = 1;
220     			posY = 1;
221     		}
222     		// the paste positions are still buggy
223     		transform.translate(posX, posY);
224     
225     		Shape offsetCopiedShape = transform.createTransformedShape(this.getShape());
226     		shape.setShape(offsetCopiedShape);
227     		return shape;
228     	}
229     
230     	@Override
231     	public void redo() {
232     		create();
233     	}
234     
235     	@Override
236     	public void selectMaxandMin(Point start, Point end) {
237     		// TODO Auto-generated method stub
238     
239     	}
240     
241     	public void setGraphics2d(Graphics2D graphics2d) {
242     		this.graphics2d = graphics2d;
243     	}
244     
245     	@Override
246     	public void setPrimaryColor(ShapeColor primaryColor) {
247     		// TODO Auto-generated method stub
248     
249     	}
250     
251     	@Override
252     	public void setSecondaryColor(ShapeColor secondaryColor) {
253     		// TODO Auto-generated method stub
254     
255     	}
256     
257     	@Override
258     	public void setShadingColor(ShapeShadingType shapeShadingType) {
259     		// TODO Auto-generated method stub
260     
261     	}
262     
263     	public void setShape(Shape shape) {
264     		this.shape = shape;
265     		updateShapeProperty(shape);
266     	}
267     
268     	public void setShapeProperty(GeometricShape geometricShape) {
269     		this.geometricShape = geometricShape;
270     	}
271     
272     	@Override
273     	public void setStroke(Stroke stroke) {
274     		// TODO Auto-generated method stub
275     
276     	}
277     
278     	@Override
279     	public void undo() {
280     		deleteShape();
281     	}
282     
283     	public void updateShapeProperty(Shape shp) {
284     		Rectangle rect = shp.getBounds();
285     		Point ptStart = new Point(rect.x, rect.y);
286     		Point ptEnd = new Point((rect.width + rect.x), (rect.height + rect.y));
287     
288     		this.geometricShape.setStartPoint(ptStart);
289     		this.geometricShape.setEndPoint(ptEnd);
290     	}
291     }
292