Τρίτη 15 Οκτωβρίου 2019

Java PreOrder Backward Enumeration Iterator

<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>


/*First class on university...6/10 !!!*/
package dim.ds;

import dim.ext.javax.swing.tree.Dmtn;//DefaultMutableTreeNode
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

/**
NOT return ROOT !

 * A Backward Pre Order Enumeration

 * <br>7, 6, 999, 99, some, 3, 2, 1
 * <br> will be given by the following tree
 * <br>0=root
 * <br>--0.1=1
 * <br>--0.2=2
 * <br>--0.3=3
 * <br>--------0.3.0=some
 * <br>----------------0.3.0.0=99
 * <br>----------------0.3.0.1=999
 * <br>--------0.3.1=6
 * <br>--0.4=7
 * @author jimidimi
 */
public class PreBackOrder implements Enumeration<Dmtn>,Iterator<Dmtn>{
    public static void main(String[] args) {
        Dmtn t=new Dmtn("root");
        //7,6,1,2,5,3,2,1
        t.crad(1);
        t.crad(2);
        Dmtn d3=t.crad(3);
            Dmtn d5=d3.crad(5);
                d5.crad(2);
                d5.crad(1);
            Dmtn d6=d3.crad(6);
        t.crad(7);
     
        PreBackOrder pbo=new PreBackOrder(t);
        while(pbo.hasMoreElements()){
            Object o=pbo.nextElement();
            System.out.println(pbo.counter+" : "+o+"  depth = "+pbo.depth);
        }
     
        System.out.println(" -------------- second example ----------- ");
        t.removeAllChildren();
     
        //
        //4,0,450,3,2,1,21
        t.sbad(21).sbad(1);
        t.sbad(21).sbad(2);
        t.sbad(21).sbad(3);
        t.sbad(450).sbad(0);
        t.sbad(450).sbad(4);

     
        pbo.clear();
        while(pbo.hasMoreElements()){
            Object o=pbo.nextElement();
            System.out.println(pbo.counter+" : "+o+"  depth = "+pbo.depth);
        }
     
     
     
    }
 
 
 
 
 
    public Dmtn root,par,it;
 
    /**
     * The current index path
     */
    public Vector<Integer> indexPathStack=new Vector<>();
    /**
     * The current last index of Vector
     */
    public int stackIndex=-1;
    /**
     * The current last Value of Vector
     */
    public int itIndexOnPar;
 
    /**
     * Simple enumeration step counter
     */
    public int counter=-1;
 
 
    /**
     * The previus (from call) stack.size()=stackIndex+1
     */
    int depth=0;

    /**
     *
     * @param theRoot to iterate without ... theRoot !
     * <br>To iterate also to Root use
     * <br>1.Or some level up root
     * <br>2.Or Dmtn temp=new Dmtn("temp");
     * <br>  temp.add(my_root);
     * <br>  new PreBackOrder(temp).iterate.....
     *
     */
    public PreBackOrder(Dmtn theRoot){
        root=theRoot;
        clear();
    }
    /**
     * When called must be synchronized(lock) ????
     */
    public void clear(){
        it=root;
        indexPathStack.clear();
        stackIndex=-1;
        int chc=it.getChildCount();
        itIndexOnPar=chc;
        while(chc>0){
            chc--;
            indexPathStack.add(chc);
            itIndexOnPar=chc;
            ++stackIndex;
            it=it.dmtn(chc);
            chc=it.getChildCount();
        }
        par=it.pardmtn();
        counter=-1;
        depth=stackIndex+1;
    }
 


    @Override
    public boolean hasNext() {
        return it!=null;
    }
    @Override
    public Dmtn next() {
        return nextElement();
    }
 
    /**
     *
     * @return it!=null;
     */
    @Override
    public boolean hasMoreElements() {
        return it!=null;
    }
    /**
     * rolls on the iteration to next stage
     * @return
     */
    @Override
    public Dmtn nextElement() {
        ++counter;//info
        depth=stackIndex+1;//info
        ///////////////////////////////////////
        Dmtn out=it;
        //this missing TAB INSET is missing but class not return ROOT ????!!!???
            --itIndexOnPar;
            //System.out.println("lastchc : "+lastChc);
            indexPathStack.setElementAt(itIndexOnPar, stackIndex);
            //System.out.println(""+itIndex);
            if(itIndexOnPar>=0){
                //System.out.println("Taking brother");
                it=par.dmtn(itIndexOnPar);
                int chc=it.getChildCount();
                while(chc>0){
                    chc--;
                    indexPathStack.add(chc);
                    itIndexOnPar=chc;
                    ++stackIndex;
                    it=it.dmtn(chc);
                    chc=it.getChildCount();
                }
                par=it.pardmtn();
            }
            else{
                //System.out.println("Finished iterate on "+par+" children");
             
                indexPathStack.remove(stackIndex);
                --stackIndex;

                if(stackIndex<0){
                    it=null;
                }
                else{
                    it=par;
                    par=it.pardmtn();
                    itIndexOnPar=indexPathStack.get(stackIndex);
                }
             
            }
        return out;
    }


 
 
 
 
 
 
}

Java Scan-Sarosi- Util Class

Java Scan-Sarosi- Util Class

package dim.games.streetracing;

import dim.ext.java.awt.Erect;

//erase also SarosiBuild class......
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JSplitPane;
import javax.swing.SpinnerNumberModel;







/**
 *
 * @author jimidimi
 */
public class Sarosi extends Erect{//Rectangle
   
    public static void main(String[] args) {
        SarosiBuild.testBuild();
    }


    /**
     * current iterator indexes setted before scan() being called
     */
    public int xScan,yScan;
   
    public int xLimit,yLimit;
    public int xDif=1,yDif=1;
    public int xOneOnly=0,yOneOnly=0;//when some dif=0
    public boolean xInnerWhile=true;
    public Sarosi(int x, int y, int width, int height) {
        super(x, y, width, height);
    }
   
   
   
   
    //OVERRIDE
    public void scan(){}
    public void scanAllFinished(){}
    public void scanAllStarting(){}
   
   


   
    //CLEARing mind

   
    public void scanAll(){
        scanAllStarting();
        if(xDif<0){
            if(yDif<0){//-1x,-1y
                if(xInnerWhile){
                    yScan=y+height-1;
                    yLimit=y-1;
                    xLimit=x-1;
                    int temp=x+width-1;
                    while(yScan>yLimit){
                        xScan=temp;
                        while(xScan>xLimit){scan();//////////////
                            xScan+=xDif;
                        }
                        yScan+=yDif;
                    }
                }
                else{
                    xScan=x+width-1;
                    xLimit=x-1;
                    yLimit=y-1;
                    int temp=y+height-1;
                    while(xScan>xLimit){
                        yScan=temp;
                        while(yScan>yLimit){scan();///////////////
                            yScan+=yDif;
                        }
                        xScan+=xDif;
                    }
                }               
            }
            else if(yDif>0){//-1x,+1y
                if(xInnerWhile){
                    yScan=y;
                    yLimit=y+height;
                    xLimit=x-1;
                    int temp=x+width-1;
                    while(yScan<yLimit){
                        xScan=temp;
                        while(xScan>xLimit){scan();///////////////////
                            xScan+=xDif;
                        }
                        yScan+=yDif;
                    }
                }
                else{
                    xScan=x+width-1;
                    xLimit=x-1;
                    yLimit=y+height;
                    while(xScan>xLimit){
                        yScan=y;
                        while(yScan<yLimit){scan();/////////////////////
                            yScan+=yDif;
                        }
                        xScan+=xDif;
                    }
                }               
            }
            else{//-1x,0y
                xScan=x+width-1;
                xLimit=x-1;
                yScan=yOneOnly;
                while(xScan>xLimit){
                    scan();
                    xScan+=xDif;
                }
               
            }
        }
        else if(xDif>0){
            if(yDif<0){//+1x,-1y
                if(xInnerWhile){
                    yScan=y+height-1;
                    yLimit=y-1;
                    xLimit=x+width;
                    while(yScan>yLimit){
                        xScan=x;
                        while(xScan<xLimit){scan();////////////////////////
                            xScan+=xDif;
                        }
                        yScan+=yDif;
                    }
                }
                else{
                    xScan=x;
                    xLimit=x+width;
                    yLimit=y-1;
                    int temp=y+height-1;
                    while(xScan<xLimit){
                        yScan=temp;
                        while(yScan>yLimit){scan();////////////////////
                            yScan+=yDif;
                        }
                        xScan+=xDif;
                    }
                }               

            }
            else if(yDif>0){//+1x,+1y
                if(xInnerWhile){//System.out.println("+1x,+1y,true");
                    yScan=y;
                    yLimit=y+height;
                    xLimit=x+width;
                    while(yScan<yLimit){
                        xScan=x;
                        while(xScan<xLimit){scan();//////////////////////
                            xScan+=xDif;
                        }
                        yScan+=yDif;
                    }
                }
                else{
                    xScan=x;
                    xLimit=x+width;
                    yLimit=y+height;
                    while(xScan<xLimit){
                        yScan=y;
                        while(yScan<yLimit){scan();////////////////////////
                            yScan+=yDif;
                        }
                        xScan+=xDif;
                    }
                }               
            }
            else{//1x,0y
                xScan=x;
                xLimit=x+width;
                yScan=yOneOnly;
                while(xScan<xLimit){
                    scan();
                    xScan+=xDif;
                }
            }
        }
        else{
            if(yDif<0){//0x-1y
                yScan=y+height-1;
                yLimit=y-1;
                xScan=xOneOnly;
                while(yScan>yLimit){
                    scan();
                    yScan+=yDif;
                }
            }
            else if(yDif>0){//0x+1y
                yScan=y;
                yLimit=y+height;
                while(yScan<yLimit){
                    scan();
                    yScan+=yDif;
                }
            }
            else{//0x 0y disabled?
                //System.out.println("Disabled all ? or just scan()");
                scan();//stupid
            }
           
        }
        scanAllFinished();

    }
   
   
   
   

   
}


