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