0

HEllo everyone...
My project consists of drawing a road two lane road using grahipcs
the code snippet below is for drawing the road
however if someone can help me in executing theses codes on an interface woud be great because i dont know how to do it
Cheers

public Road(int ID)
    {
        this.ID = ID;
        numLanes = new GeneralPath[maxNumberOfLanes];       
    }

    public void drawRoad(Graphics2D g2d) 
    {
        if (oneLane) 
        {
            g2d.setStroke(laneBackColor);
            g2d.setColor(Color.WHITE);
            g2d.draw(numLanes[0]);

            g2d.setStroke(laneColor);
            g2d.setColor(Color.BLACK);
            g2d.draw(numLanes[0]);
        } 
        else if (twoLane)
        {
            for (int i = 0; i < maxNumberOfLanes; i++) {
                if (numLanes[i] != null) {

                    g2d.setStroke(laneBackColor);
                    g2d.setColor(Color.WHITE);
                    g2d.draw(numLanes[i]);

                    g2d.setStroke(laneColor);
                    g2d.setColor(Color.BLACK);
                    g2d.draw(numLanes[i]);
                }
            }
            g2d.setStroke(middleLine);
            g2d.setColor(Color.WHITE);
            g2d.draw(numLanes[0]);
        }
    }

    public void drawHandles(Graphics2D g2d) 
    {
        if (numLanes[0] == null) 
        {
            return;
        }
        g2d.setStroke(normal);
        g2d.setColor(Color.gray);

        double seg[] = new double[6];
        Shape s;

        for (PathIterator i = numLanes[0].getPathIterator(null); !i.isDone(); i.next()) {
           i.currentSegment(seg);
            s = getSPointRectangle((int) seg[0], (int) seg[1]);
            g2d.fill(s);
        }
    }

     private Shape getSPointRectangle(int x, int y) {
        return new Rectangle2D.Double(x - 3, y - 3, 6, 6);
    }

    private Shape getLPointRectangle(int x, int y) {
        return new Rectangle2D.Double(x - 8, y - 8, 16, 16);
    }


    public void drawMiddleDirection(Graphics2D g2d) {

        g2d.setStroke(normal);

        if (oneLane && numLanes[0] != null) {
            drawMiddleDirectionLane(g2d, numLanes[0]);
        } else {
            for (int i = 1; i < maxNumberOfLanes; i++) {
                if (numLanes[i] != null) {
                    drawMiddleDirectionLane(g2d, numLanes[i]);
                }
            }
        }
    }

    private void drawMiddleDirectionLane(Graphics2D g2d, GeneralPath lane) {

        double seg[] = new double[6];
        double angle, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
        int index = 0;
        Shape s;

        for (PathIterator i = lane.getPathIterator(null); !i.isDone(); i.next()) {
           i.currentSegment(seg);

            if (index == 0) {
                x1 = seg[0];
                y1 = seg[1];
            } else if (index == 1) {
                x2 = seg[0];
                y2 = seg[1];
            } else if (index > 1) {
                x1 = x2;
                y1 = y2;
                x2 = seg[0];
                y2 = seg[1];
            }
            if (index > 0) {
                angle = GeometryFormula.getAngle(x2, y2, x1, y1);
                s = getPointTriangleAtAngle((x1 + x2) / 2, (y1 + y2) / 2, angle);

                g2d.setColor(Color.red);
                g2d.fill(s);

                g2d.setColor(Color.yellow);
                g2d.draw(s);

            }
            index++;
        }
    }

    /** Create a small triangle with the center at ("x","y")*/

    private Shape getPointTriangleAtAngle(double x, double y, double angle) {

        x = GeometryFormula.getXPointAtAngle(x, angle, -7.0);
        y = GeometryFormula.getYPointAtAngle(y, angle, -7.0);
        final int xx1 = (int) GeometryFormula.getXPointAtAngle(x, angle - (Math.PI / 8), 15.0);
        final int yy1 = (int) GeometryFormula.getYPointAtAngle(y, angle - (Math.PI / 8), 15.0);
        final int xx2 = (int) GeometryFormula.getXPointAtAngle(x, angle + (Math.PI / 8), 15.0);
        final int yy2 = (int) GeometryFormula.getYPointAtAngle(y, angle + (Math.PI / 8), 15.0);

        return new Polygon(new int[]{(int) x, xx1, xx2},
                new int[]{(int) y, yy1, yy2}, 3);
    }
2
Contributors
1
Reply
5
Views
4 Years
Discussion Span
Last Post by JamesCherrill
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.