Results 1 to 5 of 5
  1. #1
    frenkelor is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default mouse Released no work!!

    hi all
    i'm trying to a function that listen to the mouse
    basically i want to start drawing square till the user released the mouse
    and than the square will disappear
    this is my function please help

    Java Code:
         public void StartDelete()
        {
                        _pointX = new int[4];
                        _pointY = new int[4];
    
    
                      addMouseListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
                    if(_clearFlag)
                    {
    
    
                         _pointX[3] = e.getX();
                          _pointY[3]= e.getY();
                       
    
                    }
                }
            });
            addMouseMotionListener(new MouseAdapter(){
    
                public void mouseDragged(MouseEvent e){
                    if(_clearFlag)
                    {
                           _pointX[0]=(e.getX()-_pointX[3])+_pointX[3];
                           _pointY[0]=_pointY[3];
                           _pointX[1]= e.getX();
                           _pointY[1] = e.getY();
                           _pointX[2]=_pointX[3];
                           _pointY[2]=e.getY();
                          
                           repaint();
                                 
                }
                }
            });
                  addMouseMotionListener(new MouseAdapter(){
    
                public void mouseReleased(MouseEvent e){
                    if(_clearFlag)
                    {
                           _pointX=null;
                           _pointY=null;
    
                           repaint();
    
                }
                }
            });
    
    
         
    
        }
    in run time this mouseReleased dosent work he wont get in to this func when i Release the button
    10X

  2. #2
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    Can you poste whole code, please?

  3. #3
    frenkelor is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package map;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.event.MouseMotionListener;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.LinkedList;
    import java.util.Scanner;
    import java.util.Vector;
    import javax.swing.JFileChooser;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import mycontrolpanel.robot_sonar;
    
    /**
     *
     * @author orfrenkel
     */
    public class Data_Map extends JPanel implements ItemListener,MouseListener,MouseMotionListener{
    
        private int _panelMiddelXPos ;
        private int _panelMiddelYPos ;
        private  JFileChooser _fileChooser;
        private Vector _Objects;
        private  Vector   _Robot_position;
        private static  Data _obstacleDataPoints ;
        private static  Data _yowDataPoints;
        private robot_sonar _robot_sonar;//add by or
        private MouseMotionAdapter deletePoint;
        private int _x=0 , _y=0;
        private Dimension _dim;
        private int _width;
        private int _height;
        private boolean _drawFlag = false;  //if the user wont to draw on the panel
        private boolean _clearFlag =false;  //if the user wont to delete points from the panel
        private JLabel _MouseTracker;       //first set in the setMouseTracker function
        private int[] _pointX= null;
        private int[] _pointY=null;
      //  private MouseMotionAdapter l;
    
        public Data_Map(Dimension dim)
        {
             //Set up the scroll pane.
                     _dim = dim;
                     _width=_dim.width;
                     _height=_dim.height;
                     _panelMiddelXPos=_width/2;
                     _panelMiddelYPos=_height/2;
                     setSize(_dim);
                      _obstacleDataPoints =new Data(_height) ; //check if it will drow in the eges
                      _yowDataPoints = new Data(_height);
                       _robot_sonar = new robot_sonar();
                     setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
                     setMaximumSize(new java.awt.Dimension(_dim));
                     setPreferredSize(new java.awt.Dimension(_width,_height));
                     addMouseMotionListener(this);
                     repaint();
        }
        public void SetDim(final int width,final int heigth)
            {
                    _dim=new Dimension(width, heigth);
                    _height=_dim.height;
                    _width=_dim.width;
             }
        @Override
        public void paintComponent(Graphics g){
    
            super.paintComponent(g);
            //////////////////////////////--------------------Display obstacle points----------------//////////////////////////////////////////////////
            g.setColor(Color.BLACK);
            LinkedList[] positive =  new LinkedList[_obstacleDataPoints.getPositiveArraySize()] ;
            LinkedList[] negative = new LinkedList[_obstacleDataPoints.getNegativeArraySize()];
            negative = _obstacleDataPoints.getNegativeNumbers();
            positive = _obstacleDataPoints.getPositiveNumbers();
            
            for(int i=0;i<positive.length-1;i++)
            {
                  PointX x;
               for(int j=0;j<positive[i].size();j++)
               {
                    x= (PointX)positive[i].get(j);
                 //  IfPointOutOfFrame(x.getX(),i);
    
                    g.fillOval(ConvertXRobotDataToDraw(x.getX()),ConvertYRobotDataToDraw(i), 4, 4);
    
    
               }
                
            }
                    for(int i=1;i<negative.length-1;i++)
            {
                  PointX x;
               for(int j=0;j<negative[i].size();j++)
               {
                    x= (PointX)negative[i].get(j);
                 //  IfPointOutOfFrame(x.getX(),i);
    
                    g.fillOval(ConvertXRobotDataToDraw(x.getX()),ConvertYRobotDataToDraw(-i), 4, 4);
    
    
               }
    
            }
    if(_pointX!=null&&_pointY!=null)
        g.drawPolygon(_pointX, _pointY, 4);
    
     }
        public void IfPointOutOfPanel(final int x,final int y)
        {
            /*
             *this function check if need to increase the panel size by the time the robot
             * kipe wrighting points
             *
             * @arg axis y and axis x
             **/
    
           if(x<0 ||x>_width || y<0 ||y>_height)
           {
               SetDim(_width*2, _width*2);
               setSize(_dim);
                repaint();
           }
        }
        private int ConvertXRobotDataToDraw(final int x)
        {
            /*
             *this funtion get integer point from the robot and convert to the
             *Axis system to the one you have in the panel axis.
             * @arg axis y
             **/
            if(x>0)
            {
                return (_panelMiddelXPos+x);
            }
            if(x<0)
                return _panelMiddelXPos-Math.abs(x);
            else return _panelMiddelXPos;
        }
        private int ConvertYRobotDataToDraw(final int y)
        {
            /*
             *this funtion get integer point from the robot and convert to the
             *Axis system to the one you have in the panel axis.
             * @arg axis y
             **/
                if (y>0)
                    return _panelMiddelYPos-y;
                if(y<0)
                    return _panelMiddelYPos+Math.abs(y);
                else
                    return _panelMiddelYPos;
        }
        private int ConvertXDrawToRobotData(final int x)
        {
            /*
             *this funtion get integer point from the panel point
             *Axis system and convert this integer to the point that the robot know
             * @arg axis x
             **/
        if(x!=_panelMiddelXPos)
               return x-_panelMiddelXPos;
            else
                return 0;
        }
        private int ConvertYDrawToRobotData(final int y)
        {
            /*
             *this funtion get integer point from the panel point
             *Axis system and convert this integer to the point that the robot know
             * @arg axis y
             **/
            if(y!=_panelMiddelYPos)
                return _panelMiddelYPos-y;
            else
                return 0;
        }
    
    public void Get_input_File() throws FileNotFoundException, IOException
        {
                   //... Open a file dialog.
           _fileChooser = new JFileChooser();
                int retval = _fileChooser.showOpenDialog(_fileChooser);
                if (retval == JFileChooser.APPROVE_OPTION) {
                    //... The user selected a file, get it, use it.
                    File file = _fileChooser.getSelectedFile();
                    _obstacleDataPoints.deleteData(/*need to addd*/);
                    _yowDataPoints.deleteData(/*need to add*/);
                    repaint();
                    //... Update user interface.
                    ReadFile(file);
    
         }
        }
    public void ReadFile(File file)    /////Add by Tal
        {
        try {
    
            Scanner in = new Scanner(file);
                String[] arr = new String[2];
                boolean flag = true;
                while (in.hasNextLine()) {
                    String word = in.nextLine();  // Read a "token".
                 if(!(word.contains("s")))
                 {
                     if(flag){
                  arr= word.split(",");
                  arr[0] = arr[0].substring(1);
                  arr[1] = arr[1].substring(0, arr[1].length()-1);
                   if(arr[1].contains("-"))                                     ///////////Negative number
                   {
                     int x= Integer.parseInt(arr[0].trim());
                     int y = Integer.parseInt(arr[1].trim());
                       _obstacleDataPoints.add(x, y);
                   }
                   else
                   {
                      int x1 = Integer.parseInt(arr[0].trim());
                      int y1 = Integer.parseInt(arr[1].trim());
                      _obstacleDataPoints.add(x1, y1);
                   }
                     }
                     else
                     {
                   arr= word.split(",");
                  arr[0] = arr[0].substring(1);
                  arr[1] = arr[1].substring(0, arr[1].length()-1);
                   if(arr[1].contains("-"))                                             ///////////Negative number
                   {
                     int x= Integer.parseInt(arr[0].trim());
                     int y = Integer.parseInt(arr[1].trim());
                       _yowDataPoints.add(x, y);
                   }
                   else
                   {
                      int x1 = Integer.parseInt(arr[0].trim());
                      int y1 = Integer.parseInt(arr[1].trim());
                      _yowDataPoints.add(x1, y1);
                   }
                     }
                 }
                 else if(!(word.contains("c")))                      ////////yow points
                 {
                     flag=false;
                 }
                }
                repaint();
                in.close();        // Close Scanner's file.
         }
        catch (FileNotFoundException fnfex) {}
        }
     public  void SaveFile() {
          _fileChooser = new JFileChooser();
           int retval =_fileChooser.showSaveDialog((java.awt.Component) null);
                if (retval == JFileChooser.APPROVE_OPTION) {
                      try{
                                    // Create file
                                    FileWriter fstream = new FileWriter(_fileChooser.getSelectedFile());
                                     BufferedWriter out = new BufferedWriter(fstream);
              /////////////////////////////////////////////////////----------Write to file------/////////////////////////////////////////////////////////////////////
            LinkedList[] positive =  new LinkedList[_obstacleDataPoints.getPositiveNumbers().length] ;
            LinkedList[] negative = new LinkedList[_obstacleDataPoints.getNegativeNumbers().length];
            negative = _obstacleDataPoints.getNegativeNumbers();
            positive = _obstacleDataPoints.getPositiveNumbers();
            out.write("Start display obstacle  points");
            out.newLine();
            for(int j=0;j<positive.length-1;j++)
            {
                 PointX x;
                if(positive[j].size()!=0)
                {
                   for(int z=0;z<positive[j].size();z++)
                   {
                            x = (PointX)positive[j].get(z);
                           out.write("("+x.getX()+" ," + j+")");
                           out.newLine();
                   }
                }
            }
            ///////////////----------negative array-----------///////////////////
            for(int s=0;s<negative.length-1;s++)
            {
                 PointX x_negative;
                if(negative[s].size()!=0)
                {
                   for(int z=0;z<negative[s].size();z++)
                   {
                            x_negative = (PointX)negative[s].get(z);
                            out.write("("+x_negative.getX()+" ," + s*(-1)+")");
                            out.newLine();
                   }
                }
            }
            /////////////////////////////////------------------------Display Yow points----/----------------------///////////////////////////////////////////
            out.write("Start display yow points");
            out.newLine();
             positive =  new LinkedList[_yowDataPoints.getPositiveNumbers().length] ;
            negative = new LinkedList[_yowDataPoints.getNegativeNumbers().length];
            negative = _yowDataPoints.getNegativeNumbers();
            positive = _yowDataPoints.getPositiveNumbers();
            for(int j=0;j<positive.length-1;j++)
            {
                 PointX x;
                if(positive[j].size()!=0)
                {
                   for(int z=0;z<positive[j].size();z++)
                   {
                            x = (PointX)positive[j].get(z);
                            out.write("("+x.getX()+" ," +j +")");
                            out.newLine();
                   }
                }
            }
            ///////////////----------negative array-----------///////////////////
            for(int s=0;s<negative.length-1;s++)
            {
                 PointX x_negative;
                if(negative[s].size()!=0)
                {
                   for(int z=0;z<negative[s].size();z++)
                   {
                            x_negative = (PointX)negative[s].get(z);
                            out.write("("+ x_negative.getX()+" ," +s*(-1)+")");
                            out.newLine();
                   }
                }
            }
             ///////////////////// ////-------------Close the output stream-----------////////////////////////////////
                                     out.close();
                    }
                     catch (Exception e){
                                     System.err.println("Error: " + e.getMessage());
                     }
              }
    
      }
    public void itemStateChanged(ItemEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
         public void stopDelete()
        {
             removeMouseMotionListener(deletePoint);
        }
         public void StartDelete()
        {
                        _pointX = new int[4];
                        _pointY = new int[4];
    
    
                      addMouseListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
                    if(_clearFlag)
                    {
    
    
                         _pointX[3] = e.getX();
                          _pointY[3]= e.getY();
                       
    
                    }
                }
            });
            addMouseMotionListener(new MouseAdapter(){
    
                public void mouseDragged(MouseEvent e){
                    if(_clearFlag)
                    {
                           _pointX[0]=(e.getX()-_pointX[3])+_pointX[3];
                           _pointY[0]=_pointY[3];
                           _pointX[1]= e.getX();
                           _pointY[1] = e.getY();
                           _pointX[2]=_pointX[3];
                           _pointY[2]=e.getY();
                          
                           repaint();
                                 
                }
                }
            });
                  addMouseMotionListener(new MouseAdapter(){
    
                public void mouseReleased(MouseEvent e){
                    if(_clearFlag)
                    {
                           _pointX=null;
                           _pointY=null;
    
                           repaint();
    
                }
                }
            });
    
    
         
    
        }
      public void StartDraw()
        {
           addMouseListener(new MouseAdapter(){
                @Override
                public void mousePressed(MouseEvent e){
                    if(_drawFlag)
                    {
                          _x = e.getX();
                           _y = e.getY();
                        _obstacleDataPoints.add(ConvertXDrawToRobotData(_x),ConvertYDrawToRobotData(_y));
                        repaint();
    
                    }
                }
            });
    
            addMouseMotionListener(new MouseAdapter(){
                @Override
                public void mouseDragged(MouseEvent e){
                    if(_drawFlag)
                    {
                          _x = e.getX();
                           _y = e.getY();
                           _obstacleDataPoints.add(ConvertXDrawToRobotData(_x),ConvertYDrawToRobotData(_y));
                           repaint();
                    }
                }
            });
        }
    public boolean  getDrawFlag()
    {
    
        return _drawFlag;
    }
    public void setDrawFlag(final boolean flag)
    {
        _drawFlag=flag;
    }
    public boolean  getClearFlag()
    {
    
        return _clearFlag;
    }
    public void setClearFlag(final boolean flag)
    {
        _clearFlag=flag;
    }
    public void setMouseTracker(JLabel label)
        {
        /*
         *get the JLable from the control panel
         *and change him to the position of the mouse
         */
            _MouseTracker=label;
        }
    
        public void mouseDragged(MouseEvent e) {
          _MouseTracker.setText("X : "+ConvertXDrawToRobotData(e.getX())+" , Y : "+ConvertYDrawToRobotData(e.getY()) );
        }
    
        public void mouseMoved(MouseEvent e) {
              _MouseTracker.setText("X : " +ConvertXDrawToRobotData(e.getX())+" , Y : "+ConvertYDrawToRobotData(e.getY()) );
        }
    
        public void mouseClicked(MouseEvent e) {
            throw new UnsupportedOperationException("mouseClicked.");
        }
    
        public void mousePressed(MouseEvent e) {
            throw new UnsupportedOperationException("mousePressed.");
        }
    
        public void mouseReleased(MouseEvent e) {
            throw new UnsupportedOperationException("or");
        }
    
        public void mouseEntered(MouseEvent e) {
            throw new UnsupportedOperationException("mouseEntered.");
        }
    
        public void mouseExited(MouseEvent e) {
            throw new UnsupportedOperationException("mouseExited.");
        }
    
    }

  4. #4
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    by whole code he meant sscce

    and btw: this is a wall of code. i copy and pasted the whole thing and it didnt even compile; so please provide an sscce
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  5. #5
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Java Code:
    addMouseMotionListener(new MouseAdapter(){
                public void mouseReleased(MouseEvent e){
                    if(_clearFlag)
                    {
                           _pointX=null;
                           _pointY=null;
                           repaint();
                    }
                }
    });
    A MouseMotionListener never calls mouseReleased.

    This sort of error can be somewhat avoided by never using Adapters.
    My own approach is to first write
    Java Code:
    addMouseListener(new MouseListener() {});
    The IDE complains I have not created the abstract methods.
    I ask it to please do so and it does.
    It creates the headers for mousePressed and mouseReleased
    and I fill in the procedure bodies.
    (I also remove the spurious contents of the other methods it adds.)

Similar Threads

  1. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 05:34 AM
  2. JComponentPack 3.1 has been Released!
    By zfqjava in forum Java Software
    Replies: 0
    Last Post: 09-23-2009, 05:59 AM
  3. mouse click do not work after repaint
    By nobody in forum Java 2D
    Replies: 8
    Last Post: 12-07-2008, 05:43 PM
  4. Natural CLI 1.2.2 released (bug fix)
    By ferranb in forum Java Software
    Replies: 0
    Last Post: 07-20-2008, 12:37 AM
  5. VTD-XML 2.3 released
    By Jimmy Zhang in forum Java Software
    Replies: 0
    Last Post: 03-03-2008, 10:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •