Results 1 to 5 of 5
Thread: mouse Released no work!!
- 01-23-2010, 09:12 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 2
- Rep Power
- 0
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
in run time this mouseReleased dosent work he wont get in to this func when i Release the buttonJava 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(); } } }); }
10X
- 01-23-2010, 09:51 AM #2
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
Can you poste whole code, please?
- 01-23-2010, 09:56 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 2
- Rep Power
- 0
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."); } }
- 01-23-2010, 11:21 AM #4
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 ?"
- 01-23-2010, 04:49 PM #5
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
A MouseMotionListener never calls mouseReleased.Java Code:addMouseMotionListener(new MouseAdapter(){ public void mouseReleased(MouseEvent e){ if(_clearFlag) { _pointX=null; _pointY=null; repaint(); } } });
This sort of error can be somewhat avoided by never using Adapters.
My own approach is to first writeThe IDE complains I have not created the abstract methods.Java Code:addMouseListener(new MouseListener() {});
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
-
Mouse Listener for mouse floating over object?
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 04:34 AM -
JComponentPack 3.1 has been Released!
By zfqjava in forum Java SoftwareReplies: 0Last Post: 09-23-2009, 04:59 AM -
mouse click do not work after repaint
By nobody in forum Java 2DReplies: 8Last Post: 12-07-2008, 04:43 PM -
Natural CLI 1.2.2 released (bug fix)
By ferranb in forum Java SoftwareReplies: 0Last Post: 07-19-2008, 11:37 PM -
VTD-XML 2.3 released
By Jimmy Zhang in forum Java SoftwareReplies: 0Last Post: 03-03-2008, 09:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks