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

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();
            }
        });
       
   
    }

}

Δεν υπάρχουν σχόλια: