Παρασκευή 14 Φεβρουαρίου 2020

Point JTextField Java


This is an old and 'private' version so you cant easy understand 

See newer,simplest and standalone version 


---------------------------------------------------------------------------



Αντι να εχω 2 JTextField s

για να κουναω ενα Point με
το πληκτρολογιο,,,,

εχω 1 με ποντικι (και πληκτρα) !
------------------------








/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jim.lib.j;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import jim.Jim;
import jim.lib.a.Dmtn;
import jim.lib.a.Thr;
import jim.lib.d.Path;
import jim.lib.d.Point;
import jim.lib.j.swing.Jfr;
import jim.lib.j.swing.Jspl;
import jim.lib.j.swing.Jtf;
import jim.lib.j.swing.Manager;
import jim.lib.p.PaintNode;

/**
 * created at 14 Φεβ 2020
 * @author jimakoskx
 */
public class JArrows4Tf extends Jtf{
   
    public static void main(String[] args) {
        JArrows4Tf test1=new JArrows4Tf(1,2);
        JArrows4Tf test2=new JArrows4Tf(1.0,3.0,-10,10,1);
        test1.isReversingPaintMode=false;
        test2.isReversingPaintMode=true;
        test1.setName("test1");
        test2.setName("test2");
        test1.cutTextToLong=true;


       
        Jfr jfr=new Jfr(new Jspl(1,test1,test2).q(0.5).qCont());
        jfr.setBounds(0,0,888,222);
    }
   
