Changeset 636 in josm for trunk


Ignore:
Timestamp:
2008-05-12T17:02:08+02:00 (16 years ago)
Author:
framm
Message:
  • better fix for NPE when drawing segment order numbers; closes #741
File:
1 edited

Legend:

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

    r634 r636  
    99import java.awt.Point;
    1010import java.awt.Polygon;
    11 import java.awt.Rectangle;
    1211import java.awt.Stroke;
    1312import java.awt.geom.GeneralPath;
     
    3029import org.openstreetmap.josm.gui.mappaint.LineElemStyle;
    3130import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    32 import org.openstreetmap.josm.tools.ColorHelper;
    3331
    3432public class MapPaintVisitor implements Visitor {
    35        
    36         protected boolean useRealWidth;
    37         protected boolean zoomLevelDisplay;
    38         protected boolean fillAreas;
    39         protected int fillAlpha;
    40         protected Color untaggedColor;
    41         protected Color textColor;
    42         protected boolean currentDashed = false;
    43         protected int currentWidth = 0;
    44         protected Stroke currentStroke = null;   
    45         protected static final Font orderFont = new Font("Helvetica", Font.PLAIN, 8);
    46        
    47         public boolean inactive;
    48        
    49         /**
    50         * The environment to paint to.
    51         */
    52         protected Graphics g;
    53        
    54         /**
    55         * MapView to get screen coordinates.
    56         */
    57         protected NavigatableComponent nc;
    58        
    59         /**
    60         * Draw subsequent segments of same color as one Path
    61         */
    62         protected Color currentColor = null;
    63         protected GeneralPath currentPath = new GeneralPath();
    64        
    65         protected static final double PHI = Math.toRadians(20);
    66        
    67         /**
    68         * Preferences
    69         */
    70         protected Color inactiveColor;
    71         protected Color selectedColor;
    72         protected Color nodeColor;
    73         protected Color dfltWayColor;
    74         protected Color untaggedWayColor;
    75         protected Color incompleteColor;
    76         protected Color backgroundColor;
    77         protected boolean showDirectionArrow;
     33   
     34    protected boolean useRealWidth;
     35    protected boolean zoomLevelDisplay;
     36    protected boolean fillAreas;
     37    protected int fillAlpha;
     38    protected Color untaggedColor;
     39    protected Color textColor;
     40    protected boolean currentDashed = false;
     41    protected int currentWidth = 0;
     42    protected Stroke currentStroke = null;   
     43    protected static final Font orderFont = new Font("Helvetica", Font.PLAIN, 8);
     44   
     45    public boolean inactive;
     46   
     47    /**
     48    * The environment to paint to.
     49    */
     50    protected Graphics g;
     51   
     52    /**
     53    * MapView to get screen coordinates.
     54    */
     55    protected NavigatableComponent nc;
     56   
     57    /**
     58    * Draw subsequent segments of same color as one Path
     59    */
     60    protected Color currentColor = null;
     61    protected GeneralPath currentPath = new GeneralPath();
     62   
     63    protected static final double PHI = Math.toRadians(20);
     64   
     65    /**
     66    * Preferences
     67    */
     68    protected Color inactiveColor;
     69    protected Color selectedColor;
     70    protected Color nodeColor;
     71    protected Color dfltWayColor;
     72    protected Color untaggedWayColor;
     73    protected Color incompleteColor;
     74    protected Color backgroundColor;
     75    protected boolean showDirectionArrow;
    7876    protected boolean showRelevantDirectionsOnly;
    79         protected boolean showOrderNumber;
    80        
    81         public final static Color darkerblue = new Color(0,0,96);
    82         public final static Color darkblue = new Color(0,0,128);
    83        
    84         protected boolean isZoomOk(ElemStyle e) {
    85                 double circum = Main.map.mapView.getScale()*100*Main.proj.scaleFactor()*40041455; // circumference of the earth in meter
    86 
    87                 /* show everything if the user wishes so */
    88                 if (!zoomLevelDisplay) {
    89                         return true;
    90                 }
    91 
    92                 if (e == null) {
    93                         /* the default for things that don't have a rule (show, if scale is smaller than 1500m) */
    94                         if (circum < 1500)
    95                                 return true;
    96                         return false;
    97                 }
    98 
    99                 // formula to calculate a map scale: natural size / map size = scale
    100                 // example: 876000mm (876m as displayed) / 22mm (roughly estimated screen size of legend bar) = 39818
    101                 //
    102                 // so the exact "correcting value" below depends only on the screen size and resolution
    103                 // XXX - do we need a Preference setting for this (if things vary widely)?
    104                 /*System.out.println(
     77    protected boolean showOrderNumber;
     78   
     79    public final static Color darkerblue = new Color(0,0,96);
     80    public final static Color darkblue = new Color(0,0,128);
     81   
     82    protected boolean isZoomOk(ElemStyle e) {
     83        double circum = Main.map.mapView.getScale()*100*Main.proj.scaleFactor()*40041455; // circumference of the earth in meter
     84
     85        /* show everything if the user wishes so */
     86        if (!zoomLevelDisplay) {
     87            return true;
     88        }
     89
     90        if (e == null) {
     91            /* the default for things that don't have a rule (show, if scale is smaller than 1500m) */
     92            if (circum < 1500)
     93                return true;
     94            return false;
     95        }
     96
     97        // formula to calculate a map scale: natural size / map size = scale
     98        // example: 876000mm (876m as displayed) / 22mm (roughly estimated screen size of legend bar) = 39818
     99        //
     100        // so the exact "correcting value" below depends only on the screen size and resolution
     101        // XXX - do we need a Preference setting for this (if things vary widely)?
     102        /*System.out.println(
    105103   "Circum: " + circum +
    106104   " max: " + e.getMaxScale() + "(" + e.getMaxScale()/22 + ")" +
    107105   " min:" + e.getMinScale() + "(" + e.getMinScale()/22 + ")");*/
    108                 if(circum>=e.getMaxScale() / 22 || circum<e.getMinScale() / 22)
    109                         return false;
    110                 return true;
    111         }
    112 
    113         /**
    114         * Draw a small rectangle.
    115         * White if selected (as always) or red otherwise.
    116         *
    117         * @param n The node to draw.
    118         */
    119         public void visit(Node n) {
    120                 ElemStyle nodeStyle = MapPaintStyles.getStyle(n);
    121                 if (nodeStyle!=null) {
    122                         if (nodeStyle instanceof IconElemStyle) {
    123                                 if (isZoomOk(nodeStyle)) {
    124                                         drawNode(n, ((IconElemStyle)nodeStyle).getIcon(), ((IconElemStyle)nodeStyle).doAnnotate());
    125                                 }
    126                         } else {
    127                                 // throw some sort of exception
    128                         }
    129                 } else {
    130                         drawNode(n, n.selected ? selectedColor : nodeColor);
    131                 }
    132         }
    133 
    134         /**
    135         * Draw a line for all segments, according to tags.
    136         * @param w The way to draw.
    137         */
    138         public void visit(Way w) {
    139                 double circum = Main.map.mapView.getScale()*100*Main.proj.scaleFactor()*40041455; // circumference of the earth in meter
     106        if(circum>=e.getMaxScale() / 22 || circum<e.getMinScale() / 22)
     107            return false;
     108        return true;
     109    }
     110
     111    /**
     112    * Draw a small rectangle.
     113    * White if selected (as always) or red otherwise.
     114    *
     115    * @param n The node to draw.
     116    */
     117    public void visit(Node n) {
     118        ElemStyle nodeStyle = MapPaintStyles.getStyle(n);
     119        if (nodeStyle!=null) {
     120            if (nodeStyle instanceof IconElemStyle) {
     121                if (isZoomOk(nodeStyle)) {
     122                    drawNode(n, ((IconElemStyle)nodeStyle).getIcon(), ((IconElemStyle)nodeStyle).doAnnotate());
     123                }
     124            } else {
     125                // throw some sort of exception
     126            }
     127        } else {
     128            drawNode(n, n.selected ? selectedColor : nodeColor);
     129        }
     130    }
     131
     132    /**
     133    * Draw a line for all segments, according to tags.
     134    * @param w The way to draw.
     135    */
     136    public void visit(Way w) {
     137        double circum = Main.map.mapView.getScale()*100*Main.proj.scaleFactor()*40041455; // circumference of the earth in meter
    140138                // show direction arrows, if draw.segment.relevant_directions_only is not set, the way is tagged with a direction key
    141139                // (even if the tag is negated as in oneway=false) or the way is selected
    142                 boolean showDirection = w.selected ||
     140        boolean showDirection = w.selected ||
    143141                                        ((!useRealWidth) && (showDirectionArrow
    144142                                                             && (!showRelevantDirectionsOnly || w.hasDirectionKeys)));
    145143
    146                 Color colour = untaggedColor;
    147                 int width = 2;
    148                 int realWidth = 0; //the real width of the element in meters
    149                 boolean dashed = false;
    150                 boolean area = false;
    151                 ElemStyle wayStyle = MapPaintStyles.getStyle(w);
    152 
    153                 if(!isZoomOk(wayStyle)) {
    154                         return;
    155                 }
    156 
    157                 if(wayStyle!=null)
    158                 {
    159                         if(wayStyle instanceof LineElemStyle)
    160                         {
    161                                 colour = ((LineElemStyle)wayStyle).colour;
    162                                 width = ((LineElemStyle)wayStyle).width;
    163                                 realWidth = ((LineElemStyle)wayStyle).realWidth;
    164                                 dashed = ((LineElemStyle)wayStyle).dashed;
    165                         }
    166                         else if (wayStyle instanceof AreaElemStyle)
    167                         {
    168                                 colour = ((AreaElemStyle)wayStyle).getColour();
    169                                 area = true;
    170                         }
    171                 }
    172 
    173                 if (area && fillAreas)
    174                         drawWayAsArea(w, colour);
    175                 int orderNumber = 0;
    176 
    177                 Node lastN = null;
    178                 for (Node n : w.nodes) {
    179                         if (lastN == null) {
    180                                 lastN = n;
    181                                 continue;
    182                         }
    183                         orderNumber++;
    184 
    185                         if (area && fillAreas) {
     144        Color colour = untaggedColor;
     145        int width = 2;
     146        int realWidth = 0; //the real width of the element in meters
     147        boolean dashed = false;
     148        boolean area = false;
     149        ElemStyle wayStyle = MapPaintStyles.getStyle(w);
     150
     151        if(!isZoomOk(wayStyle)) {
     152            return;
     153        }
     154
     155        if(wayStyle!=null)
     156        {
     157            if(wayStyle instanceof LineElemStyle)
     158            {
     159                colour = ((LineElemStyle)wayStyle).colour;
     160                width = ((LineElemStyle)wayStyle).width;
     161                realWidth = ((LineElemStyle)wayStyle).realWidth;
     162                dashed = ((LineElemStyle)wayStyle).dashed;
     163            }
     164            else if (wayStyle instanceof AreaElemStyle)
     165            {
     166                colour = ((AreaElemStyle)wayStyle).getColour();
     167                area = true;
     168            }
     169        }
     170
     171        if (area && fillAreas)
     172            drawWayAsArea(w, colour);
     173        int orderNumber = 0;
     174
     175        Node lastN = null;
     176        for (Node n : w.nodes) {
     177            if (lastN == null) {
     178                lastN = n;
     179                continue;
     180            }
     181            orderNumber++;
     182
     183            if (area && fillAreas) {
    186184                // hack to make direction arrows visible against filled background
    187                                 if (showDirection)
    188                                     drawSeg(lastN, n, w.selected ? selectedColor : untaggedColor, showDirection, width, true);
    189                         } else {
    190                                 if (area) {
    191                                         drawSeg(lastN, n, w.selected ? selectedColor : colour, showDirection, width, true);
    192                                 } else {
    193                                         if (realWidth > 0 && useRealWidth && !showDirection) {
    194                                                 int tmpWidth = (int) (100 /  (float) (circum / realWidth));
    195                                                 if (tmpWidth > width) width = tmpWidth;
    196                                         }
     185                if (showDirection)
     186                    drawSeg(lastN, n, w.selected ? selectedColor : untaggedColor, showDirection, width, true);
     187            } else {
     188                if (area) {
     189                    drawSeg(lastN, n, w.selected ? selectedColor : colour, showDirection, width, true);
     190                } else {
     191                    if (realWidth > 0 && useRealWidth && !showDirection) {
     192                        int tmpWidth = (int) (100 /  (float) (circum / realWidth));
     193                        if (tmpWidth > width) width = tmpWidth;
     194                    }
    197195                }
    198196            }
    199197
    200                         drawSeg(lastN, n, w.selected ? selectedColor : colour, showDirection, width, dashed);
    201 
    202                         if (showOrderNumber)
    203                                 drawOrderNumber(lastN, n, orderNumber);
    204 
    205                         lastN = n;
    206                 }
    207         }
    208 
    209         public void visit(Relation e) {
    210                 // relations are not (yet?) drawn.
    211         }
    212        
    213         // This assumes that all segments are aligned in the same direction!
    214         protected void drawWayAsArea(Way w, Color colour)
    215         {
    216                 Polygon polygon = new Polygon();
    217                 Point p;
    218                 // set the opacity (alpha) level of the filled polygon
    219                 Color coloura = new Color( colour.getRed(), colour.getGreen(), colour.getBlue(), fillAlpha);
    220 
    221                 for (Node n : w.nodes)
    222                 {
    223                         p = nc.getPoint(n.eastNorth);
    224                         polygon.addPoint(p.x,p.y);
    225                 }
    226 
    227                 g.setColor( w.selected ?
    228                                 selectedColor : coloura);
    229 
    230                 g.fillPolygon(polygon);
    231         }
    232 
    233         // NEW
    234         protected void drawNode(Node n, ImageIcon icon, boolean annotate) {
    235                 Point p = nc.getPoint(n.eastNorth);
    236                 if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
    237                 int w = icon.getIconWidth(), h=icon.getIconHeight();
    238                 icon.paintIcon ( Main.map.mapView, g, p.x-w/2, p.y-h/2 );
    239                 String name = (n.keys==null) ? null : n.keys.get("name");
    240                 if (name!=null && annotate)
    241                 {
    242                         g.setColor(textColor);
    243                         Font defaultFont = g.getFont();
    244                         g.setFont (orderFont);
    245                         g.drawString (name, p.x+w/2+2, p.y+h/2+2);
    246                         g.setFont(defaultFont);
    247                 }
    248                 if (n.selected)
    249                 {
    250                         g.setColor (  selectedColor );
    251                         g.drawRect (p.x-w/2-2,p.y-w/2-2, w+4, h+4);
    252                 }
    253         }
    254 
    255         /**
    256          * Draw a line with the given color.
    257          */
    258         protected void drawSegment(Node n1, Node n2, Color col, boolean showDirection) {
    259                 if (useRealWidth && showDirection) showDirection = false;
    260                 drawSeg(n1, n2, col, showDirection, 1, false);
    261         }
    262 
    263         private void drawSeg(Node n1, Node n2, Color col, boolean showDirection, int width, boolean dashed) {
    264                 if (col != currentColor || width != currentWidth || dashed != currentDashed) {
    265                         displaySegments(col, width, dashed);
    266                 }
    267                 Point p1 = nc.getPoint(n1.eastNorth);
    268                 Point p2 = nc.getPoint(n2.eastNorth);
    269 
    270                 // checking if this segment is visible
    271                 if ((p1.x < 0) && (p2.x < 0)) return ;
    272                 if ((p1.y < 0) && (p2.y < 0)) return ;
    273                 if ((p1.x > nc.getWidth()) && (p2.x > nc.getWidth())) return ;
    274                 if ((p1.y > nc.getHeight()) && (p2.y > nc.getHeight())) return ;
    275                 //if (ls.selected)
    276                 // col = selectedColor;
    277                 //g.setColor(col);
    278                 //g.setWidth(width);
    279                 //if (dashed)
    280                 // g2d.setStroke(new BasicStroke(width,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,0,new float[] {9},0));
    281                 //else
    282                 // g2d.setStroke(new BasicStroke(width,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
    283 
    284                 //g.drawLine(p1.x, p1.y, p2.x, p2.y);
    285                 currentPath.moveTo(p1.x, p1.y);
    286                 currentPath.lineTo(p2.x, p2.y);
    287 
    288                 if (showDirection) {
    289                         double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
    290                         //g.drawLine(p2.x,p2.y, (int)(p2.x + 10*Math.cos(t-PHI)), (int)(p2.y + 10*Math.sin(t-PHI)));
    291                         //g.drawLine(p2.x,p2.y, (int)(p2.x + 10*Math.cos(t+PHI)), (int)(p2.y + 10*Math.sin(t+PHI)));
    292                         currentPath.lineTo((int)(p2.x + 10*Math.cos(t-PHI)), (int)(p2.y + 10*Math.sin(t-PHI)));
    293                         currentPath.moveTo((int)(p2.x + 10*Math.cos(t+PHI)), (int)(p2.y + 10*Math.sin(t+PHI)));
    294                         currentPath.lineTo(p2.x, p2.y);
    295                 }
    296                 //g2d.setStroke(new BasicStroke(1));
    297 
    298         }
    299 
    300         protected void displaySegments() {
    301                 displaySegments(null, 0, false);
    302         }
    303 
    304         protected void displaySegments(Color newColor, int newWidth, boolean newDash) {
    305 
    306                 if (currentPath != null) {
    307                         Graphics2D g2d = (Graphics2D)g;
    308                         g2d.setColor(inactive ? inactiveColor : currentColor);
    309                         if (currentStroke == null) {
    310                                 if (currentDashed)
    311                                         g2d.setStroke(new BasicStroke(currentWidth,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,0,new float[] {9},0));
    312                                 else
    313                                         g2d.setStroke(new BasicStroke(currentWidth,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
    314                         }
    315                         g2d.draw(currentPath);
    316                         g2d.setStroke(new BasicStroke(1));
    317 
    318                         currentPath = new GeneralPath();
    319                         currentColor = newColor;
    320                         currentWidth = newWidth;
    321                         currentDashed = newDash;
    322                         currentStroke = null;
    323                 }
    324         }
    325 
    326         /**
    327          * Draw the node as small rectangle with the given color.
    328          *
    329          * @param n  The node to draw.
    330          * @param color The color of the node.
    331          */
    332         public void drawNode(Node n, Color color) {
    333                 if(isZoomOk(null)) {
    334                         Point p = nc.getPoint(n.eastNorth);
    335                         if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
    336                         g.setColor(color);
    337                         g.drawRect(p.x-1, p.y-1, 2, 2);
    338                 }
    339         }
    340 
    341         // NW 111106 Overridden from SimplePaintVisitor in josm-1.4-nw1
    342         // Shows areas before non-areas
    343         public void visitAll(DataSet data) {
    344                 inactiveColor = Preferences.getPreferencesColor("inactive", Color.DARK_GRAY);
    345                 selectedColor = Preferences.getPreferencesColor("selected", Color.YELLOW);
    346                 nodeColor = Preferences.getPreferencesColor("node", Color.RED);
    347                 dfltWayColor = Preferences.getPreferencesColor("way", darkblue);
    348                 incompleteColor = Preferences.getPreferencesColor("incomplete way", darkerblue);
    349                 backgroundColor = Preferences.getPreferencesColor("background", Color.BLACK);
    350                 untaggedColor = Preferences.getPreferencesColor("untagged",Color.GRAY);
    351                 textColor = Preferences.getPreferencesColor ("text", Color.WHITE);
    352                 showDirectionArrow = Main.pref.getBoolean("draw.segment.direction");
    353                 showRelevantDirectionsOnly = Main.pref.getBoolean("draw.segment.relevant_directions_only");
    354                 showOrderNumber = Main.pref.getBoolean("draw.segment.order_number");
    355                 useRealWidth = Main.pref.getBoolean("mappaint.useRealWidth",false);
    356                 zoomLevelDisplay = Main.pref.getBoolean("mappaint.zoomLevelDisplay",false);
    357                 fillAreas = Main.pref.getBoolean("mappaint.fillareas", true);
    358 
    359                 /* XXX - there must be a better way to get a bounded Integer pref! */
    360                 try {
    361                         fillAlpha = Integer.valueOf(Main.pref.get("mappaint.fillalpha", "50"));
    362                         if (fillAlpha < 0) {
    363                                 fillAlpha = 0;
    364                         }
    365                         if (fillAlpha > 255) {
    366                                 fillAlpha = 255;
    367                         }
    368                 } catch (NumberFormatException nfe) {
    369                         fillAlpha = 50;
    370                 }
    371 
    372                 Collection<Way> noAreaWays = new LinkedList<Way>();
    373 
    374                 for (final OsmPrimitive osm : data.ways)
    375                         if (!osm.incomplete && !osm.deleted && MapPaintStyles.isArea(osm))
    376                                 osm.visit(this);
    377                         else if (!osm.deleted && !osm.incomplete)
    378                                 noAreaWays.add((Way)osm);
    379 
    380                 for (final OsmPrimitive osm : noAreaWays)
    381                         osm.visit(this);
    382 
    383                 for (final OsmPrimitive osm : data.nodes)
    384                         if (!osm.incomplete && !osm.deleted)
    385                                 osm.visit(this);
    386 
    387                 for (final OsmPrimitive osm : data.getSelected())
    388                         if (!osm.incomplete && !osm.deleted){
    389                                 osm.visit(this);
    390                         }
    391                 displaySegments();
    392         }
    393        
    394         /**
    395          * Draw an number of the order of the two consecutive nodes within the
    396          * parents way
    397          */
    398         protected void drawOrderNumber(Node n1, Node n2, int orderNumber) {
    399                 int strlen = (""+orderNumber).length();
    400                 Point p1 = nc.getPoint(n1.eastNorth);
    401                 Point p2 = nc.getPoint(n2.eastNorth);
    402                 int x = (p1.x+p2.x)/2 - 4*strlen;
    403                 int y = (p1.y+p2.y)/2 + 4;
    404 
    405                 Rectangle screen = g.getClipBounds();
    406                 if ((screen != null) && screen.contains(x,y)) {
    407                         Color c = g.getColor();
    408                         g.setColor(backgroundColor);
    409                         g.fillRect(x-1, y-12, 8*strlen+1, 14);
    410                         g.setColor(c);
    411                         g.drawString(""+orderNumber, x, y);
    412                 }
    413     }
    414        
    415         public void setGraphics(Graphics g) {
    416         this.g = g;
    417     }
    418 
    419         public void setNavigatableComponent(NavigatableComponent nc) {
    420         this.nc = nc;
     198            drawSeg(lastN, n, w.selected ? selectedColor : colour, showDirection, width, dashed);
     199
     200            if (showOrderNumber)
     201                drawOrderNumber(lastN, n, orderNumber);
     202
     203            lastN = n;
     204        }
     205    }
     206
     207    public void visit(Relation e) {
     208        // relations are not (yet?) drawn.
     209    }
     210   
     211    // This assumes that all segments are aligned in the same direction!
     212    protected void drawWayAsArea(Way w, Color colour)
     213    {
     214        Polygon polygon = new Polygon();
     215        Point p;
     216        // set the opacity (alpha) level of the filled polygon
     217        Color coloura = new Color( colour.getRed(), colour.getGreen(), colour.getBlue(), fillAlpha);
     218
     219        for (Node n : w.nodes)
     220        {
     221            p = nc.getPoint(n.eastNorth);
     222            polygon.addPoint(p.x,p.y);
     223        }
     224
     225        g.setColor( w.selected ?
     226                selectedColor : coloura);
     227
     228        g.fillPolygon(polygon);
     229    }
     230
     231    // NEW
     232    protected void drawNode(Node n, ImageIcon icon, boolean annotate) {
     233        Point p = nc.getPoint(n.eastNorth);
     234        if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
     235        int w = icon.getIconWidth(), h=icon.getIconHeight();
     236        icon.paintIcon ( Main.map.mapView, g, p.x-w/2, p.y-h/2 );
     237        String name = (n.keys==null) ? null : n.keys.get("name");
     238        if (name!=null && annotate)
     239        {
     240            g.setColor(textColor);
     241            Font defaultFont = g.getFont();
     242            g.setFont (orderFont);
     243            g.drawString (name, p.x+w/2+2, p.y+h/2+2);
     244            g.setFont(defaultFont);
     245        }
     246        if (n.selected)
     247        {
     248            g.setColor (  selectedColor );
     249            g.drawRect (p.x-w/2-2,p.y-w/2-2, w+4, h+4);
     250        }
     251    }
     252
     253    /**
     254     * Draw a line with the given color.
     255     */
     256    protected void drawSegment(Node n1, Node n2, Color col, boolean showDirection) {
     257        if (useRealWidth && showDirection) showDirection = false;
     258        drawSeg(n1, n2, col, showDirection, 1, false);
     259    }
     260
     261    private void drawSeg(Node n1, Node n2, Color col, boolean showDirection, int width, boolean dashed) {
     262        if (col != currentColor || width != currentWidth || dashed != currentDashed) {
     263            displaySegments(col, width, dashed);
     264        }
     265        Point p1 = nc.getPoint(n1.eastNorth);
     266        Point p2 = nc.getPoint(n2.eastNorth);
     267
     268        if (!isSegmentVisible(p1, p2)) {
     269            return;
     270        }
     271        //if (ls.selected)
     272        // col = selectedColor;
     273        //g.setColor(col);
     274        //g.setWidth(width);
     275        //if (dashed)
     276        // g2d.setStroke(new BasicStroke(width,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,0,new float[] {9},0));
     277        //else
     278        // g2d.setStroke(new BasicStroke(width,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
     279
     280        //g.drawLine(p1.x, p1.y, p2.x, p2.y);
     281        currentPath.moveTo(p1.x, p1.y);
     282        currentPath.lineTo(p2.x, p2.y);
     283
     284        if (showDirection) {
     285            double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
     286            //g.drawLine(p2.x,p2.y, (int)(p2.x + 10*Math.cos(t-PHI)), (int)(p2.y + 10*Math.sin(t-PHI)));
     287            //g.drawLine(p2.x,p2.y, (int)(p2.x + 10*Math.cos(t+PHI)), (int)(p2.y + 10*Math.sin(t+PHI)));
     288            currentPath.lineTo((int)(p2.x + 10*Math.cos(t-PHI)), (int)(p2.y + 10*Math.sin(t-PHI)));
     289            currentPath.moveTo((int)(p2.x + 10*Math.cos(t+PHI)), (int)(p2.y + 10*Math.sin(t+PHI)));
     290            currentPath.lineTo(p2.x, p2.y);
     291        }
     292        //g2d.setStroke(new BasicStroke(1));
     293
     294    }
     295
     296    protected void displaySegments() {
     297        displaySegments(null, 0, false);
     298    }
     299
     300    protected void displaySegments(Color newColor, int newWidth, boolean newDash) {
     301
     302        if (currentPath != null) {
     303            Graphics2D g2d = (Graphics2D)g;
     304            g2d.setColor(inactive ? inactiveColor : currentColor);
     305            if (currentStroke == null) {
     306                if (currentDashed)
     307                    g2d.setStroke(new BasicStroke(currentWidth,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,0,new float[] {9},0));
     308                else
     309                    g2d.setStroke(new BasicStroke(currentWidth,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
     310            }
     311            g2d.draw(currentPath);
     312            g2d.setStroke(new BasicStroke(1));
     313
     314            currentPath = new GeneralPath();
     315            currentColor = newColor;
     316            currentWidth = newWidth;
     317            currentDashed = newDash;
     318            currentStroke = null;
     319        }
     320    }
     321
     322    /**
     323     * Draw the node as small rectangle with the given color.
     324     *
     325     * @param n  The node to draw.
     326     * @param color The color of the node.
     327     */
     328    public void drawNode(Node n, Color color) {
     329        if(isZoomOk(null)) {
     330            Point p = nc.getPoint(n.eastNorth);
     331            if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
     332            g.setColor(color);
     333            g.drawRect(p.x-1, p.y-1, 2, 2);
     334        }
     335    }
     336
     337    // NW 111106 Overridden from SimplePaintVisitor in josm-1.4-nw1
     338    // Shows areas before non-areas
     339    public void visitAll(DataSet data) {
     340        inactiveColor = Preferences.getPreferencesColor("inactive", Color.DARK_GRAY);
     341        selectedColor = Preferences.getPreferencesColor("selected", Color.YELLOW);
     342        nodeColor = Preferences.getPreferencesColor("node", Color.RED);
     343        dfltWayColor = Preferences.getPreferencesColor("way", darkblue);
     344        incompleteColor = Preferences.getPreferencesColor("incomplete way", darkerblue);
     345        backgroundColor = Preferences.getPreferencesColor("background", Color.BLACK);
     346        untaggedColor = Preferences.getPreferencesColor("untagged",Color.GRAY);
     347        textColor = Preferences.getPreferencesColor ("text", Color.WHITE);
     348        showDirectionArrow = Main.pref.getBoolean("draw.segment.direction");
     349        showRelevantDirectionsOnly = Main.pref.getBoolean("draw.segment.relevant_directions_only");
     350        showOrderNumber = Main.pref.getBoolean("draw.segment.order_number");
     351        useRealWidth = Main.pref.getBoolean("mappaint.useRealWidth",false);
     352        zoomLevelDisplay = Main.pref.getBoolean("mappaint.zoomLevelDisplay",false);
     353        fillAreas = Main.pref.getBoolean("mappaint.fillareas", true);
     354
     355        /* XXX - there must be a better way to get a bounded Integer pref! */
     356        try {
     357            fillAlpha = Integer.valueOf(Main.pref.get("mappaint.fillalpha", "50"));
     358            if (fillAlpha < 0) {
     359                fillAlpha = 0;
     360            }
     361            if (fillAlpha > 255) {
     362                fillAlpha = 255;
     363            }
     364        } catch (NumberFormatException nfe) {
     365            fillAlpha = 50;
     366        }
     367
     368        Collection<Way> noAreaWays = new LinkedList<Way>();
     369
     370        for (final OsmPrimitive osm : data.ways)
     371            if (!osm.incomplete && !osm.deleted && MapPaintStyles.isArea(osm))
     372                osm.visit(this);
     373            else if (!osm.deleted && !osm.incomplete)
     374                noAreaWays.add((Way)osm);
     375
     376        for (final OsmPrimitive osm : noAreaWays)
     377            osm.visit(this);
     378
     379        for (final OsmPrimitive osm : data.nodes)
     380            if (!osm.incomplete && !osm.deleted)
     381                osm.visit(this);
     382
     383        for (final OsmPrimitive osm : data.getSelected())
     384            if (!osm.incomplete && !osm.deleted){
     385                osm.visit(this);
     386            }
     387        displaySegments();
     388    }
     389   
     390    /**
     391     * Draw a number of the order of the two consecutive nodes within the
     392     * parents way
     393     */
     394    protected void drawOrderNumber(Node n1, Node n2, int orderNumber) {
     395        Point p1 = nc.getPoint(n1.eastNorth);
     396        Point p2 = nc.getPoint(n2.eastNorth);
     397        if (!isSegmentVisible(p1, p2)) {
     398            return;
     399        }
     400        int strlen = (""+orderNumber).length();
     401        int x = (p1.x+p2.x)/2 - 4*strlen;
     402        int y = (p1.y+p2.y)/2 + 4;
     403
     404        Color c = g.getColor();
     405        g.setColor(backgroundColor);
     406        g.fillRect(x-1, y-12, 8*strlen+1, 14);
     407        g.setColor(c);
     408        g.drawString(""+orderNumber, x, y);
     409      }
     410
     411    private boolean isSegmentVisible(Point p1, Point p2) {
     412        if ((p1.x < 0) && (p2.x < 0)) return false;
     413        if ((p1.y < 0) && (p2.y < 0)) return false;
     414        if ((p1.x > nc.getWidth()) && (p2.x > nc.getWidth())) return false;
     415        if ((p1.y > nc.getHeight()) && (p2.y > nc.getHeight())) return false;
     416        return true;
     417    }
     418   
     419    public void setGraphics(Graphics g) {
     420        this.g = g;
     421    }
     422
     423    public void setNavigatableComponent(NavigatableComponent nc) {
     424        this.nc = nc;
    421425    }
    422426}
Note: See TracChangeset for help on using the changeset viewer.