File: /Users/shatabdi/Education/GitHub/se450_project/src/model/others/BoundingBox.java

1     package model.others;
2     
3     import java.awt.AlphaComposite;
4     import java.awt.BasicStroke;
5     import java.awt.Color;
6     import java.awt.Font;
7     import java.awt.Graphics2D;
8     import java.awt.Point;
9     import java.awt.Rectangle;
10     import java.awt.Shape;
11     import java.awt.Stroke;
12     import java.util.ArrayList;
13     import java.util.Collections;
14     import java.util.List;
15     
16     import model.GeometricShape;
17     import model.factory.ShapeFactory;
18     import view.Enum.ShapeType;
19     
20     public class BoundingBox {
21     
22     	public static Shape boundFromPoints(Point startPoint, Point endPoint) {
23     		GeometricShape geometricShape = new GeometricShape(startPoint, endPoint);
24     		geometricShape.setShapeType(ShapeType.RECTANGLE);
25     		return ShapeFactory.createShape(geometricShape);
26     	}
27     
28     	/**
29     	 * Discussion board replies Top left of bounding box is (x, y) Bottom right is
30     	 * (x+Width, y+Height) Width of a bounding box is xMax - xMin Height is given by
31     	 * yMax - yMin Bounding box = (xMin, yMin, xMax - xMin, yMax - yMin);
32     	 */
33     	public static Shape createBoundingBox(List<Shape> list) {
34     			List<Integer> listStartXCoord = new ArrayList<>();
35     			List<Integer> listStartYCoord = new ArrayList<>();
36     
37     			List<Integer> listEndXCoord = new ArrayList<>();
38     			List<Integer> listEndYCoord = new ArrayList<>();
39     
40     			for (Shape shape : list) {
41     				Rectangle shapeBoundingRec = shape.getBounds();
42     				listStartXCoord.add(shapeBoundingRec.x);
43     				listStartYCoord.add(shapeBoundingRec.y);
44     
45     				listEndXCoord.add(shapeBoundingRec.x + shapeBoundingRec.width);
46     				listEndYCoord.add(shapeBoundingRec.y + shapeBoundingRec.height);
47     			}
48     
49     			int xMin = Collections.min(listStartXCoord);
50     			int yMin = Collections.min(listStartYCoord);
51     			int xMax = Collections.max(listEndXCoord);
52     			int yMax = Collections.max(listEndYCoord);
53     
54     			Point startPoint = new Point(xMin, yMin);
55     			Point endPoint = new Point(xMax, yMax);
56     			return boundFromPoints(startPoint, endPoint);
57     	}
58     
59     	private Shape boundingBox;
60     	
61     	public void drawBoundingBox(Graphics2D graphics2d) {
62     		Stroke dashed_outline = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 1, new float[] { 9 },
63     				0);
64     
65     		graphics2d.setStroke(dashed_outline);
66     		graphics2d.setPaint(Color.green);
67     		graphics2d.setColor(Color.black);
68     		graphics2d.setFont(new Font(Messages.getString("BoundingBox.0"), Font.BOLD, 16)); //$NON-NLS-1$
69     		graphics2d.draw(boundingBox);
70     		graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f));
71     
72     		graphics2d.drawString(Messages.getString("BoundingBox.1"), boundingBox.getBounds().x, boundingBox.getBounds().y); //$NON-NLS-1$
73     	}
74     	
75     	public Shape generateFromPoints(Point startPoint, Point endPoint) {
76     		this.boundingBox = boundFromPoints(startPoint, endPoint);
77     		return this.boundingBox;
78     	}
79     }
80