Package kartoMNT

Source Code of kartoMNT.ContourDialog$MyDocumentListener

/**
* Boite de dialogue permettant de d�finir les param�tres de d�t�ction des lignes de niveaux
*
* @author HEIT Sylvestre & Coustillac Laurent
*
*
* Programme kartoMNT : g�n�ration de MNT � partir d'une carte de lignes de niveaux
* Copyright (C) 2002  HEIT Sylvestre & Coustillac Laurent
*
* Ce programme est libre, vous pouvez le redistribuer et/ou le modifier selon les termes de la Licence
*Publique G�n�rale GNU publi�e par la Free Software Foundation (version 2).
*
*Ce programme est distribu� car potentiellement utile, mais SANS AUCUNE GARANTIE, ni explicite ni implicite,
*y compris les garanties de commercialisation ou d'adaptation dans un but sp�cifique. Reportez-vous � la
*Licence Publique G�n�rale GNU pour plus de d�tails.
*
*Vous devez avoir re�u une copie de la Licence Publique G�n�rale GNU en m�me temps que ce programme ; si
*ce n'est pas le cas, �crivez � la
*Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, �tats-Unis.
*/
package kartoMNT;


import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.image.*;
import kartoMNT.karto.outils.MsgInfo;


/**
*boite de dialogue permettant de selectionner une couleur en RGB ou YUV
*
*/
public class ContourDialog extends DocumentDialog implements ColorListener,ActionListener,ComponentListener,SelectDocumentListener
    {
        public ContourDialog(JFrame parent,DocumentKartoContainer cont,ActionToolContextual _actionSelectColor,
                             JToggleButton _button)
        {
            super(parent);
            setTitle(ResourceManager.get("detect"));
            mainWindow=cont;
            button=_button;
            JPanel mainpanel=new JPanel();
            jpanel=new JPanel();

            jpanel.setBorder(BorderFactory.createCompoundBorder(
                                 BorderFactory.createTitledBorder(ResourceManager.get("borderColor")),
                                 BorderFactory.createEmptyBorder(2,2,2,2)));

            actionSelectColor=_actionSelectColor;

            addComponentListener(this);
            //addWindowListener(new MyWindowAdapter(actionSelectColor,this));
            ((ToolSelectColor)actionSelectColor.getTool()).addColorListener(this);

            mainpanel.setLayout(new BorderLayout());

            getContentPane().add(mainpanel);
            mainpanel.add(jpanel,BorderLayout.CENTER);


            documentKarto=null;

            gridbag=new GridBagLayout();
            GridBagConstraints constraint = new GridBagConstraints();
            jpanel.setLayout(gridbag);

            constraint.fill=GridBagConstraints.NONE;
            constraint.anchor =GridBagConstraints.WEST ;
            constraint.insets=new Insets(4,4,4,4);

            constraint.gridwidth = 1;
            constraint.weightx=constraint.weighty=0;

            // selection de couleur

            addc(button,constraint);
            button.setText(null);


            color=new JPanel();
            color.setPreferredSize(button.getPreferredSize());
            color.setBackground(new Color(0f,0f,0f));
            constraint.gridwidth = GridBagConstraints.REMAINDER ;
            addc(color,constraint);


            //////////////////////////////////////
            // selection couleur RGB            //
            //////////////////////////////////////
            constraint.gridwidth = 1;
            constraint.weightx=constraint.weighty=0;

            JLabel c1=new JLabel("R");
            c1.setForeground(new Color(1.0f,0.0f,0.0f));
            addc(c1,constraint);

            tR=new JTextField("0");
            tR.setPreferredSize(new Dimension(30,(int)(tR.getPreferredSize().getHeight())));
            addc(tR,constraint);

            JLabel c2=new JLabel("G");
            c2.setForeground(new Color(0.0f,1.0f,0.0f));
            addc(c2,constraint);

            tG=new JTextField("0");
            tG.setPreferredSize(new Dimension(30,(int)(tG.getPreferredSize().getHeight())));
            addc(tG,constraint);

            JLabel c3=new JLabel("B");
            c3.setForeground(new Color(0.0f,0.0f,1.0f));
            addc(c3,constraint);

            tB=new JTextField("0");
            tB.setPreferredSize(new Dimension(30,(int)(tB.getPreferredSize().getHeight())));
            constraint.gridwidth = GridBagConstraints.REMAINDER ;
            addc(tB,constraint);


            //////////////////////////////////////
            // selection couleur YUV            //
            //////////////////////////////////////
            constraint.gridwidth = 1;
            constraint.weightx=constraint.weighty=0;

            addc(new JLabel("Y"),constraint);

            tY=new JTextField("0");
            tY.setPreferredSize(new Dimension(30,(int)(tY.getPreferredSize().getHeight())));
            addc(tY,constraint);

            addc(new JLabel("U"),constraint);

            tU=new JTextField("0");
            tU.setPreferredSize(new Dimension(30,(int)(tU.getPreferredSize().getHeight())));
            addc(tU,constraint);

            addc(new JLabel("V"),constraint);
            tV=new JTextField("0");
            tV.setPreferredSize(new Dimension(30,(int)(tV.getPreferredSize().getHeight())));
            constraint.gridwidth = GridBagConstraints.REMAINDER ;///
            addc(tV,constraint);


            //////////////////////////////////////
            // selection intervalle d'erreur    //
            //////////////////////////////////////
            constraint.gridwidth = GridBagConstraints.REMAINDER;
            addc(new JLabel(ResourceManager.get("errInt")),constraint);

            constraint.weightx=constraint.weighty=0;
            constraint.gridwidth = 1;

            addc(new JLabel("+/-"),constraint);

            delta1=new JTextField("60");
            delta1.setPreferredSize(new Dimension(30,(int)(delta1.getPreferredSize().getHeight())));
            addc(delta1,constraint);

            addc(new JLabel("+/-"),constraint);

            delta2=new JTextField("30");
            delta2.setPreferredSize(new Dimension(30,(int)(delta2.getPreferredSize().getHeight())));
            addc(delta2,constraint);

            addc(new JLabel("+/-"),constraint);

            delta3=new JTextField("30");
            delta3.setPreferredSize(new Dimension(30,(int)(delta3.getPreferredSize().getHeight())));
            constraint.gridwidth = GridBagConstraints.REMAINDER ;
            addc(delta3,constraint);


            //////////////////////////////////////////
            // boutons selection espace de couleur  //
            //////////////////////////////////////////
            addc(cYUV=new JCheckBox(ResourceManager.get("detecYUV")),constraint);
            addc(cRGB=new JCheckBox(ResourceManager.get("detecRGB")),constraint);
            ButtonGroup bg=new ButtonGroup();
            bg.add(cYUV);
            bg.add(cRGB);
            cYUV.setSelected(true);

            ////////////////////////////////////////
            // boutons OK APPLIQUER ANNULER       //
            ////////////////////////////////////////
            constraint.weightx=constraint.weighty=0;
            constraint.gridwidth = GridBagConstraints.REMAINDER;
            JPanel panel1=new JPanel();
            panel1.setLayout(new FlowLayout());
            panel1.add(ok=new JButton(ResourceManager.get("OkKey")));
            panel1.add(apply=new JButton(ResourceManager.get("apply")));
            panel1.add(cancel=new JButton(ResourceManager.get("CancelKey")));

            mainpanel.add(panel1,BorderLayout.SOUTH);

            ok.addActionListener(this);
            apply.addActionListener(this);
            cancel.addActionListener(this);
            ok.setEnabled(documentKarto!=null);
            apply.setEnabled(documentKarto!=null);

            pack();
            //centre la fenetre
            Util.centrer(this);

            jpanel.setLayout(null);

            updateYUV=false;
            updateRGB=false;
            MyDocumentListener listenerRGB=new MyDocumentListener()
                                           {
                                               public void run()
                                               {
                                                   updateFromRGB();
                                               }
                                           };
            tR.getDocument().addDocumentListener(listenerRGB);
            tG.getDocument().addDocumentListener(listenerRGB);
            tB.getDocument().addDocumentListener(listenerRGB);

            MyDocumentListener listenerYUV=new MyDocumentListener()
                                           {
                                               public void run()
                                               {
                                                   updateFromYUV();
                                               }
                                           };
            tY.getDocument().addDocumentListener(listenerYUV);
            tU.getDocument().addDocumentListener(listenerYUV);
            tV.getDocument().addDocumentListener(listenerYUV);

        }

        void addc(JComponent comp,GridBagConstraints c)
        {
            gridbag.setConstraints(comp, c);
            jpanel.add(comp);
        }



        public void documentSelected(Document doc)
        {
            documentKarto=(DocumentKarto)doc;
            ok.setEnabled(documentKarto!=null);
            apply.setEnabled(documentKarto!=null);
        }


        public void colorSelected(Color c)
        {
            tR.setText(new Integer(c.getRed()).toString());
            tG.setText(new Integer(c.getGreen()).toString());
            tB.setText(new Integer(c.getBlue()).toString());
            int[] tab=ImageOperator.RGB2YUV (c.getRed(),c.getGreen(),c.getBlue());
            tY.setText(new Integer(tab[0]).toString());
            tU.setText(new Integer(tab[1]).toString());
            tV.setText(new Integer(tab[2]).toString());
            color.setBackground(c);
        }

        void detect()
        {
            if(documentKarto==null)
                {
                    return;
                }
            try
                {
                    BufferedImage srcimg=documentKarto.getCarte();
                    if(srcimg==null)
                        {
                            return;
                        }
                       
                    // Modifi� afin de sauvegarder les lignes pr�c�dentes pour
                    // superposer les nouvelles
                    LignesDeNiveaux dstimg=documentKarto.getLignesDeNiveaux();
                   
                    if(cYUV.isSelected())
                        {
                            int r,g,b;
                            r=Integer.parseInt(tR.getText());
                            g=Integer.parseInt(tG.getText());
                            b=Integer.parseInt(tB.getText());
                            int[] tab=ImageOperator.RGB2YUV (r,g,b);

                            dstimg=ImageOperator.colorFilter(dstimg,ImageOperator.RGB2YUV(srcimg),
                                                             tab[0],Integer.parseInt(delta1.getText()),
                                                             tab[1],Integer.parseInt(delta2.getText()),
                                                             tab[2],Integer.parseInt(delta3.getText()));
                            /*dstimg=ImageOperator.detect(dstimg,ImageOperator.RGB2YUV(srcimg),
                            tab[0],Integer.parseInt(delta1.getText()),
                            tab[1],Integer.parseInt(delta2.getText()),
                            tab[2],Integer.parseInt(delta3.getText()),
                            60,1000,1000,
                            Double.parseDouble(alpha.getText()),
                            Double.parseDouble(seuil.getText()) );*/
                            /*Integer.parseInt(delta1.getText()),
                            Integer.parseInt(delta2.getText()),
                            Integer.parseInt(delta3.getText())*/

                        }
                    else
                        {
                            dstimg=ImageOperator.colorFilter(dstimg,srcimg,
                                                             Integer.parseInt(tR.getText()),Integer.parseInt(delta1.getText()),
                                                             Integer.parseInt(tG.getText()),Integer.parseInt(delta2.getText()),
                                                             Integer.parseInt(tB.getText()),Integer.parseInt(delta3.getText()));
                        }

                    documentKarto.setLignesDeNiveaux(dstimg);

                }
            catch(NumberFormatException ne)
                {
                    MsgInfo msg=new MsgInfo(frame,MsgInfo.ERROR,MsgInfo.OK,ResourceManager.get("fillError"));

                }

        }


        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==cancel)
                {
                    dispose();
                }
            if(e.getSource()==ok)
                {
                    detect();
                    dispose();
                }
            if(e.getSource()==apply)
                {
                    detect();
                }
        }

        void updateFromRGB()
        {
            if(updateYUV)
                return;
            try
                {
                    int R=Integer.parseInt(tR.getText());
                    int G=Integer.parseInt(tG.getText());
                    int B=Integer.parseInt(tB.getText());
                    if (!((R<=255)&&(R>=0)&&(G<=255)&&(G>=0)&&(B<=255)&&(B>=0)))
                        return;
                    updateRGB=true;
                    Color col=new Color(Integer.parseInt(tR.getText()),
                                        Integer.parseInt(tG.getText()),
                                        Integer.parseInt(tB.getText())
                                       );
                    int[] tab=ImageOperator.RGB2YUV (col.getRed(),col.getGreen(),col.getBlue());
                    tY.setText(new Integer(tab[0]).toString());
                    tU.setText(new Integer(tab[1]).toString());
                    tV.setText(new Integer(tab[2]).toString());
                    color.setBackground(col);
                }
            catch(NumberFormatException ne)
                {
                    updateRGB=false;
                }
            updateRGB=false;
        }

        void updateFromYUV()
        {
            if(updateRGB)
                return;
            try
                {
                    int Y=Integer.parseInt(tY.getText());
                    int U=Integer.parseInt(tU.getText());
                    int V=Integer.parseInt(tV.getText());
                    if (!((Y<=255)&&(Y>=0)&&(U<=255)&&(U>=0)&&(V<=255)&&(V>=0)))
                        return;

                    updateYUV=true;
                    int[] tab=ImageOperator.YUV2RGB(Y,U,V);

                    Color col=new Color(tab[0],
                                        tab[1],
                                        tab[2]
                                       );

                    tR.setText(new Integer(tab[0]).toString());
                    tG.setText(new Integer(tab[1]).toString());
                    tB.setText(new Integer(tab[2]).toString());
                    color.setBackground(col);
                }
            catch(NumberFormatException ne)
                {
                    updateYUV=false;
                }
            updateYUV=false;
        }





        public void componentHidden(ComponentEvent e)
        {}

        public void componentMoved(ComponentEvent e)
        {}

        public void componentResized(ComponentEvent e)
        {}

        public void componentShown(ComponentEvent e)
        {//simuler click
            button.setSelected(true);
            actionSelectColor.actionPerformed(new ActionEvent(button,0,"selected"));

        }




        protected class MyDocumentListener implements DocumentListener
            {
                public void changedUpdate(DocumentEvent e)
                {
                    run();
                }
                public void insertUpdate(DocumentEvent e)
                {
                    run();
                }
                public void removeUpdate(DocumentEvent e)
                {
                    run();
                }
                public void run()
                {}
            }

        boolean updateYUV,updateRGB;
        protected JTextField tR,tG,tB,delta1,delta2,delta3,tY,tU,tV,  alpha,seuil;
        protected DocumentKarto documentKarto;
        protected GridBagLayout gridbag;
        protected JPanel jpanel,color;
        protected ActionToolContextual actionSelectColor;
        JButton ok,apply,cancel;
        JToggleButton button;
        DocumentKartoContainer mainWindow;
        JCheckBox cRGB,cYUV;


    }
TOP

Related Classes of kartoMNT.ContourDialog$MyDocumentListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.