- Timestamp:
- 2009-10-25T13:04:05+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/visitor/MapPaintVisitor.java
r2308 r2313 10 10 import java.awt.Color; 11 11 import java.awt.Font; 12 import java.awt.FontMetrics; 12 13 import java.awt.Graphics2D; 13 14 import java.awt.Image; 14 15 import java.awt.Point; 15 16 import java.awt.Polygon; 17 import java.awt.Rectangle; 16 18 import java.awt.Stroke; 17 19 import java.awt.geom.GeneralPath; 20 import java.awt.geom.Point2D; 21 import java.awt.geom.Rectangle2D; 18 22 import java.util.ArrayList; 19 23 import java.util.Arrays; … … 60 64 protected Color untaggedColor; 61 65 protected Color textColor; 66 protected Color areaTextColor; 62 67 protected float[] currentDashed = new float[0]; 63 68 protected Color currentDashedColor; … … 1151 1156 } 1152 1157 1158 protected Point2D getCentroid(Polygon p) 1159 { 1160 double cx = 0.0, cy = 0.0, a = 0.0; 1161 1162 // usually requires points[0] == points[npoints] and can then use i+1 instead of j. 1163 // Faked it slowly using j. If this is really gets used, this should be fixed. 1164 for (int i = 0; i < p.npoints; i++) { 1165 int j = i+1 == p.npoints ? 0 : i+1; 1166 a += (p.xpoints[i] * p.ypoints[j]) - (p.ypoints[i] * p.xpoints[j]); 1167 cx += (p.xpoints[i] + p.xpoints[j]) * (p.xpoints[i] * p.ypoints[j] - p.ypoints[i] * p.xpoints[j]); 1168 cy += (p.ypoints[i] + p.ypoints[j]) * (p.xpoints[i] * p.ypoints[j] - p.ypoints[i] * p.xpoints[j]); 1169 } 1170 return new Point2D.Double(cx / (3.0*a), cy / (3.0*a)); 1171 } 1172 1173 protected double getArea(Polygon p) 1174 { 1175 double sum = 0.0; 1176 1177 // usually requires points[0] == points[npoints] and can then use i+1 instead of j. 1178 // Faked it slowly using j. If this is really gets used, this should be fixed. 1179 for (int i = 0; i < p.npoints; i++) { 1180 int j = i+1 == p.npoints ? 0 : i+1; 1181 sum = sum + (p.xpoints[i] * p.ypoints[j]) - (p.ypoints[i] * p.xpoints[j]); 1182 } 1183 return Math.abs(sum/2.0); 1184 } 1185 1153 1186 protected void drawArea(Way w, Color color) 1154 1187 { … … 1158 1191 g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), fillAlpha)); 1159 1192 g.fillPolygon(polygon); 1193 1194 if (showNames > dist) { 1195 String name = getWayName(w); 1196 if (name!=null /* && annotate */) { 1197 Rectangle pb = polygon.getBounds(); 1198 FontMetrics fontMetrics = g.getFontMetrics(orderFont); // if slow, use cache 1199 Rectangle2D nb = fontMetrics.getStringBounds(name, g); // if slow, approximate by strlen()*maxcharbounds(font) 1200 1201 // Point2D c = getCentroid(polygon); 1202 // Using the Centroid is Nicer for buildings like: +--------+ 1203 // but this needs to be fast. As most houses are | 42 | 1204 // boxes anyway, the center of the bounding box +---++---+ 1205 // will have to do. ++ 1206 // Centroids are not optimal either, just imagine a U-shaped house. 1207 // Point2D c = new Point2D.Double(pb.x + pb.width / 2.0, pb.y + pb.height / 2.0); 1208 // Rectangle2D.Double centeredNBounds = 1209 // new Rectangle2D.Double(c.getX() - nb.getWidth()/2, 1210 // c.getY() - nb.getHeight()/2, 1211 // nb.getWidth(), 1212 // nb.getHeight()); 1213 1214 Rectangle centeredNBounds = new Rectangle(pb.x + (int)((pb.width - nb.getWidth())/2.0), 1215 pb.y + (int)((pb.height - nb.getHeight())/2.0), 1216 (int)nb.getWidth(), 1217 (int)nb.getHeight()); 1218 1219 //// Draw name bounding box for debugging: 1220 // g.setColor(new Color(255,255,0,128)); 1221 // g.drawRect((int)centeredNBounds.getMinX(), 1222 // (int)centeredNBounds.getMinY(), 1223 // (int)centeredNBounds.getWidth(), 1224 // (int)centeredNBounds.getHeight()); 1225 1226 if ((pb.width >= nb.getWidth() && pb.height >= nb.getHeight()) && // quick check 1227 polygon.contains(centeredNBounds) // slow but nice 1228 ) { 1229 g.setColor(areaTextColor); 1230 Font defaultFont = g.getFont(); 1231 g.setFont (orderFont); 1232 g.drawString (name, 1233 (int)(centeredNBounds.getMinX() - nb.getMinX()), 1234 (int)(centeredNBounds.getMinY() - nb.getMinY())); 1235 g.setFont(defaultFont); 1236 } 1237 } 1238 } 1239 } 1240 1241 protected String getWayName(Way w) { 1242 String name = null; 1243 if (w.hasKeys()) { 1244 for (String rn : regionalNameOrder) { 1245 name = w.get(rn); 1246 if (name != null) { 1247 break; 1248 } 1249 } 1250 } 1251 return name; 1160 1252 } 1161 1253 … … 1329 1421 untaggedColor = Main.pref.getColor(marktr("untagged"),Color.GRAY); 1330 1422 textColor = Main.pref.getColor (marktr("text"), Color.WHITE); 1423 areaTextColor = Main.pref.getColor (marktr("areatext"), Color.GRAY); 1331 1424 } 1332 1425
Note:
See TracChangeset
for help on using the changeset viewer.