Skip to content

Commit

Permalink
Make RectangleAnchor an enumeration.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfree committed May 14, 2017
1 parent 3ffb749 commit 7674b09
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2016, by Object Refinery Limited and Contributors.
* (C) Copyright 2000-2017, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
Expand Down Expand Up @@ -221,8 +221,7 @@ else if (orientation == PlotOrientation.VERTICAL) {
int h = this.image.getHeight(null);

Rectangle2D imageRect = new Rectangle2D.Double(0, 0, w, h);
Point2D anchorPoint = RectangleAnchor.coordinates(imageRect,
this.anchor);
Point2D anchorPoint = this.anchor.getAnchorPoint(imageRect);
xx = xx - (float) anchorPoint.getX();
yy = yy - (float) anchorPoint.getY();
g2.drawImage(this.image, (int) xx, (int) yy, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2016, by Object Refinery Limited and Contributors.
* (C) Copyright 2000-2017, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
Expand Down Expand Up @@ -300,8 +300,7 @@ else if (orientation == PlotOrientation.VERTICAL) {
Size2D size = this.title.arrange(g2, rc);
Rectangle2D titleRect = new Rectangle2D.Double(0, 0, size.width,
size.height);
Point2D anchorPoint = RectangleAnchor.coordinates(titleRect,
this.anchor);
Point2D anchorPoint = this.anchor.getAnchorPoint(titleRect);
xx = xx - (float) anchorPoint.getX();
yy = yy - (float) anchorPoint.getY();
titleRect.setRect(xx, yy, titleRect.getWidth(), titleRect.getHeight());
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jfree/chart/axis/CategoryAxis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1069,8 +1069,7 @@ else if (edge == RectangleEdge.RIGHT) {
}
Rectangle2D area = new Rectangle2D.Double(x0, y0, (x1 - x0),
(y1 - y0));
Point2D anchorPoint = RectangleAnchor.coordinates(area,
position.getCategoryAnchor());
Point2D anchorPoint = position.getCategoryAnchor().getAnchorPoint(area);
TextBlock block = tick.getLabel();
block.draw(g2, (float) anchorPoint.getX(),
(float) anchorPoint.getY(), position.getLabelAnchor(),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jfree/chart/block/LabelBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
}
g2.setPaint(this.paint);
g2.setFont(this.font);
Point2D pt = RectangleAnchor.coordinates(area, this.textAnchor);
Point2D pt = this.textAnchor.getAnchorPoint(area);
this.label.draw(g2, (float) pt.getX(), (float) pt.getY(),
this.contentAlignmentPoint);
BlockResult result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
Shape savedClip = g2.getClip();
g2.clip(fb);

Point2D pt2 = RectangleAnchor.coordinates(bounds, this.valueAnchor);
Point2D pt2 = this.valueAnchor.getAnchorPoint(bounds);
g2.setPaint(this.paint);
g2.setFont(this.font);
TextUtils.drawAlignedString(valueStr, g2, (float) pt2.getX(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ protected Point2D calculateDomainMarkerTextAnchorPoint(Graphics2D g2,
anchorRect = markerOffset.createAdjustedRectangle(markerArea,
labelOffsetType, LengthAdjustmentType.CONTRACT);
}
return RectangleAnchor.coordinates(anchorRect, anchor);
return anchor.getAnchorPoint(anchorRect);
}

/**
Expand Down Expand Up @@ -1229,7 +1229,7 @@ protected Point2D calculateRangeMarkerTextAnchorPoint(Graphics2D g2,
anchorRect = markerOffset.createAdjustedRectangle(markerArea,
LengthAdjustmentType.CONTRACT, labelOffsetType);
}
return RectangleAnchor.coordinates(anchorRect, anchor);
return anchor.getAnchorPoint(anchorRect);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ else if (orientation == PlotOrientation.VERTICAL) {
anchorRect = markerOffset.createAdjustedRectangle(markerArea,
labelOffsetType, LengthAdjustmentType.CONTRACT);
}
return RectangleAnchor.coordinates(anchorRect, anchor);
return anchor.getAnchorPoint(anchorRect);

}

Expand Down Expand Up @@ -1442,7 +1442,7 @@ else if (orientation == PlotOrientation.VERTICAL) {
anchorRect = markerOffset.createAdjustedRectangle(markerArea,
LengthAdjustmentType.CONTRACT, labelOffsetForRange);
}
return RectangleAnchor.coordinates(anchorRect, anchor);
return anchor.getAnchorPoint(anchorRect);

}

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/jfree/chart/title/LegendGraphic.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2016, by Object Refinery Limited and Contributors.
* (C) Copyright 2000-2017, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
Expand Down Expand Up @@ -580,8 +580,7 @@ public void draw(Graphics2D g2, Rectangle2D area) {
area = trimPadding(area);

if (this.lineVisible) {
Point2D location = RectangleAnchor.coordinates(area,
this.shapeLocation);
Point2D location = this.shapeLocation.getAnchorPoint(area);
Shape aLine = ShapeUtils.createTranslatedShape(getLine(),
this.shapeAnchor, location.getX(), location.getY());
g2.setPaint(this.linePaint);
Expand All @@ -590,8 +589,7 @@ public void draw(Graphics2D g2, Rectangle2D area) {
}

if (this.shapeVisible) {
Point2D location = RectangleAnchor.coordinates(area,
this.shapeLocation);
Point2D location = this.shapeLocation.getAnchorPoint(area);

Shape s = ShapeUtils.createTranslatedShape(this.shape,
this.shapeAnchor, location.getX(), location.getY());
Expand Down
173 changes: 38 additions & 135 deletions src/main/java/org/jfree/chart/ui/RectangleAnchor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,42 @@

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.ObjectStreamException;
import java.io.Serializable;
import org.jfree.chart.util.Args;

/**
* Used to indicate an anchor point for a rectangle.
*/
public final class RectangleAnchor implements Serializable {
public enum RectangleAnchor {

/** For serialization. */
private static final long serialVersionUID = -2457494205644416327L;

/** Center. */
public static final RectangleAnchor CENTER
= new RectangleAnchor("RectangleAnchor.CENTER");
CENTER("RectangleAnchor.CENTER"),

/** Top. */
public static final RectangleAnchor TOP
= new RectangleAnchor("RectangleAnchor.TOP");
TOP("RectangleAnchor.TOP"),

/** Top-Left. */
public static final RectangleAnchor TOP_LEFT
= new RectangleAnchor("RectangleAnchor.TOP_LEFT");
TOP_LEFT("RectangleAnchor.TOP_LEFT"),

/** Top-Right. */
public static final RectangleAnchor TOP_RIGHT
= new RectangleAnchor("RectangleAnchor.TOP_RIGHT");
TOP_RIGHT("RectangleAnchor.TOP_RIGHT"),

/** Bottom. */
public static final RectangleAnchor BOTTOM
= new RectangleAnchor("RectangleAnchor.BOTTOM");
BOTTOM("RectangleAnchor.BOTTOM"),

/** Bottom-Left. */
public static final RectangleAnchor BOTTOM_LEFT
= new RectangleAnchor("RectangleAnchor.BOTTOM_LEFT");
BOTTOM_LEFT("RectangleAnchor.BOTTOM_LEFT"),

/** Bottom-Right. */
public static final RectangleAnchor BOTTOM_RIGHT
= new RectangleAnchor("RectangleAnchor.BOTTOM_RIGHT");
BOTTOM_RIGHT("RectangleAnchor.BOTTOM_RIGHT"),

/** Left. */
public static final RectangleAnchor LEFT
= new RectangleAnchor("RectangleAnchor.LEFT");
LEFT("RectangleAnchor.LEFT"),

/** Right. */
public static final RectangleAnchor RIGHT
= new RectangleAnchor("RectangleAnchor.RIGHT");
RIGHT("RectangleAnchor.RIGHT");

/** The name. */
private String name;
private final String name;

/**
* Private constructor.
Expand All @@ -90,90 +77,46 @@ private RectangleAnchor(String name) {
}

/**
* Returns a string representing the object.
*
* @return The string.
*/
public String toString() {
return this.name;
}

/**
* Returns {@code true} if this object is equal to the specified
* object, and {@code false} otherwise.
*
* @param obj the other object ({@code null} permitted).
*
* @return A boolean.
*/
@Override
public boolean equals(Object obj) {

if (this == obj) {
return true;
}
if (!(obj instanceof RectangleAnchor)) {
return false;
}

RectangleAnchor order = (RectangleAnchor) obj;
if (!this.name.equals(order.name)) {
return false;
}

return true;
}

/**
* Returns a hash code value for the object.
*
* @return The hashcode
*/
@Override
public int hashCode() {
return this.name.hashCode();
}

/**
* Returns the (x, y) coordinates of the specified anchor.
* Returns the anchor point relative to the specified rectangle.
*
* @param rectangle the rectangle.
* @param anchor the anchor.
* @param rectangle the rectangle (<code>null</code> not permitted).
*
* @return The (x, y) coordinates.
* @return The anchor point (never <code>null</code>).
*/
public static Point2D coordinates(Rectangle2D rectangle,
RectangleAnchor anchor) {
public Point2D getAnchorPoint(Rectangle2D rectangle) {
Args.nullNotPermitted(rectangle, "rectangle");
Point2D result = new Point2D.Double();
if (anchor == RectangleAnchor.CENTER) {
if (this == RectangleAnchor.CENTER) {
result.setLocation(rectangle.getCenterX(), rectangle.getCenterY());
}
else if (anchor == RectangleAnchor.TOP) {
} else if (this == RectangleAnchor.TOP) {
result.setLocation(rectangle.getCenterX(), rectangle.getMinY());
}
else if (anchor == RectangleAnchor.BOTTOM) {
} else if (this == RectangleAnchor.BOTTOM) {
result.setLocation(rectangle.getCenterX(), rectangle.getMaxY());
}
else if (anchor == RectangleAnchor.LEFT) {
} else if (this == RectangleAnchor.LEFT) {
result.setLocation(rectangle.getMinX(), rectangle.getCenterY());
}
else if (anchor == RectangleAnchor.RIGHT) {
} else if (this == RectangleAnchor.RIGHT) {
result.setLocation(rectangle.getMaxX(), rectangle.getCenterY());
}
else if (anchor == RectangleAnchor.TOP_LEFT) {
} else if (this == RectangleAnchor.TOP_LEFT) {
result.setLocation(rectangle.getMinX(), rectangle.getMinY());
}
else if (anchor == RectangleAnchor.TOP_RIGHT) {
} else if (this == RectangleAnchor.TOP_RIGHT) {
result.setLocation(rectangle.getMaxX(), rectangle.getMinY());
}
else if (anchor == RectangleAnchor.BOTTOM_LEFT) {
} else if (this == RectangleAnchor.BOTTOM_LEFT) {
result.setLocation(rectangle.getMinX(), rectangle.getMaxY());
}
else if (anchor == RectangleAnchor.BOTTOM_RIGHT) {
} else if (this == RectangleAnchor.BOTTOM_RIGHT) {
result.setLocation(rectangle.getMaxX(), rectangle.getMaxY());
}
return result;
}

/**
* Returns a string representing the object.
*
* @return The string.
*/
@Override
public String toString() {
return this.name;
}

/**
* Creates a new rectangle with the specified dimensions that is aligned to
Expand All @@ -189,8 +132,8 @@ else if (anchor == RectangleAnchor.BOTTOM_RIGHT) {
public static Rectangle2D createRectangle(Size2D dimensions,
double anchorX, double anchorY, RectangleAnchor anchor) {
Rectangle2D result = null;
final double w = dimensions.getWidth();
final double h = dimensions.getHeight();
double w = dimensions.getWidth();
double h = dimensions.getHeight();
if (anchor == RectangleAnchor.CENTER) {
result = new Rectangle2D.Double(anchorX - w / 2.0,
anchorY - h / 2.0, w, h);
Expand All @@ -216,44 +159,4 @@ public static Rectangle2D createRectangle(Size2D dimensions,
return result;
}

/**
* Ensures that serialization returns the unique instances.
*
* @return The object.
*
* @throws ObjectStreamException if there is a problem.
*/
private Object readResolve() throws ObjectStreamException {
RectangleAnchor result = null;
if (this.equals(RectangleAnchor.CENTER)) {
result = RectangleAnchor.CENTER;
}
else if (this.equals(RectangleAnchor.TOP)) {
result = RectangleAnchor.TOP;
}
else if (this.equals(RectangleAnchor.BOTTOM)) {
result = RectangleAnchor.BOTTOM;
}
else if (this.equals(RectangleAnchor.LEFT)) {
result = RectangleAnchor.LEFT;
}
else if (this.equals(RectangleAnchor.RIGHT)) {
result = RectangleAnchor.RIGHT;
}
else if (this.equals(RectangleAnchor.TOP_LEFT)) {
result = RectangleAnchor.TOP_LEFT;
}
else if (this.equals(RectangleAnchor.TOP_RIGHT)) {
result = RectangleAnchor.TOP_RIGHT;
}
else if (this.equals(RectangleAnchor.BOTTOM_LEFT)) {
result = RectangleAnchor.BOTTOM_LEFT;
}
else if (this.equals(RectangleAnchor.BOTTOM_RIGHT)) {
result = RectangleAnchor.BOTTOM_RIGHT;
}
return result;
}

}

3 changes: 1 addition & 2 deletions src/main/java/org/jfree/chart/util/ShapeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ public static Shape createTranslatedShape(Shape shape,
if (anchor == null) {
throw new IllegalArgumentException("Null 'anchor' argument.");
}
Point2D anchorPoint = RectangleAnchor.coordinates(
shape.getBounds2D(), anchor);
Point2D anchorPoint = anchor.getAnchorPoint(shape.getBounds2D());
final AffineTransform transform = AffineTransform.getTranslateInstance(
locationX - anchorPoint.getX(), locationY - anchorPoint.getY());
return transform.createTransformedShape(shape);
Expand Down

0 comments on commit 7674b09

Please sign in to comment.