Problem to store user input using mouse event

Hi,

I have a problem with my mouse event. I have succesfully code my program so that it can accept user input via mouse click event i.e. the user click on any points in my input image and the RGB values of the corresponding points is selected. The next in the sequence is to store the RGB values in an arraylist (for example) so that I can use these values for the subsequent task. My problem is that the RGB values are not captured in the arraylist. What my code do is just let the user click on the point and that's all... Can anyone point out my mistakes and let me have some ideas to solve the problem? TQ.

public void pickRandomCentroid(){
...
      clusterLimit = new Pixel[cluster];
       centroidList = new HashSet();
       cluster = Integer.parseInt( (String) clusterSize.getSelectedItem());
       textOutputPanel.println("Number of Cluster: " + cluster);
       listener = new MouseAdapter(){
         public void mousePressed(MouseEvent evt){
             mouseDown(evt);
           }};
       imagePanel.addMouseListener(listener);
     }
     catch (Exception e) {
         e.printStackTrace();
     }

public void mouseDown(MouseEvent evt){
   mouse_x = evt.getX();
   mouse_y = evt.getY();
   if (mouse_x >= 0 && mouse_x < w) {
     if (mouse_y >= 0 && mouse_y < h) {
       int rgb = bi.getRGB(mouse_x, mouse_y);
       RGB(rgb);
       textOutputPanel.println("(" + R_val + "," + G_val + "," + B_val + ")");
       Pixel pixel = new Pixel(R_val, G_val, B_val);
       centroidList.add(pixel);
       if (centroidList.size() == cluster)
         imagePanel.removeMouseListener(listener);
       }
     }  
  }
--------------------------------------------------------------
...
      Image image = imagePanel.getImage();
      findHeightWidth(image);
      bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
      Graphics2D graphics = bi.createGraphics();
      graphics.drawImage(image, 0, 0, this);
      this.pickRandomCentroid();
      Clustering cluster = new Clustering(textOutputPanel);
      cluster.setCentroid(centroidList); ----------------> to store the rgb in an arraylist
      cluster.procCluster(image, w, h, intImg, cluster); ------------------> to do next task using the content in the arraylist
...



hzinoxAsked:
Who is Participating?
 
objectsCommented:
The next steps in the sequence need to be triggered by an event, something like adding the following in your listener:

       if (centroidList.size() == cluster)
         imagePanel.removeMouseListener(listener);
         cluster.setCentroid(centroidList); ----------------> to store the rgb in an arraylist
        cluster.procCluster(image, w, h, intImg, cluster); ------------------> to do next task using the content in the arraylist
      }
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can create an array-list using:

ArrayList al = new ArrayList () ;

Then store in it using: al.add ( pixel ) ; // if that is the value you want to store

You can get elements from an array-list using:

for ( int i = 0, count = al.size () ; i < count ; i ++ )
  Pixel p = ( Pixel ) al.get ( i ) ;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.