TextRegion.java

00001 package edu.stanford.hci.r3.paper.regions;
00002 
00003 import java.awt.Color;
00004 import java.awt.Dimension;
00005 import java.awt.Font;
00006 import java.awt.geom.Rectangle2D;
00007 
00008 import edu.stanford.hci.r3.paper.Region;
00009 import edu.stanford.hci.r3.render.RegionRenderer;
00010 import edu.stanford.hci.r3.render.regions.TextRenderer;
00011 import edu.stanford.hci.r3.units.Points;
00012 import edu.stanford.hci.r3.units.Units;
00013 import edu.stanford.hci.r3.util.StringUtils;
00014 
00026 public class TextRegion extends Region {
00027 
00031         private Rectangle2D bounds;
00032 
00036         private Color color = new Color(100, 100, 100, 128);
00037 
00041         private Font font;
00042 
00046         private Points heightInPoints;
00047 
00051         private String[] lines;
00052 
00056         private Units originX;
00057 
00061         private Units originY;
00062 
00066         private String text;
00067 
00068         private Points widthInPoints;
00069 
00075         private boolean isLineWrapped = false;
00076         
00081         private int maxLines = -1;
00082         
00094         public TextRegion(String name, String theText, Font theFont, Units origX, Units origY) {
00095                 super(name, origX);
00096                 text = theText;
00097                 font = theFont;
00098 
00099                 originX = origX;
00100                 originY = origY;
00101 
00102                 lines = theText.split("\n");
00103 
00104                 // determine the font's boundaries
00105                 // represent it as a Rectangle (x, y, w, h)
00106                 final Dimension stringSize = StringUtils.getStringSize(text, font);
00107                 heightInPoints = new Points(stringSize.getHeight());
00108                 widthInPoints = new Points(stringSize.getWidth());
00109                 final Rectangle2D rect = new Rectangle2D.Double(origX.getValue(), origY
00110                                 .getValueIn(referenceUnits), widthInPoints.getValueIn(referenceUnits),
00111                                 heightInPoints.getValueIn(referenceUnits));
00112                 bounds = rect;
00113                 setShape(rect);
00114         }
00115 
00127         public TextRegion(String name, String theText, Font theFont, Units origX, Units origY,
00128                         Units width, Units height) {
00129                 super(name, origX);
00130                 text = theText;
00131                 font = theFont;
00132 
00133                 originX = origX;
00134                 originY = origY;
00135 
00136                 lines = theText.split("\n");
00137 
00138                 // determine the font's boundaries
00139                 // represent it as a Rectangle (x, y, w, h)
00140                 final Dimension stringSize = StringUtils.getStringSize(text, font);
00141                 heightInPoints = new Points(stringSize.getHeight());
00142                 widthInPoints = new Points(stringSize.getWidth());
00143                 final Rectangle2D rect = new Rectangle2D.Double(origX.getValue(), origY
00144                                 .getValueIn(referenceUnits), width.getValueIn(referenceUnits), height
00145                                 .getValueIn(referenceUnits));
00146                 bounds = rect;
00147                 setShape(rect);
00148         }
00149 
00153         public Color getColor() {
00154                 return color;
00155         }
00156 
00160         public Font getFont() {
00161                 return font;
00162         }
00163 
00167         public String[] getLinesOfText() {
00168                 return lines;
00169         }
00170 
00174         public RegionRenderer getRenderer() {
00175                 return new TextRenderer(this);
00176         }
00177 
00181         public String getText() {
00182                 return text;
00183         }
00184 
00188         public Units getX() {
00189                 return originX;
00190         }
00191 
00195         public Units getY() {
00196                 return originY;
00197         }
00198 
00202         public void setColor(Color c) {
00203                 color = c;
00204         }
00205 
00210         public void setLineWrapped(boolean b) {
00211                 isLineWrapped = b;
00212         }
00213         
00218         public boolean isLineWrapped() {
00219                 return isLineWrapped;
00220         }
00221         
00225         public void setMaxLines(int i) {
00226                 maxLines = i;
00227         }
00228         
00232         public int getMaxLines() {
00233                 return maxLines;
00234         }
00235         
00239         public String toString() {
00240                 return "Text: {" + text + "} " + font.getSize() + "pt " + font.getName()
00241                                 + " at Bounds: [x=" + originX.getValue() + " y=" + originY.getValue() + " w="
00242                                 + bounds.getWidth() + " h=" + bounds.getHeight() + "] in "
00243                                 + referenceUnits.getUnitName();
00244         }
00245 }

Generated on Sat Apr 14 18:21:39 2007 for R3 Paper Toolkit by  doxygen 1.4.7