Problem to store user input using mouse event

Posted on 2006-05-16
Last Modified: 2010-03-31

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){
     catch (Exception e) {

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);
       textOutputPanel.println("(" + R_val + "," + G_val + "," + B_val + ")");
       Pixel pixel = new Pixel(R_val, G_val, B_val);
       if (centroidList.size() == cluster)
      Image image = imagePanel.getImage();
      bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
      Graphics2D graphics = bi.createGraphics();
      graphics.drawImage(image, 0, 0, this);
      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

Question by:hzinox
    LVL 30

    Assisted Solution

    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 ) ;
    LVL 3

    Assisted Solution

    LVL 23

    Assisted Solution

    LVL 92

    Accepted Solution

    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)
             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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now