| •1 |
|
| 2 |
|
| 3 |
|
| •4 |
|
| 5 |
|
| •6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| •11 |
|
| 12 |
|
| 13 |
|
| •14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package org.stathissideris.ascii2image.graphics; |
| 21 |
|
| 22 |
import java.awt.Color; |
| 23 |
import java.awt.Font; |
| •24 |
import java.awt.Rectangle; |
| 25 |
import java.awt.geom.Rectangle2D; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class DiagramText extends DiagramComponent { |
| 32 |
|
| 33 |
public static final Color DEFAULT_COLOR = Color.black; |
| 34 |
|
| 35 |
private String text; |
| 36 |
private Font font; |
| 37 |
private int xPos, yPos; |
| 38 |
private Color color = Color.black; |
| 39 |
private boolean isTextOnLine = false; |
| 40 |
private boolean hasOutline = false; |
| 41 |
private Color outlineColor = Color.white; |
| 42 |
|
| 43 |
public DiagramText(int x, int y, String text, Font font){ |
| 44 |
if(text == null) throw new IllegalArgumentException("DiagramText cannot be initialised with a null string"); |
| 45 |
if(font == null) throw new IllegalArgumentException("DiagramText cannot be initialised with a null font"); |
| 46 |
|
| 47 |
this.xPos = x; |
| 48 |
this.yPos = y; |
| 49 |
this.text = text; |
| 50 |
this.font = font; |
| 51 |
} |
| 52 |
|
| 53 |
public void centerInBounds(Rectangle2D bounds){ |
| 54 |
centerHorizontallyBetween((int) bounds.getMinX(), (int) bounds.getMaxX()); |
| 55 |
centerVerticallyBetween((int) bounds.getMinY(), (int) bounds.getMaxY()); |
| 56 |
} |
| 57 |
|
| 58 |
public void centerHorizontallyBetween(int minX, int maxX){ |
| 59 |
int width = FontMeasurer.instance().getWidthFor(text, font); |
| 60 |
int center = Math.abs(maxX - minX) / 2; |
| 61 |
xPos += Math.abs(center - width / 2); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
public void centerVerticallyBetween(int minY, int maxY){ |
| 66 |
int zHeight = FontMeasurer.instance().getZHeight(font); |
| 67 |
int center = Math.abs(maxY - minY) / 2; |
| 68 |
yPos -= Math.abs(center - zHeight / 2); |
| 69 |
} |
| 70 |
|
| 71 |
public void alignRightEdgeTo(int x){ |
| 72 |
int width = FontMeasurer.instance().getWidthFor(text, font); |
| 73 |
xPos = x - width; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public Color getColor() { |
| 81 |
return color; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public Font getFont() { |
| 88 |
return font; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public String getText() { |
| 95 |
return text; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public int getXPos() { |
| 102 |
return xPos; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public int getYPos() { |
| 109 |
return yPos; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public void setColor(Color color) { |
| 116 |
this.color = color; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public void setFont(Font font) { |
| 123 |
this.font = font; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public void setText(String string) { |
| 130 |
text = string; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public void setXPos(int i) { |
| 137 |
xPos = i; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public void setYPos(int i) { |
| 144 |
yPos = i; |
| 145 |
} |
| 146 |
|
| 147 |
public Rectangle2D getBounds(){ |
| 148 |
Rectangle2D bounds = FontMeasurer.instance().getBoundsFor(text, font); |
| 149 |
bounds.setRect( |
| 150 |
bounds.getMinX() + xPos, |
| 151 |
bounds.getMinY() + yPos, |
| 152 |
bounds.getWidth(), |
| 153 |
bounds.getHeight()); |
| 154 |
return bounds; |
| 155 |
} |
| 156 |
|
| 157 |
public String toString(){ |
| 158 |
return "DiagramText, at ("+xPos+", "+yPos+"), within "+getBounds()+" '"+text+"', "+color+" "+font; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public boolean isTextOnLine() { |
| 165 |
return isTextOnLine; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public void setTextOnLine(boolean b) { |
| 172 |
isTextOnLine = b; |
| 173 |
} |
| 174 |
|
| 175 |
public boolean hasOutline() { |
| 176 |
return hasOutline; |
| 177 |
} |
| 178 |
|
| 179 |
public void setHasOutline(boolean hasOutline) { |
| 180 |
this.hasOutline = hasOutline; |
| 181 |
} |
| 182 |
|
| 183 |
public Color getOutlineColor() { |
| 184 |
return outlineColor; |
| 185 |
} |
| 186 |
|
| 187 |
public void setOutlineColor(Color outlineColor) { |
| 188 |
this.outlineColor = outlineColor; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
} |