board coordinate improvements

This commit is contained in:
Branislav Stojkovic
2019-01-20 10:30:19 +01:00
parent cb21392d1e
commit 32b2713732
2 changed files with 101 additions and 57 deletions
@@ -60,7 +60,9 @@ public abstract class AbstractBoard extends AbstractTransparentComponent<String>
protected Color borderColor = BORDER_COLOR; protected Color borderColor = BORDER_COLOR;
protected Color coordinateColor = COORDINATE_COLOR; protected Color coordinateColor = COORDINATE_COLOR;
protected Boolean drawCoordinates = null; protected Boolean drawCoordinates = null;
protected CoordinateType coordinateType = CoordinateType.XY; protected CoordinateType xType = CoordinateType.Numbers;
protected CoordinateDisplay coordinateDisplay = CoordinateDisplay.One_Side;
protected CoordinateType yType = CoordinateType.Letters;
@Override @Override
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project, public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project,
@@ -84,30 +86,40 @@ public abstract class AbstractBoard extends AbstractTransparentComponent<String>
} }
protected void drawCoordinates(Graphics2D g2d, int spacing, Project project) { protected void drawCoordinates(Graphics2D g2d, int spacing, Project project) {
CoordinateType ct = getCoordinateType();
if (ct == CoordinateType.None)
return;
Point p = new Point(firstPoint);
g2d.setColor(coordinateColor); g2d.setColor(coordinateColor);
g2d.setFont(project.getFont().deriveFont(COORDINATE_FONT_SIZE)); g2d.setFont(project.getFont().deriveFont(COORDINATE_FONT_SIZE));
if (getCoordinateDisplay() != CoordinateDisplay.None) {
Point p = new Point(firstPoint);
int t = 1; int t = 1;
while (p.y < secondPoint.y - spacing) { while (p.y < secondPoint.y - spacing) {
p.y += spacing; p.y += spacing;
super.drawCenteredText(g2d, ct == CoordinateType.XY ? getCoordinateLabel(t) : Integer.toString(t), p.x + (ct == CoordinateType.YX && t >= 10 ? 0 : 2), p.y, HorizontalAlignment.LEFT, int offset = (getyType() == CoordinateType.Numbers && t >= 10) || (getyType() == CoordinateType.Letters && t >= 27) ? 0 : 2;
drawCenteredText(g2d, getyType() == CoordinateType.Letters ? getCoordinateLabel(t) : Integer.toString(t), p.x + offset, p.y, HorizontalAlignment.LEFT,
VerticalAlignment.CENTER); VerticalAlignment.CENTER);
if (getCoordinateDisplay() == CoordinateDisplay.Both_Sides) {
drawCenteredText(g2d, getyType() == CoordinateType.Letters ? getCoordinateLabel(t) : Integer.toString(t), secondPoint.x - offset, p.y, HorizontalAlignment.RIGHT,
VerticalAlignment.CENTER);
}
t++; t++;
} }
}
p = new Point(firstPoint); if (getCoordinateDisplay() != CoordinateDisplay.None) {
t = 1; Point p = new Point(firstPoint);
int t = 1;
while (p.x < secondPoint.x - spacing) { while (p.x < secondPoint.x - spacing) {
p.x += spacing; p.x += spacing;
super.drawCenteredText(g2d, ct == CoordinateType.XY ? Integer.toString(t) : getCoordinateLabel(t), p.x, p.y - 2, HorizontalAlignment.CENTER, drawCenteredText(g2d, getxType() == CoordinateType.Letters ? getCoordinateLabel(t) : Integer.toString(t), p.x, p.y - 2, HorizontalAlignment.CENTER,
VerticalAlignment.TOP); VerticalAlignment.TOP);
if (getCoordinateDisplay() == CoordinateDisplay.Both_Sides) {
drawCenteredText(g2d, getxType() == CoordinateType.Letters ? getCoordinateLabel(t) : Integer.toString(t), p.x, (int) (secondPoint.y - COORDINATE_FONT_SIZE), HorizontalAlignment.CENTER,
VerticalAlignment.BOTTOM);
}
t++; t++;
} }
} }
}
private String getCoordinateLabel(int coordinate) { private String getCoordinateLabel(int coordinate) {
String result = ""; String result = "";
@@ -152,15 +164,37 @@ public abstract class AbstractBoard extends AbstractTransparentComponent<String>
this.borderColor = borderColor; this.borderColor = borderColor;
} }
@EditableProperty(name ="Coordinates") @EditableProperty(name = "X")
public CoordinateType getCoordinateType() { public CoordinateType getxType() {
if (coordinateType == null) if (xType == null)
coordinateType = drawCoordinates == null || drawCoordinates ? CoordinateType.XY : CoordinateType.None; xType = CoordinateType.Numbers;
return coordinateType; return xType;
} }
public void setCoordinateType(CoordinateType coordinateType) { public void setxType(CoordinateType xType) {
this.coordinateType = coordinateType; this.xType = xType;
}
@EditableProperty(name = "Coordinates")
public CoordinateDisplay getCoordinateDisplay() {
if (coordinateDisplay == null)
coordinateDisplay = CoordinateDisplay.One_Side;
return coordinateDisplay;
}
public void setCoordinateDisplay(CoordinateDisplay coordinateDisplay) {
this.coordinateDisplay = coordinateDisplay;
}
@EditableProperty(name = "Y")
public CoordinateType getyType() {
if (yType == null)
yType = CoordinateType.Letters;
return yType;
}
public void setyType(CoordinateType yType) {
this.yType = yType;
} }
@Override @Override
@@ -204,17 +238,15 @@ public abstract class AbstractBoard extends AbstractTransparentComponent<String>
} }
public static enum CoordinateType { public static enum CoordinateType {
None("None"), XY("X-numbers Y-letters"), YX("X-letters Y-numbers"); Letters, Numbers
private String label;
private CoordinateType(String label) {
this.label = label;
} }
public static enum CoordinateDisplay {
None, One_Side, Both_Sides;
@Override @Override
public String toString() { public String toString() {
return label; return super.toString().replace('_', ' ');
} };
} }
} }
@@ -89,9 +89,21 @@ public class BlankBoard extends AbstractBoard {
} }
@Override @Override
public CoordinateType getCoordinateType() { public CoordinateType getxType() {
// Override to prevent editing. // Override to prevent editing.
return super.getCoordinateType(); return super.getxType();
}
@Override
public CoordinateDisplay getCoordinateDisplay() {
// Override to prevent editing.
return super.getCoordinateDisplay();
}
@Override
public CoordinateType getyType() {
// Override to prevent editing.
return super.getyType();
} }
@Override @Override