Changeset 2313 in josm for trunk/src


Ignore:
Timestamp:
2009-10-25T13:04:05+01:00 (14 years ago)
Author:
stoecker
Message:

applied #3272 - patch by roques - display names of areas as well (using a different color)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/MapPaintVisitor.java

    r2308 r2313  
    1010import java.awt.Color;
    1111import java.awt.Font;
     12import java.awt.FontMetrics;
    1213import java.awt.Graphics2D;
    1314import java.awt.Image;
    1415import java.awt.Point;
    1516import java.awt.Polygon;
     17import java.awt.Rectangle;
    1618import java.awt.Stroke;
    1719import java.awt.geom.GeneralPath;
     20import java.awt.geom.Point2D;
     21import java.awt.geom.Rectangle2D;
    1822import java.util.ArrayList;
    1923import java.util.Arrays;
     
    6064    protected Color untaggedColor;
    6165    protected Color textColor;
     66    protected Color areaTextColor;
    6267    protected float[] currentDashed = new float[0];
    6368    protected Color currentDashedColor;
     
    11511156    }
    11521157
     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
    11531186    protected void drawArea(Way w, Color color)
    11541187    {
     
    11581191        g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), fillAlpha));
    11591192        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;
    11601252    }
    11611253
     
    13291421        untaggedColor = Main.pref.getColor(marktr("untagged"),Color.GRAY);
    13301422        textColor = Main.pref.getColor (marktr("text"), Color.WHITE);
     1423        areaTextColor = Main.pref.getColor (marktr("areatext"), Color.GRAY);
    13311424    }
    13321425
Note: See TracChangeset for help on using the changeset viewer.