class SarosiBuild{
    static int rows=3;
    static int cols=7;
    static JPanel jpGrid=new JPanel(new GridLayout(rows,cols));
    static JRadioButton comps[][]=new JRadioButton[rows][cols];
    static JSpinner jslXdif=new JSpinner(new SpinnerNumberModel(1,-101,101,1));
    static JSpinner jslYdif=new JSpinner(new SpinnerNumberModel(1,-101,101,1));
    static Sarosi testInit=new Sarosi(0,0,cols,rows){
        public void scan(){
            //System.out.println(xScan+" , "+yScan);
            comps[yScan][xScan]=new JRadioButton(xScan+" , "+yScan,false);
            jpGrid.add(comps[yScan][xScan]);
        }
    };
    static Sarosi testReset=new Sarosi(0,0,cols,rows){
        public void scan(){
            comps[yScan][xScan].setSelected(false);
        }
    };
    static Sarosi testAct=new Sarosi(0,0,cols,rows){
        public void scan(){
            comps[yScan][xScan].setSelected(true);
            Toolkit.getDefaultToolkit().beep();
            try{Thread.currentThread().sleep(500);}catch(InterruptedException inter){}
        }
    };

    static void testBuild(){
        JPanel jpButs=new JPanel(new GridLayout(4,1));
       
       
        JButton jbRestart=new JButton("Restart");
        jpButs.add(jbRestart);
       
       
        JPanel jpX=new JPanel(new BorderLayout());
        jpX.add(jslXdif,BorderLayout.CENTER);
        jpX.add(new JLabel(" X dif "),BorderLayout.WEST);
        jpButs.add(jpX);
       
        JPanel jpY=new JPanel(new BorderLayout());
        jpY.add(jslYdif,BorderLayout.CENTER);
        jpY.add(new JLabel(" Y dif "),BorderLayout.WEST);
        jpButs.add(jpY);

        JRadioButton jrbInnerWhile=new JRadioButton("X Inner While",true);
        jpButs.add(jrbInnerWhile);
       
       

       
        testInit.scanAll();
       
        JSplitPane jspl=new JSplitPane(1,jpButs,jpGrid);
       
       
       
        JFrame jfr=new JFrame("testing "+Sarosi.class);
        Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
        jfr.setBounds(screen.width/4,screen.height/4,screen.width/2,screen.height/2);
        jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfr.getContentPane().add(jspl,BorderLayout.CENTER);
        jfr.setVisible(true);
       
        jbRestart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Thread thr=new Thread(){
                    public void run(){
                        testReset.scanAll();
                        try{Thread.currentThread().sleep(1234);}catch(InterruptedException inter){}
                        testAct.xDif=(Integer)jslXdif.getValue();
                        testAct.yDif=(Integer)jslYdif.getValue();
                        testAct.xInnerWhile=jrbInnerWhile.isSelected();
                        testAct.scanAll();
                       
                    }
                };
                thr.setDaemon(true);
                thr.start();
            }
        });
       
   
    }

}