Changeset 21174 in osm for applications/editors


Ignore:
Timestamp:
2010-05-08T11:09:41+02:00 (14 years ago)
Author:
jttt
Message:

Fix painting in openjdk

Location:
applications/editors/josm/plugins/routes/src/org/openstreetmap/josm/plugins/routes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/routes/src/org/openstreetmap/josm/plugins/routes/RelationEditMode.java

    r20427 r21174  
    6262        @Override
    6363        public void mouseClicked(MouseEvent e) {
     64                if (Main.main.getCurrentDataSet() == null)
     65                        return;
     66
    6467                Way way = Main.map.mapView.getNearestWay(e.getPoint());
    65 
    6668                Collection<Relation> selectedRelations = Main.main.getCurrentDataSet().getSelectedRelations();
    6769
  • applications/editors/josm/plugins/routes/src/org/openstreetmap/josm/plugins/routes/paint/AbstractLinePainter.java

    r16428 r21174  
    11package org.openstreetmap.josm.plugins.routes.paint;
    22
     3import java.awt.Graphics2D;
     4import java.awt.Point;
     5import java.awt.Rectangle;
     6import java.awt.geom.GeneralPath;
    37import java.awt.geom.Line2D;
    48import java.awt.geom.Point2D;
     9import java.util.List;
     10
     11import org.openstreetmap.josm.Main;
     12import org.openstreetmap.josm.data.osm.Node;
     13import org.openstreetmap.josm.data.osm.visitor.paint.LineClip;
     14import org.openstreetmap.josm.gui.MapView;
     15import org.openstreetmap.josm.gui.NavigatableComponent;
    516
    617public abstract class AbstractLinePainter implements PathPainter {
    7        
     18
    819        // Following two method copied from http://blog.persistent.info/2004/03/java-lineline-intersections.html
    920        protected boolean getLineLineIntersection(Line2D.Double l1,
    1021                        Line2D.Double l2,
    11                         Point2D.Double intersection)
     22                        Point intersection)
    1223        {
    1324                double  x1 = l1.getX1(), y1 = l1.getY1(),
     
    1526                x3 = l2.getX1(), y3 = l2.getY1(),
    1627                x4 = l2.getX2(), y4 = l2.getY2();
    17                
    18                 double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3)*(y2 - y1));
    19                 intersection.x = x1 + ua * (x2 - x1);
    20                 intersection.y = y1 + ua * (y2 - y1);
    21                  
     28                double dx1 = x2 - x1;
     29                double dx2 = x4 - x3;
     30                double dy1 = y2 - y1;
     31                double dy2 = y4 - y3;
    2232
    23 /*              intersection.x = det(det(x1, y1, x2, y2), x1 - x2,
    24                                 det(x3, y3, x4, y4), x3 - x4)/
    25                                 det(x1 - x2, y1 - y2, x3 - x4, y3 - y4);
    26                 intersection.y = det(det(x1, y1, x2, y2), y1 - y2,
    27                                 det(x3, y3, x4, y4), y3 - y4)/
    28                                 det(x1 - x2, y1 - y2, x3 - x4, y3 - y4);*/
     33                double ua = (dx2 * (y1 - y3) - dy2 * (x1 - x3)) / (dy2 * dx1 - dx2 * dy1);
     34
     35                if (Math.abs(dy2 * dx1 - dx2 * dy1) < 0.0001) {
     36                        intersection.x = (int)l1.x2;
     37                        intersection.y = (int)l1.y2;
     38                        return false;
     39                } else {
     40                        intersection.x = (int)(x1 + ua * (x2 - x1));
     41                        intersection.y = (int)(y1 + ua * (y2 - y1));
     42                }
     43
     44                if (intersection.x > 600) {
     45                        System.out.println();
     46                }
    2947
    3048                return true;
     
    3654        }
    3755
    38         protected Point2D shiftPoint(Point2D p1, Point2D p2, double shift) {
     56        protected Point shiftPoint(Point2D p1, Point2D p2, double shift) {
    3957                double dx = p2.getX() - p1.getX();
    4058                double dy = p2.getY() - p1.getY();
    41                
     59
    4260                // Perpendicular vector
    4361                double ndx = -dy;
    4462                double ndy = dx;
    45                
     63
    4664                // Normalize
    4765                double length = Math.sqrt(ndx * ndx + ndy * ndy);
    4866                ndx = ndx / length;
    4967                ndy = ndy / length;
    50                
    51                 return new Point2D.Double(p1.getX() + shift * ndx, p1.getY() + shift * ndy);
     68
     69                return new Point((int)(p1.getX() + shift * ndx), (int)(p1.getY() + shift * ndy));
    5270        }
    53        
     71
    5472        protected Line2D.Double shiftLine(Point2D p1, Point2D p2, double shift) {
    5573                double dx = p2.getX() - p1.getX();
     
    5876                Point2D point1 = shiftPoint(p1, p2, shift);
    5977                Point2D point2 = new Point2D.Double(point1.getX() + dx, point1.getY() + dy);
    60        
     78
    6179                return new Line2D.Double(
    6280                                point1, point2);
    6381        }
    6482
     83        protected GeneralPath getPath(Graphics2D g, MapView mapView, List<Node> nodes, double shift) {
     84
     85                GeneralPath path = new GeneralPath();
     86
     87                if (nodes.size() < 2) {
     88                        return path;
     89                }
     90
     91                Point p1 = null;
     92                Point p2 = null;
     93                Point p3 = null;
     94                Point lastPoint = null;
     95
     96                for (Node n: nodes) {
     97                        Point p = mapView.getPoint(n);
     98
     99                        if (!p.equals(p3)) {
     100                                p1 = p2;
     101                                p2 = p3;
     102                                p3 = p;
     103                        } else {
     104                                continue;
     105                        }
     106
     107                        p = null;
     108                        if (p2 != null) {
     109                                if (p1 == null) {
     110                                        p = shiftPoint(p2, p3, shift);
     111                                } else {
     112                                        Line2D.Double line1 = shiftLine(p1, p2, shift);
     113                                        Line2D.Double line2 = shiftLine(p2, p3, shift);
     114
     115                                        /*path.moveTo((float)line1.x1, (float)line1.y1);
     116                                        path.lineTo((float)line1.x2, (float)line1.y2);
     117                                        path.moveTo((float)line2.x1, (float)line2.y1);
     118                                        path.lineTo((float)line2.x2, (float)line2.y2);*/
     119
     120                                        p = new Point();
     121                                        if (!getLineLineIntersection(line1, line2, p)) {
     122                                                p = null;
     123                                        } else {
     124                                                int dx = p.x - p2.x;
     125                                                int dy = p.y - p2.y;
     126                                                int distance = (int)Math.sqrt(dx * dx + dy * dy);
     127                                                if (distance > 10) {
     128                                                        p.x = p2.x + dx / (distance / 10);
     129                                                        p.y = p2.y + dy / (distance / 10);
     130                                                }
     131                                        }
     132                                }
     133                        }
     134
     135                        if (p != null && lastPoint != null) {
     136                                drawSegment(g, mapView, path, lastPoint, p);
     137                        }
     138                        if (p != null) {
     139                                lastPoint = p;
     140                        }
     141                }
     142
     143                if (p2 != null && p3 != null && lastPoint != null) {
     144                        p3 = shiftPoint(p3, p2, -shift);
     145                        drawSegment(g, mapView, path, lastPoint, p3);
     146                }
     147
     148                return path;
     149        }
     150
     151        private void drawSegment(Graphics2D g, NavigatableComponent nc, GeneralPath path, Point p1, Point p2) {
     152                boolean drawIt = false;
     153                if (Main.isOpenjdk) {
     154                        /**
     155                         * Work around openjdk bug. It leads to drawing artefacts when zooming in a lot. (#4289, #4424)
     156                         * (It looks like int overflow when clipping.) We do custom clipping.
     157                         */
     158                        Rectangle bounds = g.getClipBounds();
     159                        bounds.grow(100, 100);                  // avoid arrow heads at the border
     160                        LineClip clip = new LineClip();
     161                        drawIt = clip.cohenSutherland(p1.x, p1.y, p2.x, p2.y, bounds.x, bounds.y, bounds.x+bounds.width, bounds.y+bounds.height);
     162                        if (drawIt) {
     163                                p1 = clip.getP1();
     164                                p2 = clip.getP2();
     165                        }
     166                } else {
     167                        drawIt = isSegmentVisible(nc, p1, p2);
     168                }
     169                if (drawIt) {
     170                        /* draw segment line */
     171                        path.moveTo(p1.x, p1.y);
     172                        path.lineTo(p2.x, p2.y);
     173                }
     174        }
     175
     176        private boolean isSegmentVisible(NavigatableComponent nc, Point p1, Point p2) {
     177                if ((p1.x < 0) && (p2.x < 0)) return false;
     178                if ((p1.y < 0) && (p2.y < 0)) return false;
     179                if ((p1.x > nc.getWidth()) && (p2.x > nc.getWidth())) return false;
     180                if ((p1.y > nc.getHeight()) && (p2.y > nc.getHeight())) return false;
     181                return true;
     182        }
     183
     184
    65185}
  • applications/editors/josm/plugins/routes/src/org/openstreetmap/josm/plugins/routes/paint/NarrowLinePainter.java

    r16428 r21174  
    33import java.awt.BasicStroke;
    44import java.awt.Graphics2D;
    5 import java.awt.Point;
    6 import java.awt.geom.GeneralPath;
    7 import java.awt.geom.Line2D;
    8 import java.awt.geom.Point2D;
    95import java.util.BitSet;
    106import java.util.List;
     
    1713
    1814public class NarrowLinePainter extends AbstractLinePainter {
    19        
     15
    2016        private static final float LINE_WIDTH = 5;
    2117        private final RouteLayer layer;
    22        
     18
    2319        public NarrowLinePainter(RouteLayer layer) {
    2420                this.layer = layer;
     
    2824                List<Node> nodes = way.getNodes();
    2925                BitSet routes = way.getRoutes();
    30                
     26
    3127                if (nodes.size() < 2) {
    3228                        return;
    3329                }
    34                
     30
    3531                //double totalWidth = LINE_WIDTH + (colors.size() - 1) * 4;
    3632                //double width = totalWidth / colors.size();
     
    3834                double width = LINE_WIDTH;
    3935                double shift = - (LINE_WIDTH * routes.cardinality()) / 2 + width / 2;
    40                
     36
    4137                for (int k=0; k<routes.length(); k++) {
    42                        
     38
    4339                        if (!routes.get(k)) {
    4440                                continue;
    4541                        }
    46                        
     42
    4743                        RouteDefinition route = layer.getRoutes().get(k);
    48                        
     44
    4945                        g.setColor(route.getColor());
    5046                        g.setStroke(new BasicStroke((float) width));
    51                        
    52                         GeneralPath path = new GeneralPath();
    53                         Point2D start = shiftPoint(mapView.getPoint(nodes.get(0).getEastNorth()),
    54                                         mapView.getPoint(nodes.get(1).getEastNorth()), shift);
    55                         path.moveTo((float)start.getX(), (float)start.getY());
    56                         for (int i=1; i<nodes.size() - 1; i++) {
    57                                 Point p1 = mapView.getPoint(nodes.get(i - 1).getEastNorth());
    58                                 Point p2 = mapView.getPoint(nodes.get(i).getEastNorth());
    59                                 Point p3 = mapView.getPoint(nodes.get(i + 1).getEastNorth());
    6047
     48                        g.draw(getPath(g, mapView, nodes, shift));
    6149
    62                                 Line2D.Double line1 = shiftLine(p1, p2, shift);
    63                                 Line2D.Double line2 = shiftLine(p2, p3, shift);
    64 
    65                                 Point2D.Double intersection = new Point2D.Double();
    66                                 getLineLineIntersection(line1, line2, intersection);
    67                                 if (!Double.isNaN(intersection.getX())  && !Double.isNaN(intersection.getY())) {
    68                                         path.lineTo((float)intersection.getX(), (float)intersection.getY());
    69                                 }
    70                         }
    71                         Point2D stop = shiftPoint(mapView.getPoint(nodes.get(nodes.size() - 1).getEastNorth()),
    72                                         mapView.getPoint(nodes.get(nodes.size() - 2).getEastNorth()), -shift);
    73                         path.lineTo((float)stop.getX(), (float)stop.getY());
    74                         g.draw(path);
    75                        
    7650                        shift += width + 2;
    7751                }
  • applications/editors/josm/plugins/routes/src/org/openstreetmap/josm/plugins/routes/paint/WideLinePainter.java

    r16428 r21174  
    44import java.awt.Color;
    55import java.awt.Graphics2D;
    6 import java.awt.Point;
    7 import java.awt.geom.GeneralPath;
    8 import java.awt.geom.Line2D;
    9 import java.awt.geom.Point2D;
    106import java.util.BitSet;
    117import java.util.List;
     
    1814
    1915public class WideLinePainter extends AbstractLinePainter {
    20        
     16
    2117        private static final float LINE_WIDTH = 10;
    2218        private final RouteLayer layer;
    23        
     19
    2420        public WideLinePainter(RouteLayer layer) {
    2521                this.layer = layer;
     
    2925                List<Node> nodes = way.getNodes();
    3026                BitSet routes = way.getRoutes();
    31                
     27
    3228                if (nodes.size() < 2) {
    3329                        return;
    3430                }
    35                
     31
    3632                double totalWidth = LINE_WIDTH + (routes.size() - 1) * 4;
    3733                double width = totalWidth / routes.cardinality();
    3834                double shift = -totalWidth / 2 + width / 2;
    39                
     35
    4036                for (int k=0; k<routes.length(); k++) {
    41                        
     37
    4238                        if (!routes.get(k)) {
    4339                                continue;
    4440                        }
    45                        
     41
    4642                        RouteDefinition route = layer.getRoutes().get(k);
    47        
     43
    4844                        Color color = route.getColor();
    4945                        g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), 100));
    5046                        g.setStroke(new BasicStroke((float) width));
    51                        
    52                         GeneralPath path = new GeneralPath();
    53                         Point2D start = shiftPoint(mapView.getPoint(nodes.get(0).getEastNorth()),
    54                                         mapView.getPoint(nodes.get(1).getEastNorth()), shift);
    55                         path.moveTo((float)start.getX(), (float)start.getY());
    56                         for (int i=1; i<nodes.size() - 1; i++) {
    57                                 Point p1 = mapView.getPoint(nodes.get(i - 1).getEastNorth());
    58                                 Point p2 = mapView.getPoint(nodes.get(i).getEastNorth());
    59                                 Point p3 = mapView.getPoint(nodes.get(i + 1).getEastNorth());
    6047
     48                        g.draw(getPath(g, mapView, nodes, shift));
    6149
    62                                 Line2D.Double line1 = shiftLine(p1, p2, shift);
    63                                 Line2D.Double line2 = shiftLine(p2, p3, shift);
    64 
    65                                 Point2D.Double intersection = new Point2D.Double();
    66                                 getLineLineIntersection(line1, line2, intersection);
    67                                 if (!Double.isNaN(intersection.getX())  && !Double.isNaN(intersection.getY())) {
    68                                         path.lineTo((float)intersection.getX(), (float)intersection.getY());
    69                                 }
    70                         }
    71                         Point2D stop = shiftPoint(mapView.getPoint(nodes.get(nodes.size() - 1).getEastNorth()),
    72                                         mapView.getPoint(nodes.get(nodes.size() - 2).getEastNorth()), -shift);
    73                         path.lineTo((float)stop.getX(), (float)stop.getY());
    74                         g.draw(path);
    75                        
    7650                        shift += width;
    7751                }
Note: See TracChangeset for help on using the changeset viewer.