| 1 |
/* |
| 2 |
* DiTAA - Diagrams Through Ascii Art |
| 3 |
* |
| 4 |
* Copyright (C) 2004 Efstathios Sideris |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.stathissideris.ascii2image.graphics; |
| 22 |
|
| 23 |
import java.awt.Color; |
| 24 |
import java.awt.Font; |
| 25 |
import java.awt.geom.Rectangle2D; |
| 26 |
|
| 27 |
/** |
| 28 |
* |
| 29 |
* @author Efstathios Sideris |
| 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 |
* @return |
| 79 |
*/ |
| 80 |
public Color getColor() { |
| 81 |
return color; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @return |
| 86 |
*/ |
| 87 |
public Font getFont() { |
| 88 |
return font; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @return |
| 93 |
*/ |
| 94 |
public String getText() { |
| 95 |
return text; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @return |
| 100 |
*/ |
| 101 |
public int getXPos() { |
| 102 |
return xPos; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @return |
| 107 |
*/ |
| 108 |
public int getYPos() { |
| 109 |
return yPos; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param color |
| 114 |
*/ |
| 115 |
public void setColor(Color color) { |
| 116 |
this.color = color; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @param font |
| 121 |
*/ |
| 122 |
public void setFont(Font font) { |
| 123 |
this.font = font; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @param string |
| 128 |
*/ |
| 129 |
public void setText(String string) { |
| 130 |
text = string; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* @param i |
| 135 |
*/ |
| 136 |
public void setXPos(int i) { |
| 137 |
xPos = i; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @param i |
| 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 |
* @return |
| 163 |
*/ |
| 164 |
public boolean isTextOnLine() { |
| 165 |
return isTextOnLine; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @param b |
| 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 |
} |