001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package armyc2.c5isr.graphics2d;
006import java.util.ArrayList;
007import armyc2.c5isr.JavaLineArray.POINT2;
008
009/**
010 *
011*
012 */
013public class PathIterator implements IPathIterator
014{
015    private int _currentSeg=0;
016    private ArrayList<POINT2>_pts=null;
017    public PathIterator(AffineTransform tx)
018    {
019        _currentSeg=0;
020        _pts=new ArrayList();
021    }
022    public ArrayList<POINT2>getPoints()
023    {
024        return _pts;
025    }
026    public int currentSegment(double[]coords)
027    {       
028        int type=_pts.get(_currentSeg).style;
029        if(type== SEG_LINETO || type== SEG_MOVETO)
030        {
031            coords[0]=_pts.get(_currentSeg).x;
032            coords[1]=_pts.get(_currentSeg).y;            
033        }
034        else if(type== SEG_CUBICTO)
035        {
036            coords[0]=_pts.get(_currentSeg).x;
037            coords[1]=_pts.get(_currentSeg).y;                        
038            _currentSeg++;
039            coords[2]=_pts.get(_currentSeg).x;
040            coords[3]=_pts.get(_currentSeg).y;                        
041            _currentSeg++;
042            coords[4]=_pts.get(_currentSeg).x;
043            coords[5]=_pts.get(_currentSeg).y;                        
044        } 
045        else if(type== SEG_QUADTO)
046        {
047            coords[0]=_pts.get(_currentSeg).x;
048            coords[1]=_pts.get(_currentSeg).y;                        
049            _currentSeg++;
050            coords[2]=_pts.get(_currentSeg).x;
051            coords[3]=_pts.get(_currentSeg).y;            
052        }
053        return type;
054    }
055
056    /**
057     * @deprecated use {@link #currentSegment(double[])}
058     */
059    public int currentSegment(float[] coords)
060    {
061        coords[0]=(float)_pts.get(_currentSeg).x;
062        coords[1]=(float)_pts.get(_currentSeg).y;
063        return _pts.get(_currentSeg).style;
064    }
065    public int getWindingRule()
066    {
067        return 1;        
068    }
069    public boolean isDone()
070    {
071        if(_currentSeg==_pts.size())
072            return true;
073        
074        return false;
075    }
076    public void next()
077    {
078        _currentSeg++;        
079    }
080    
081    //public methods to collect the poins and the moves
082    //GeneralPath must call this whenever its getPathIterator method is called to reset the iterator
083    public void reset()
084    {
085        _currentSeg=0;
086    }
087    public void moveTo(double x, double y)
088    {
089        _pts.add(new POINT2(x,y, SEG_MOVETO));
090    }
091    public void lineTo(double x, double y)
092    {
093        _pts.add(new POINT2(x,y, SEG_LINETO));
094    }
095    public void cubicTo(double x1, double y1, double x2, double y2, double x3, double y3)
096    {
097        _pts.add(new POINT2(x1,y1, SEG_CUBICTO));
098        _pts.add(new POINT2(x2,y2, SEG_CUBICTO));
099        _pts.add(new POINT2(x3,y3, SEG_CUBICTO));
100    }
101    public void curveTo(double x1, double y1, double x2, double y2, double x3, double y3)
102    {
103        _pts.add(new POINT2(x1,y1, SEG_CUBICTO));
104        _pts.add(new POINT2(x2,y2, SEG_CUBICTO));
105        _pts.add(new POINT2(x3,y3, SEG_CUBICTO));
106    }
107    public void quadTo(double x1, double y1, double x2, double y2)
108    {
109        _pts.add(new POINT2(x1,y1, SEG_QUADTO));
110        _pts.add(new POINT2(x2,y2, SEG_QUADTO));
111    }
112    public Rectangle2D getBounds(){
113        int j=0;
114        double left=_pts.get(0).x;
115        double right=_pts.get(0).x;
116        double top=_pts.get(0).y;
117        double bottom=_pts.get(0).y;
118        int n=_pts.size();
119        //for(j=1;j<_pts.size();j++)
120        for(j=1;j<n;j++)
121        {
122            if(_pts.get(j).x<left)
123                left=_pts.get(j).x;
124            if(_pts.get(j).x>right)
125                right=_pts.get(j).x;
126            if(_pts.get(j).y<top)
127                top=_pts.get(j).y;
128            if(_pts.get(j).y>bottom)
129                bottom=_pts.get(j).y;
130        }
131        Rectangle2D rect=new Rectangle2D.Double(left,top,right-left,bottom-top);
132        return rect;
133    }
134    public void setPathIterator(ArrayList<POINT2>pts)
135    {
136        reset();
137        _pts=pts;
138    }
139}