Results 1 to 2 of 2
  1. #1
    IcedWings is offline Member
    Join Date
    Mar 2013
    Rep Power

    Default Looping through an Array

    I have this applet that will paint an array of color objects in the form of squares.
    Java Code:
    Color [][] a;
    Part if my void init()
    Java Code:
    a = new Color[8][8]; 
           // implement MouseListener interface
    I want to loop through this array of color objects and find if the mouseClick is in the region of the array.

    In my mouseClicked, I have so far:
    Java Code:
    	public void mouseClicked(MouseEvent evt)
    		// save the evt pointer in my global variable
    		 this.evt = evt;
    		// stores the current click of the mouse, X and Y position
    		myClickX = evt.getX();
    		myClickY = evt.getY();
    		// if the coordinates of where the mouse was click is in the region of the array
     		for (int row = 0; row < a.length; row++)
    			for (int col = 0; col < a.length; col++)
    	                      // return isClicked = true
    		              // else 
    		             // return isClicked = false
    I've tried something like a[row][col].click(myClickX, myClickY), but the click part isn't working. Eclipse is saying "The method click(int, int) is undefined for the type Color". Not sure what to do.

    Can you give me any simple examples? Any help would be appreciated

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: Looping through an Array

    Your a array is an array of Color objects, and for a Color object a click is meaningless. What does it mean for instance to call, myClckY)? It doesn't make either logical sense or code sense. Instead you need to associate clicks with visualized *components* that are clicked, that have the MouseListener attached to them.

    If this were my application, I'd simplify things quite a bit, would be sure to use the Swing library, meaning using a JApplet, have my applet hold a JPanel that used a GridLayout and that held an array of JLabels. I'd make my JLabels opaque so that they showed their background Color, and would add a MouseListener to the JLabels in a for loop.

    Edit: you've already been told about using a JPanel and have been given good suggestions in your previous post that you didn't mention in this one. I suggest that you either follow my advice or camicker as either would solve this problem. I also suggest that you let us know if you already have an active similar question when asking questions so we can see in advance what has been discussed before.
    Last edited by Fubarable; 03-11-2013 at 01:51 AM.

Similar Threads

  1. Need Help looping through an Array
    By grimlek in forum New To Java
    Replies: 20
    Last Post: 12-19-2012, 06:25 AM
  2. Looping through an array
    By Person264 in forum New To Java
    Replies: 0
    Last Post: 12-12-2012, 10:59 PM
  3. Looping over averages in an int[][] array
    By adventurerOK in forum Advanced Java
    Replies: 1
    Last Post: 09-28-2011, 09:16 PM
  4. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  5. Looping Array Elements
    By enzyme in forum New To Java
    Replies: 3
    Last Post: 11-26-2009, 04:35 PM

Posting Permissions

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