support for line recognition

recognize deprecated properties
This commit is contained in:
bancika
2011-01-08 18:22:36 +00:00
parent f3bc414676
commit 4dd470f185
4 changed files with 49 additions and 8 deletions
@@ -13,6 +13,7 @@ import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
import org.diylc.utils.Constants;
/**
@@ -43,6 +44,8 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
this.height = getDefaultHeight().clone();
} catch (CloneNotSupportedException e) {
// This should never happen because Size supports cloning.
} catch (NullPointerException e) {
// This will happen if components do not have any shape.
}
}
@@ -74,6 +77,20 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
*/
protected abstract Color getBorderColor();
/**
* @return default lead thickness. Override this method to change it.
*/
protected Size getLeadThickness() {
return new Size(0.5d, SizeUnit.mm);
}
/**
* @return default lead color. Override this method to change it.
*/
protected Color getLeadColor() {
return LEAD_COLOR;
}
@Override
public int getControlPointCount() {
return points.length;
@@ -91,14 +108,17 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
@Override
public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
g2d.setColor(LEAD_COLOR);
g2d.setStroke(new BasicStroke(1));
g2d.setColor(getLeadColor());
g2d.setStroke(new BasicStroke(getLeadThickness().convertToPixels()));
double distance = points[0].distance(points[1]);
Shape shape = getComponentShape();
if (shape == null) {
g2d.drawLine(points[0].x, points[0].y, points[1].x, points[1].y);
return;
}
Rectangle shapeRect = shape.getBounds();
double leadLenght = (distance - shapeRect.width) / 2;
Double theta;
theta = Math.atan2(points[1].y - points[0].y, points[1].x - points[0].x);
Double theta = Math.atan2(points[1].y - points[0].y, points[1].x - points[0].x);
// Draw leads.
g2d.drawLine(points[0].x, points[0].y, (int) (points[0].x + leadLenght * Math.cos(theta)),
(int) (points[0].y + leadLenght * Math.sin(theta)));
@@ -114,6 +134,7 @@ public abstract class AbstractLeadedDIYComponent<T> implements IDIYComponent<T>
g2d.setColor(getBodyColor());
g2d.fill(shape);
}
g2d.setStroke(new BasicStroke(1));
g2d.setColor(getBorderColor());
g2d.draw(shape);
// Draw label.
@@ -56,7 +56,8 @@ public class ComponentProcessor {
for (Method method : clazz.getMethods()) {
if (method.getName().startsWith("get")) {
try {
if (method.isAnnotationPresent(EditableProperty.class)) {
if (method.isAnnotationPresent(EditableProperty.class)
&& !method.isAnnotationPresent(Deprecated.class)) {
EditableProperty annotation = method.getAnnotation(EditableProperty.class);
String name;
if (annotation.name().equals("")) {
@@ -64,8 +65,8 @@ public class ComponentProcessor {
} else {
name = annotation.name();
}
Method setter = clazz.getMethod("set" + method.getName().substring(3), method
.getReturnType());
Method setter = clazz.getMethod("set" + method.getName().substring(3),
method.getReturnType());
PropertyWrapper property = new PropertyWrapper(name,
method.getReturnType(), method, setter, annotation.defaultable());
properties.add(property);
@@ -1,5 +1,6 @@
package org.diylc.presenter;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
@@ -21,6 +22,7 @@ import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
@@ -41,6 +43,8 @@ import java.util.Map;
*/
class G2DWrapper extends Graphics2D {
public static int LINE_SENSITIVITY_MARGIN = 2;
private Graphics2D canvasGraphics;
private Stroke originalStroke;
private Color originalColor;
@@ -375,6 +379,21 @@ class G2DWrapper extends Graphics2D {
@Override
public void drawLine(int x1, int y1, int x2, int y2) {
canvasGraphics.drawLine(x1, y1, x2, y2);
Double theta = Math.atan2(y2 - y1, x2 - x1);
double width = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
double height;
if (getStroke() instanceof BasicStroke) {
height = ((BasicStroke) getStroke()).getLineWidth() + 2 * LINE_SENSITIVITY_MARGIN;
} else {
height = 2 * LINE_SENSITIVITY_MARGIN;
}
int midX = (x1 + x2) / 2;
int midY = (y1 + y2) / 2;
Rectangle2D rect = new Rectangle2D.Double(midX - width / 2, midY - height / 2, width,
height);
Area area = new Area(rect);
area.transform(AffineTransform.getRotateInstance(theta, midX, midY));
appendShape(area);
}
@Override
@@ -416,7 +435,7 @@ class G2DWrapper extends Graphics2D {
canvasGraphics.fillPolygon(points, points2, points3);
appendShape(new Polygon(points, points2, points3));
}
@Override
public void drawRect(int x, int y, int width, int height) {
canvasGraphics.drawRect(x, y, width, height);