Results 1 to 6 of 6

Thread: [resolved] Java get Mouse coordinates

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Resolved [resolved] Java get Mouse coordinates

    Hello, this is the code I was given to work with. My olny problem is that I am very new to java.

    Can some one help and tell me where I would put a MouseListener so I could get the current X-Y co-od's of the point when the mouse is clicked....but only after i load both (left/right) images ?? it would be very helpful.

    I think this code is long and complex for a begginner...So it's comming in 2 posts.

    Please help and thanks...
    Last edited by jlbovo; Nov 3rd, 2006 at 02:37 PM.
    --thanks for the help.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Re: Java get Mouse coordinates

    Code:
    //---------------------------------------------------------- 
    //  UIDDemo1 Code for 3731 User-Interface-Design
    //---------------------------------------------------------- 
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import javax.imageio.*;
    
    //----- the UIDDemo1 class ----------------------------------------
    
    public class UIDDemo1 extends JFrame {
    
    // ---- GUI components ---------------------------------------
    
       private JMenuBar   menuBar;
       private JMenu      fileMenu, toolMenu, helpMenu;
       private JMenuItem  openLeftImageItem, openRightImageItem, saveAsItem, exitItem;
       private JMenuItem  overlayItem, featMatchItem, eyeDistItem, moreFunItem;
       private JMenuItem  aboutItem, documentItem, searchItem;
       private JLabel     leftLabel, rightLabel, resultLabel;
       private JTextField leftDisplay, rightDisplay, resultDisplay;
       private String     fileName;  
       private ImagePanel leftImgPanel, rightImgPanel, resultImgPanel;
       private JPanel     leftP, rightP, resultP; 
       private JFileChooser   fc;
    
    // ---- constructor for static GUI layout  -----------
    
       public UIDDemo1 () {
         
         super("UIDDemo1");
         Container c = getContentPane();
         ActionEventHandler eh = new ActionEventHandler();
         fc = new JFileChooser();
    
         //set up file menu
         fileMenu           = new JMenu("File");
         openLeftImageItem  = new JMenuItem("Open Left Image");
         openRightImageItem = new JMenuItem("Open Right Image");
         saveAsItem         = new JMenuItem("Save As");
         exitItem           = new JMenuItem("Exit");
          
         openLeftImageItem.addActionListener(eh);
         openRightImageItem.addActionListener(eh);
         saveAsItem.addActionListener(eh);
         exitItem.addActionListener( new ActionListener() {
               public void actionPerformed(ActionEvent event) {
                  System.exit(0);
               }
            }
         );
    
         fileMenu.add(openLeftImageItem);
         fileMenu.add(openRightImageItem);
         fileMenu.add(saveAsItem);
         fileMenu.add(exitItem);
    
         //set up tool menu
         toolMenu      = new JMenu("Tools");    
         overlayItem   = new JMenuItem("Overlay Two Images");
         featMatchItem = new JMenuItem("Feature Matching");
         eyeDistItem = new JMenuItem("Eye Distance");
         moreFunItem   = new JMenuItem("More Functions");
         
         overlayItem.addActionListener(eh);
         featMatchItem.addActionListener(eh);
         eyeDistItem.addActionListener(eh);
         moreFunItem.addActionListener(eh);
    
         toolMenu.add(overlayItem);
         toolMenu.add(featMatchItem);
         toolMenu.add(eyeDistItem);
         toolMenu.add(moreFunItem);
    
         //set up help menu
         helpMenu     = new JMenu("Help");
         aboutItem    = new JMenuItem("About 3731 User Interface Design");
         documentItem = new JMenuItem("Documents");
         searchItem   = new JMenuItem("Search Index");
    
         aboutItem.addActionListener(eh);
         documentItem.addActionListener(eh);
         searchItem.addActionListener(eh);
    
         helpMenu.add(aboutItem);
         helpMenu.add(documentItem);
         helpMenu.add(searchItem);
    
         //setup the menu bar
         menuBar = new JMenuBar();
         setJMenuBar(menuBar);
    
         menuBar.add(fileMenu);
         menuBar.add(toolMenu);
         menuBar.add(helpMenu);
    
         //set image panels
         leftImgPanel   = new ImagePanel();
         rightImgPanel  = new ImagePanel();
         resultImgPanel = new ImagePanel();
    
         // labels
         leftLabel   = new JLabel("Left Image", JLabel.CENTER);
         rightLabel  = new JLabel("Right Image", JLabel.CENTER);
         resultLabel = new JLabel("Result", JLabel.CENTER);
    
         //message display fields
         leftDisplay   = new JTextField(30);
         rightDisplay  = new JTextField(30);
         resultDisplay = new JTextField(30);
         leftDisplay.setEditable(false);
         rightDisplay.setEditable(false);
         resultDisplay.setEditable(false);
    
         // left panel
         leftP = new JPanel();
         leftP.setLayout(new BorderLayout());
         leftP.add(leftLabel, BorderLayout.NORTH);
         leftP.add(new JScrollPane(leftImgPanel), BorderLayout.CENTER);
         leftP.add(leftDisplay, BorderLayout.SOUTH);
         
         // right panel
         rightP = new JPanel();
         rightP.setLayout(new BorderLayout());
         rightP.add(rightLabel, BorderLayout.NORTH);
         rightP.add(new JScrollPane(rightImgPanel), BorderLayout.CENTER);
         rightP.add(rightDisplay, BorderLayout.SOUTH);
    
         // result panel
         resultP = new JPanel();
         resultP.setLayout(new BorderLayout());
         resultP.add(resultLabel, BorderLayout.NORTH);
         resultP.add(new JScrollPane(resultImgPanel), BorderLayout.CENTER);
         resultP.add(resultDisplay, BorderLayout.SOUTH);
    
         //add three panels to the contentPane.
         c.setLayout(new GridLayout(1,3));
         c.add(leftP);
         c.add(rightP);
         c.add(resultP);
    
       } // end of constructor
    
    // -----------------------------------------------------------
       public static void main(String[] args) {     
         UIDDemo1 demo  = new UIDDemo1();
         demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         demo.setSize(1000, 600);
         demo.setVisible(true);
       }
    
    // ---- inner class for event handling  --------------------------
    
       private class ActionEventHandler implements ActionListener {   
         public void actionPerformed(ActionEvent e) {      
           if( e.getSource() == openLeftImageItem ) {
               fc.setCurrentDirectory(new File("."));
               int result = fc.showOpenDialog(UIDDemo1.this);
               if (result == JFileChooser.APPROVE_OPTION) {
                   fileName = fc.getSelectedFile().getPath();
                   System.out.println("fileName=   " + fileName);
                   leftImgPanel.setImage(fileName);
               } 
           }
           else if( e.getSource() == openRightImageItem ) {
               fc.setCurrentDirectory(new File("."));
               int result = fc.showOpenDialog(UIDDemo1.this);
               if (result == JFileChooser.APPROVE_OPTION) {
                   fileName = fc.getSelectedFile().getPath();
                   System.out.println("fileName=   " + fileName);
                   rightImgPanel.setImage(fileName);
               } 
           }
           else if( e.getSource() == saveAsItem) {
               //resultImgPanel.saveImage();
           }
           else if( e.getSource() == overlayItem) {
               double user_ratio1 = 0.5;
               double user_ratio2 = 0.5;
               resultImgPanel.setImage(resultImgPanel.overlay(leftImgPanel.getImage(), user_ratio1, rightImgPanel.getImage(), user_ratio2));
           }
           else if( e.getSource() == featMatchItem) {
               //method(); 
           }
           else if( e.getSource() == eyeDistItem) {
               JOptionPane.showMessageDialog (UIDDemo1.this,
                                              "ijijmijmpiojmk ",
                                              "UIDDemo1",
                                              JOptionPane.INFORMATION_MESSAGE);
               return;   
           }
           else if( e.getSource() == moreFunItem) {
               //method(); 
           }
           else if( e.getSource() == aboutItem) {
               JOptionPane.showMessageDialog (UIDDemo1.this,
                                              "This is a demo program for 3731-User-Interface-Design. \n Feel free to add new features. ",
                                              "UIDDemo1",
                                              JOptionPane.INFORMATION_MESSAGE);
               return;
           }
           else if( e.getSource() == documentItem) {
               //method(); 
           }
           else if( e.getSource() == searchItem) {
               //method(); 
           }
         }
       } // end of inner class.
    
    } // end of UIDDemo1 class
    
    
    
    // -------- A special panel used to show and process images --------------------------
    
    class ImagePanel extends JPanel {
    
       private BufferedImage currentbufimg; // this is the real image associated with each ImagePanel object.
    
       // The first time an ImagePanle ojbect is displayed on the window,
       // there is no image --> empty constructor.
       ImagePanel() {
          //System.out.println("empty parameter, doing nothing");
       }
    
       // Overloaded method setImage(), accepts a string parameter.
       // (1)Create an icon using the fileName.
       // (2)Create a BufferedImage (bufimg) and pass it to setImage method.
       // (3)Inside setImage method, bufimg will be used to update currentbufimg.
       // 
       public boolean setImage(String fileName){
    	ImageIcon imgicon = new ImageIcon(fileName);
       	Image img = imgicon.getImage();
    	int width  = img.getWidth(this);
    	int height = img.getHeight(this);
          System.out.println("width= "  + width);
          System.out.println("height= " + height);
    
    	if(width == -1 || height == -1) 
             return false;
    
    	BufferedImage bufimg     = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    	Graphics2D bufimgContext = bufimg.createGraphics();
    	bufimgContext.drawImage(img, 0, 0, null);
    	setImage(bufimg);
    
    	return true;
       }
    ........more to come.
    --thanks for the help.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Re: Java get Mouse coordinates

    Code:
    // (1)Overloaded method, accept a BufferedImage parameter.
       // (2)Update currentbufimg with the passed tembufimg.
       // (3)Show the updated currentbufimg.
       //
       public void setImage(BufferedImage tempbufimg){
    	if(tempbufimg==null)
             return;
    	this.currentbufimg=tempbufimg;
    	Dimension size=new Dimension(tempbufimg.getWidth(this),tempbufimg.getHeight(this));
    	setMinimumSize(size); 
          setPreferredSize(size);
    	repaint();
       }
       
       public void paintComponent(Graphics g){
    	super.paintComponent(g);
    	if(currentbufimg != null)
             g.drawImage(currentbufimg,0,0,this);
       }
    
       public BufferedImage getImage() { 
          return currentbufimg; 
       }
    
       // overlay method accepts two BufferedImage objects (img1 and img2)
       // and generate a new BufferedImage (img3) by overlaying img1 and img2
       // and return it to the calling method.
       //
       public BufferedImage overlay(BufferedImage img1, double ratio1, BufferedImage img2, double ratio2) {
          int rows = img1.getHeight();
          int cols = img1.getWidth();
    	BufferedImage img3 = new BufferedImage(cols, rows, BufferedImage.TYPE_INT_RGB);
          
          WritableRaster wr1 = img1.getRaster();
          WritableRaster wr2 = img2.getRaster();
          WritableRaster wr3 = img3.getRaster();
          
          // 0 for red band, 1 for green band, 2 for blue gand.
          int[] r1 = wr1.getSamples(0,0,cols,rows,0,(int[])null);  
          int[] g1 = wr1.getSamples(0,0,cols,rows,1,(int[])null);  
          int[] b1 = wr1.getSamples(0,0,cols,rows,2,(int[])null);       
    
          int[] r2 = wr2.getSamples(0,0,cols,rows,0,(int[])null);  
          int[] g2 = wr2.getSamples(0,0,cols,rows,1,(int[])null);
          int[] b2 = wr2.getSamples(0,0,cols,rows,2,(int[])null); 
    
          int[] r3 = wr3.getSamples(0,0,cols,rows,0,(int[])null);  
          int[] g3 = wr3.getSamples(0,0,cols,rows,1,(int[])null);  
          int[] b3 = wr3.getSamples(0,0,cols,rows,2,(int[])null);  
    
          int k;
          int minI = 100;
          int maxI = 130;
          int minJ = 100;
          int maxJ = 130;
          for(int i=0; i<rows; i++) {
          for(int j=0; j<cols; j++) {
              k = j + i*cols;
              if (minI<i && i<maxI && minJ<j && j<maxJ) {  // draw a red square.
                r3[k] = 255;
                g3[k] = 0;
                b3[k] = 0;
              }
              else {
                r3[k] = (int)(r1[k]*ratio1 + r2[k]*ratio2);
                g3[k] = (int)(g1[k]*ratio1 + g2[k]*ratio2);
                b3[k] = (int)(b1[k]*ratio1 + b2[k]*ratio2);
              }
              if (i==j) {  // draw a diagnoal line.
                r3[k] = 0;
                g3[k] = 255;
                b3[k] = 0;    
              }
          }
          }
    
          wr3.setSamples(0,0,cols,rows,0,r3);
          wr3.setSamples(0,0,cols,rows,1,g3);
          wr3.setSamples(0,0,cols,rows,2,b3);
    
          return img3;
       }
    }
    // end of ImageShowPanel class
    ....I just need to know where i Would put something like

    Code:
    //event handler for gettin co-od's
       private class EventHandler implements MouseListener, MouseMotionListener {
     public void mousePressed(MouseEvent e) {
             int newX = e.getX();
             int newY = e.getY();
          }
    }
    i'm not even sure if thats right, i'm just really looking for some help Thanks for listening.
    --thanks for the help.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java get Mouse coordinates

    I hope this is what you are looking for
    Code:
        this.addMouseListener( new MouseListener()
        {
          public void mouseClicked ( MouseEvent e )
          {
          }
    
          public void mousePressed ( MouseEvent e )
          {
    	int newX = e.getX() ;
    	int newY = e.getY() ;
          }
    
          public void mouseReleased ( MouseEvent e )
          {
          }
    
          public void mouseEntered ( MouseEvent e )
          {
          }
    
          public void mouseExited ( MouseEvent e )
          {
          }
    
        }
        ) ;
        this.addMouseMotionListener( new MouseMotionListener()
        {
          public void mouseDragged ( MouseEvent e )
          {
          }
    
          public void mouseMoved ( MouseEvent e )
          {
          }
    
        }
        ) ;
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2004
    Location
    youngstown, oh
    Posts
    202

    Re: Java get Mouse coordinates

    ok thanks for the help...now i put that at the end of my ImagePanel class right ?
    ...and i would need some where to display the coordinates....like
    VB Code:
    1. System.out.println("x=   " + newX);
    2. System.out.println("y=   " + newY);
    .....to be able to print them out to the system for now.

    p.s...how'd u get colour in your code ?
    --thanks for the help.

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java get Mouse coordinates

    I'm using FireFox with the VBF extension, provided by NoteMe
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

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



Click Here to Expand Forum to Full Width