problem in attaching mouselistener on a imagepanel
cindrella ss
Greenhorn
Joined: May 30, 2006
Posts: 6
posted
0
hi all, i am trying to add a mouselistener in a panel which is a part of a frame.....but the listener is not responding to click of mouse........please help me in finding where i m wrong.....
buttonpanel.add(wordButton); content.add(buttonpanel,"South"); // Create JPanel canvas to hold the picture imagepanel = new DrawingPanel(); //imagepanel.addMouseListener(new MouseClickListener());
// Create JScrollPane to hold the canvas containing the picture JScrollPane scroller = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scroller.setPreferredSize(new Dimension(500,300)); scroller.setViewportView(imagepanel); scroller.setViewportBorder( BorderFactory.createLineBorder(Color.black)); // Add scroller pane to Panel content.add(scroller,"Center");
class DrawingPanel extends JPanel { int harr[]=new int[100]; int varr[]=new int[100]; int x,y; public DrawingPanel() { for(int i=0;i<50;i++) harr[i]=varr[i]=0; setBackground(Color.white); setPreferredSize(new Dimension(750,950)); MouseClickListener listener= new MouseClickListener(); addMouseListener(listener);
}
protected void paintComponent(Graphics g) { super.paintComponent(g); if (img!=null) { // resizing the JPanel to the pic size //int width = img.getWidth(this); //int height = img.getHeight(this); //imagepanel.setPreferredSize(new Dimension(width,height)); // printing the image on the panel g.drawImage(img, 0, 0, this);
} //darwing line at position given by mouse click if(click==1) g.drawLine(0,y,img.getHeight(imagepanel),y);
}
public void sethLine(int arre[] ) { for(int i=0;i<hcount;i++) this.harr[i]=arre[i]; repaint(); } public void setvLine(int arre[]) { for(int i=0;i<vcount;i++) this.varr[i]=arre[i]; repaint(); } public void setaLine(int x,int y) { repaint(); } }
class MouseClickListener extends MouseAdapter { public void MouseClicked(MouseEvent e) {int mouseX=e.getX(); int mouseY=e.getY(); click=1; System.out.println(mouseX); System.out.println(mouseY); imagepanel.setaLine(mouseX,mouseY);
} }
// OpenAction class OpenAction implements ActionListener { public void actionPerformed(ActionEvent ae) { //... Open a file dialog. int retval = m_fileChooser.showOpenDialog(openpic2.this); if (retval == JFileChooser.APPROVE_OPTION) { //... The user selected a file, process it. File file = m_fileChooser.getSelectedFile(); // store the file path in a string and send to JNI String s = file.getPath(); System.out.println("The Path is:\n" + s); //... Update user interface. img = Toolkit.getDefaultToolkit().getImage(file.getPath()); //if(img!= null) // loadimage(); m_fileNameTF.setText(file.getName()); imagepanel.repaint(); } }
} //this tries to build lines class LineAction implements ActionListener { public void actionPerformed(ActionEvent e) { int width = img.getWidth(imagepanel); int height = img.getHeight(imagepanel); flag=0; try{ int array[]= new int[100]; int k=0; for(int x=0;x<=height;x=x+20) {array[k]=x;
class wordAction implements ActionListener { public void actionPerformed(ActionEvent e) { int width=img.getWidth(imagepanel); int height=img.getHeight(imagepanel);
flag=1; try{ int array[]= new int[100]; int k=0; for(int x=0;x<=width;x=x+20) {array[k]=x;
} /** Filter to work with JFileChooser to select java file types. **/ class JavaFilter extends javax.swing.filechooser.FileFilter { // this function is internally used for the Filtering action public boolean accept (File f) { return f.getName ().toLowerCase ().endsWith (".jpeg") || f.getName ().toLowerCase ().endsWith (".jpg") || f.getName ().toLowerCase ().endsWith (".gif") || f.isDirectory (); }
// this function is internally used for the Filter Option drop down menu public String getDescription () { return "*.jpeg, *.gif, *.jpg"; } } }
Why not add some "System.out.println(...)" debug statements inside the MouseListener to see if it's "working" or not?
It looks like all the method calls/event handling, etc. should be working OK - the problem is that all it's doing is calling a method that calls "repaint()" - and the X and Y values from the MouseEvent never make it anywhere useful. (They are saved in local values of the mouseClicked() method, then sent to setaLine() method... and not used in any way...)
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
4
posted
0
Please get your display name sorted out.
That is the problem with Adapter classes. Not only is Adapter spelt wrongly, but if there is the slightest error in spelling elsewhere . . .
Go and look in the proper place for getting mouse events, ie the java.awt.event.MouseListener interface, and implement that instead of using the Adapter classes. Then keep adding the methods until you don't get any more compiler errors. Far more reliable way to sort out Listeners. And check what the methods are called.
CAUTION: If you misspell the name of a method when extending an adapter class, then the compiler won't catch your error. For example, if you define a method windowIsClosing in a WindowAdapter class, then you get a class with eight methods, and the windowClosing method does nothing.
BTW: You ought to be calling your listener classes SomethingListener. And your use of catch(ArrayIndexOutOfBoundsException) is totally dreadful.