RegionRenderer.java

00001 package edu.stanford.hci.r3.render;
00002 
00003 import java.awt.BasicStroke;
00004 import java.awt.Color;
00005 import java.awt.Font;
00006 import java.awt.Graphics2D;
00007 import java.awt.font.FontRenderContext;
00008 import java.awt.geom.Rectangle2D;
00009 import java.util.List;
00010 
00011 import edu.stanford.hci.r3.config.Configuration;
00012 import edu.stanford.hci.r3.paper.Region;
00013 import edu.stanford.hci.r3.units.Points;
00014 import edu.stanford.hci.r3.units.Units;
00015 import edu.stanford.hci.r3.util.DebugUtils;
00016 import edu.stanford.hci.r3.util.MathUtils;
00017 import edu.stanford.hci.r3.util.StringUtils;
00018 
00030 public class RegionRenderer {
00031 
00035         public static final String CONFIG_FILE_KEY = "regionrenderer.debugregions.file";
00036 
00040         public static final String CONFIG_FILE_VALUE = "/config/RegionRenderer.xml";
00041 
00045         // public static final boolean DEBUG_REGIONS = true;
00046         public static final boolean DEBUG_REGIONS = readDebugFlagFromConfigFile();
00047 
00051         private static final Font FONT = new Font("Trebuchet MS", Font.PLAIN, 10);
00052 
00053         private static final BasicStroke OUTLINE = new BasicStroke(0);
00054 
00058         private static final String PROPERTY_NAME = "debugRegions";
00059 
00063         private static final Color REGION_COLOR = new Color(123, 123, 123, 30);
00064 
00068         private static final Color TEXT_COLOR = Color.BLACK;
00069 
00073         private static boolean readDebugFlagFromConfigFile() {
00074                 final String property = Configuration.getPropertyFromConfigFile(PROPERTY_NAME,
00075                                 CONFIG_FILE_KEY);
00076                 final boolean debug = Boolean.parseBoolean(property);
00077                 return debug;
00078         }
00079 
00080         protected Region region;
00081 
00085         public RegionRenderer(Region r) {
00086                 region = r;
00087         }
00088 
00094         public void renderToG2D(Graphics2D g2d) {
00095                 final Rectangle2D b = region.getUnscaledBounds2D();
00096 
00097                 final float scaleX = (float) region.getScaleX();
00098                 final float scaleY = (float) region.getScaleY();
00099 
00100                 final Units units = region.getUnits();
00101                 final double conv = units.getConversionTo(Points.ONE);
00102 
00103                 final float xPts = (float) Math.round(conv * b.getX());
00104                 final float yPts = (float) Math.round(conv * b.getY());
00105                 final float wPts = (float) Math.round(conv * b.getWidth());
00106                 final float hPts = (float) Math.round(conv * b.getHeight());
00107 
00108                 // x and y origins are NOT affected by the horiz/vert scaling factors
00109                 final int finalX = (int) Math.round(xPts);
00110                 final int finalY = (int) Math.round(yPts);
00111                 final int finalW = (int) Math.round(wPts * scaleX);
00112                 final int finalH = (int) Math.round(hPts * scaleY);
00113 
00114                 // handle different regions differently
00115 
00116                 g2d.setStroke(OUTLINE);
00117                 g2d.setColor(region.getStrokeColor());
00118                 g2d.drawRect(finalX, finalY, finalW, finalH);
00119 
00120                 final double opacity = region.getOpacity();
00121                 // should we render the fill of this region?
00122                 if (opacity > 0) {
00123                         Color fillColor = region.getFillColor();
00124                         g2d.setColor(new Color(fillColor.getRed(), fillColor.getGreen(), fillColor.getBlue(),
00125                                         MathUtils.rint(opacity * 255)));
00126                         g2d.fillRect(finalX, finalY, finalW, finalH);
00127                 }
00128 
00129                 if (DEBUG_REGIONS) { // draw some debug text and fill in the region
00130                         DebugUtils.println("Debugging regions in renderToG2D(...)");
00131                         g2d.setFont(FONT);
00132                         g2d.setColor(REGION_COLOR);
00133                         g2d.fillRect(finalX, finalY, finalW, finalH);
00134                         g2d.setColor(TEXT_COLOR);
00135                         final String regionString = region.toString();
00136 
00137                         final Rectangle2D stringBounds = FONT.getStringBounds(regionString,
00138                                         new FontRenderContext(null, true, true));
00139                         final double lineWidth = stringBounds.getWidth();
00140                         final int lineHeight = MathUtils.rint(stringBounds.getHeight());
00141                         final int lengthOfString = regionString.length();
00142 
00143                         if (finalW > lineWidth) {
00144                                 g2d.drawString(regionString, finalX, finalY + lineHeight);
00145                         } else {
00146 
00147                                 // the box is not wide enough
00148                                 final double fraction = finalW / lineWidth;
00149                                 final int maxCharsPerLine = (int) (fraction * lengthOfString);
00150 
00151                                 // split the string so it's more readable
00152                                 final List<String> lines = StringUtils.splitString(regionString, maxCharsPerLine);
00153                                 double yOffset = finalY + lineHeight;
00154                                 final int xOffset = finalX + 3;
00155                                 for (String line : lines) {
00156                                         g2d.drawString(line, xOffset, (int) Math.round(yOffset));
00157                                         yOffset += lineHeight;
00158                                 }
00159                         }
00160                 }
00161         }
00162 }

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