    static ComponentListener CML=new ComponentListener() {
        @Override
        public void componentResized(ComponentEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.resizedArrows4Tf();}
        }
        @Override
        public void componentMoved(ComponentEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.movedArrows4Tf();}
        }

        @Override
        public void componentShown(ComponentEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.shownArrows4Tf();}
        }

        @Override
        public void componentHidden(ComponentEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.hiddenArrows4Tf();}
        }
    };
    static MouseMotionListener MML=new MouseMotionListener() {
        @Override
        public void mouseDragged(MouseEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.draggedArrows4Tf(e);}
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.movedArrows4Tf(e);}
        }
    };
    static MouseListener ML=new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.clickedArrows4Tf(e);}
        }
        @Override
        public void mousePressed(MouseEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.pressedArrows4Tf(e);}
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.releasedArrows4Tf(e);}
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.enteredArrows4Tf(e); }
        }
        @Override
        public void mouseExited(MouseEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.exitedArrows4Tf(e);}
        }
    };

    static KeyListener KL=new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {}
        @Override
        public void keyPressed(KeyEvent e) {//System.out.println("shifter");
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.klcheckingOneByShift(e);}
        }
        @Override
        public void keyReleased(KeyEvent e) {
            JArrows4Tf src=(JArrows4Tf)e.getComponent();
            if(src!=null){src.klcheckingOneByShift(e);}
        }
    };
    static KeyListener KLparser=new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {}
        @Override
        public void keyPressed(KeyEvent e) {//System.out.println("parser");
            if(e.getKeyCode()==KeyEvent.VK_ENTER){
                JArrows4Tf src=(JArrows4Tf)e.getComponent();
                if(src!=null){src.parseText();}
            }
        }
        @Override
        public void keyReleased(KeyEvent e) {}
    };
   
    /**
     * need low Alpha for the text of JTextField to be visible
     */
    static Color arrowsColorDefault=new Color(143,44,0,90);
   
   
    //System.out.println("l"+SwingConstants.LEFT);
    //System.out.println("r"+SwingConstants.RIGHT);
    //System.out.println("b"+SwingConstants.BOTTOM);
    //System.out.println("t"+SwingConstants.TOP);   
    static final int swUp1=1;
    static final int swLeft2=2;
    static final int swDown3=3;
    static final int swRight4=4;
    static final String swStr[]=new String[]{"Center","Up","Left","Down","Right"};
   
    static long sleepBetweenPointIncDecrCalls=50;//20 frames per second
    static Thr unicMouseTimeMover=new Thr(){
        @Override
        public void doWork() {
            JArrows4Tf src=(JArrows4Tf)workOn.getUserObject();
            //System.out.println("working on "+src.getName());
            while(src.draggedDir4>0){
                //System.out.println("calling for "+swStr[src.draggedDir4]);
                src.applyAgainCurrDirectionToPoint();
                sl(sleepBetweenPointIncDecrCalls);
            }
        }
    }.q("JArrows4Tf.unicMouseTimeMover");
   
    static{
        unicMouseTimeMover.start();
    }   
    ///////////////////////////////////////////////////////////////
    ///////  C L A S S       H E R E   ////////////////////////////
    ///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////
   

    Point val=new Point(0,0);
    Point stepAbs=new Point(1,1);
    Point min=new Point(Jim.minlong,Jim.minlong);
    Point max=new Point(Jim.maxlong,Jim.maxlong);
    Point temp=new Point(0,0);
   
   
    Path pLeft=new Path();
    PaintNode pnLeft=new PaintNode(pLeft).qfill();
    Path pRight=new Path();
    PaintNode pnRight=new PaintNode(pRight).qfill();
    Path pUp=new Path();
    PaintNode pnUp=new PaintNode(pUp).qfill();
    Path pDown=new Path();
    PaintNode pnDown=new PaintNode(pDown).qfill();
    /**
     * if even w > h may need to draw arrows vertically (or opposite)...may dummy never be used
     */
    boolean isReversingPaintMode=false;
    /**
     * width , w/2 , w/3 , 2*(w/3)
     */
    int w,wd2,wd3,wd32;
    /**
     * height , h/2 , h/3 , 2*(h/3)
     */
    int h,hd2,hd3,hd32;
   
   
    /**
     * if(false) then arrows not showing if mouse is out of JTextField
     */
    boolean arrowsAlwaysOn=true;
    /**
     *  Up0,Left1,Down2,Right3 Point move modes
     */
    /**
     * updated direction on dragging (before releasing mouse button)
     */
    int draggedDir4=-1;
    /**
     * Triggering starts when user press mouse on JTextfield<br>
     * MouseMotionListener and KeyListener will be added
     * Buffer of the static Thread will be filled with a reference to this Object<br>
     */
    int pressedDir4=-1;
    /**
     * According of the mouse button for slow,normal,quick speed of the Point movement
     */
    int multiplier=1;
    /**
     * If user suddenly while moving the Point press shift then we must slow speed<br>
     * to be easy to adjust at exact point
     */
    boolean isShiftDown=false;
    /**
     * One instead of STEP....cancelling step that is usually adjusted with multiplier !
     */
    int one=1;
   
   
    Dmtn buttons=new Dmtn();//MultipliersPerButtons ..binary search just for 1,2,3 values? Answer:...Just easy programming

    /**
     *
     */
    public JArrows4Tf (){
        super("OLA KALA EDO ");
        //setCursor(Cursor.getDefaultCursor());//no text cursor
        setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));//


        painter=new PaintNode();
        painter.q(arrowsColorDefault);
        painter.add(pnDown);
        painter.add(pnLeft);
        painter.add(pnRight);
        painter.add(pnUp);
       
       
        buttons.abin(MouseEvent.BUTTON1).crad(1);
        //buttons.abin(MouseEvent.BUTTON2).crad(1);
        buttons.abin(MouseEvent.BUTTON3).crad(3);
       
       
        addComponentListener(CML);
        addKeyListener(KLparser);
        addMouseListener(ML);
        refreshText();
    }
   
    public JArrows4Tf (double x,double y){
        this();
        val.x=x;
        val.y=y;
        refreshText();
    }
    public JArrows4Tf (double x,double y,double minv,double maxv,double stepv){
        this();
        if( stepv<=0 || (minv>=maxv) || x<minv || y<minv || x>maxv || y>maxv || (maxv-minv<stepv) ){
            throw new IllegalArgumentException("JArrows4Tf NOT if( step<=0 || (min>=max) || x<min || y<min || x>max || y>max || (max-min<step) )");
        }     
        val.x=x;
        val.y=y;
       
        min.x=minv;
        min.y=minv;
       
        max.x=maxv;
        max.y=maxv;
       
        stepAbs.x=stepv;
        stepAbs.y=stepv;
       
        refreshText();
    }
   
   
   
   
   
    /**
     * Override to notify for the new Point
     */
    public void xyChanged(){
        //System.out.println("must implement by class programmer user");
    }
    /**
     * Check limits and if ok sets and calls valuesChanged()
     * @param x
     * @param y
     * @return
     */
    public boolean setxy(double x,double y){
        if( x<min.x || x>max.x || y<min.y || y>max.y  ){
            return false;
        }
        else{
            val.x=x;
            val.y=y;
            xyChanged();
            refreshText();
            return true;
        }
    }
    public void setxyNoCheck(double x,double y){
        val.x=x;
        val.y=y;
        xyChanged();
        refreshText();
    }
    public void setxNoCheck(double x){
        val.x=x;
        xyChanged();
        refreshText();
    }
    public void setyNoCheck(double y){
        val.y=y;
        xyChanged();
        refreshText();
    }

    public boolean checkxy(double x,double y){
        return !( x<min.x || x>max.x || y<min.y || y>max.y );
    }
    public boolean checkx(double x){
        return !( x<min.x || x>max.x);
    }
    public boolean checky(double y){
        return !( y<min.y || y>max.y );
    }

   
   
    public int xi(){return (int)val.x;}
    public int yi(){return (int)val.y;}
    public long xl(){return (long)val.x;}
    public long yl(){return (long)val.y;}
    public double x(){return val.x;}
    public double y(){return val.y;}
   
    public void getxy(Point2D p){
        p.setLocation(val.x,val.y);
    }
    synchronized void parseText(){
        String text=getText();
        Point p=new Point();
        Exception e=jim.lib.d.Point.parsePoint(text, p);
        if(e!=null){
            JOptionPane.showMessageDialog(this, e,"Unable to parse Point from Text",JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            if(setxy(p.x,p.y)){
                //System.out.println("good");
            }
            else{
                JOptionPane.showMessageDialog(this, "attempted to set \n("+p.x+","+p.y+")\nNot allowed because of\nlimitations\n"+getLimitsText(),"Values out of limits",JOptionPane.INFORMATION_MESSAGE);
                refreshText();
            }
        }
    }
    public String getLimitsText(){
        return min.x+" <= x <= "+max.x+" \n"+min.y+" <= y <= "+max.y+" \n";
    }
   
    public void setArrowsPainting(boolean active){
        painter.childrenOn=active;
        repaint();
    }
    public boolean getArrowsPainting(){
        return painter.childrenOn;
    }
    public void setArrowsColor(Color c){
        painter.q(c);
        repaint();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if(enabled){addMouseListener(ML);}
        else{removeMouseListener(ML);}
    }
   
   
   
   
    boolean isArrowsHorizontal(){
        if(w>=h){
            if(isReversingPaintMode){return false;}
            else{return true;}
        }
        else{
            if(isReversingPaintMode){return true;}
            else{return false;}
        }
    }
    void resizedArrows4Tf(){
        w=getWidth();
        h=getHeight();
        pDown.reset();
        pLeft.reset();
        pRight.reset();
        pUp.reset();
       
       
        wd2=w/2;
        hd2=h/2;
        wd3=w/3;
        wd32=wd3*2;
        hd3=h/3;
        hd32=hd3*2;

        if(w>=h){
            if(isReversingPaintMode){resetingPaintModeVertical();}
            else{resetingPaintModeHorizontal();}
        }
        else{
            if(isReversingPaintMode){resetingPaintModeHorizontal();}
            else{resetingPaintModeVertical();}
        }
    }
    void movedArrows4Tf(){}
    void shownArrows4Tf(){}
    void hiddenArrows4Tf(){}
   
    void resetingPaintModeHorizontal(){
        pDown.moveTo(wd2, h);
        pDown.lineTo(wd3, hd2);
        pDown.lineTo(wd32,hd2);
        pDown.closePath();

        pUp.moveTo(wd2, 0);
        pUp.lineTo(wd32, hd2-1);
        pUp.lineTo(wd3,hd2-1);
        pUp.closePath();

        pLeft.moveTo(0, hd2);
        pLeft.lineTo(wd3, h);
        pLeft.lineTo(wd3,0);
        pLeft.closePath();

        pRight.moveTo(w, hd2);
        pRight.lineTo(wd32, 0);
        pRight.lineTo(wd32,h);
        pRight.closePath();       
    }
    void resetingPaintModeVertical(){
        pDown.moveTo(wd2, h);
        pDown.lineTo(w, hd32);
        pDown.lineTo(0,hd32);
        pDown.closePath();

        pUp.moveTo(wd2, 0);
        pUp.lineTo(0, hd3);
        pUp.lineTo(w,hd3);
        pUp.closePath();

        pLeft.moveTo(0, hd2);
        pLeft.lineTo(wd2-1,hd32);
        pLeft.lineTo(wd2-1,hd3);
        pLeft.closePath();

        pRight.moveTo(w, hd2);
        pRight.lineTo(wd2, hd3);
        pRight.lineTo(wd2,hd32);
        pRight.closePath();       
    }
   
   
   
   
   
   
         
    int getDir4(int ex,int ey){
        if(isArrowsHorizontal()){//System.out.println("Arrows are horizontal");
            if(ex>wd32){//System.out.println("Right");
                return swRight4;
            }
            else if(ex<wd3){//System.out.println("Left");
                return swLeft2;
            }
            else{
                if(ey<hd2){//System.out.println("Up");
                    return swUp1;
                }
                else{//System.out.println("Down");
                    return swDown3;
                }
            }
        }
        else{//System.out.println("Arrows are vertical");
            if(ey>hd32){//System.out.println("Down");
                return swDown3;
            }
            else if(ey<hd3){//System.out.println("Up");
                return swUp1;
            }
            else{
                if(ex<wd2){//System.out.println("Left");
                    return swLeft2;
                }
                else{//System.out.println("Right");
                    return swRight4;
                }
            }
        }
    }
    void clickedArrows4Tf(MouseEvent e){}
    void pressedArrows4Tf(MouseEvent e){
       
        int but=e.getButton();
        Dmtn buttonNode=buttons.abn(but);
        if(buttonNode!=null){
            multiplier=buttonNode.dmtn(0).i();
           
            int ex=e.getX();
            int ey=e.getY();
            pressedDir4=getDir4(ex, ey);
            draggedDir4=pressedDir4;


            isShiftDown=e.isShiftDown();///means NO MULTIPLIER AT ALL ..ONE
            //System.out.println("Pressed "+swStr[pressedDir4]+" mult="+multiplier);

            unicMouseTimeMover.works.crad(this);
            unicMouseTimeMover.interrupt();
            addKeyListener(KL);
            addMouseMotionListener(MML);
        }
        //else{System.out.println("This button is not installed");}
   
    }
    void releasedArrows4Tf(MouseEvent e){
        draggedDir4=0;
        removeMouseMotionListener(MML);
        removeKeyListener(KL);
    }
    void enteredArrows4Tf(MouseEvent e){
        if(!arrowsAlwaysOn){setArrowsPainting(true);}
    }
    void exitedArrows4Tf(MouseEvent e){
        if(!arrowsAlwaysOn){setArrowsPainting(false);}
    }
    void draggedArrows4Tf(MouseEvent e){
       
        isShiftDown=e.isShiftDown();///means NO MULTIPLIER AT ALL ..ONE
       
        int refresh=getDir4(e.getX(), e.getY());
        if(refresh!=draggedDir4){
            draggedDir4=refresh;
            //System.out.println("Dir changed to "+swStr[draggedDir4]);
        }
       
    }
   
    void movedArrows4Tf(MouseEvent e){}
   
   
   
         


    void valuesChanged0(){
        refreshText();
        xyChanged();
    }
    boolean cutTextToLong=false;
    public String getPointText(){
        if(cutTextToLong){return (long)val.x+","+(long)val.y;}
        else{return val.x+","+val.y;}
    }
    void refreshText(){
        setText(getPointText());           
    }


    void klcheckingOneByShift(KeyEvent e) {
        isShiftDown=e.isShiftDown();
    }

   

    void beep(){
        Manager.beep();
    }

    void applyAgainCurrDirectionToPoint(){
        //System.out.println("Going "+swStr[draggedDir4]);
        if(draggedDir4==swDown3){
            temp.y=val.y;
            if(isShiftDown){temp.y+=one;}
            else{temp.y+=stepAbs.y*multiplier;}
            if(temp.y>max.y){beep();}
            else{setyNoCheck(temp.y);}
        }
        else if(draggedDir4==swLeft2){
            temp.x=val.x;
            if(isShiftDown){temp.x-=one;}
            else{temp.x-=stepAbs.x*multiplier;}
            if(temp.x<min.x){beep();}
            else{setxNoCheck(temp.x);}
        }
        else if(draggedDir4==swRight4){
            temp.x=val.x;
            if(isShiftDown){temp.x+=one;}
            else{temp.x+=stepAbs.x*multiplier;}
            if(temp.x>max.x){beep();}
            else{setxNoCheck(temp.x);}
        }
        else if(draggedDir4==swUp1){
            temp.y=val.y;
            if(isShiftDown){temp.y-=one;}
            else{temp.y-=stepAbs.y*multiplier;}
            if(temp.y<min.y){beep();}
            else{setyNoCheck(temp.y);}           
        }
        //else{System.out.println("error 0...expecting 1,2,3,4");}
    }   
   
   
   
   
   
}

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