Results 1 to 5 of 5
  1. #1
    IcedWings is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Talking mouseClicked and arrays

    Hey guys!

    I have coded an applet that will show something like this (mines a different color):

    mouseClicked and arrays-imndah.jpg

    The code below does WORK. The mouseListener part doesn't.

    Java Code:
    public class ColoredSquares extends JApplet implements MouseListener
    {
    	
    	Random rand = new Random();
    	private MouseEvent evt = null;
    	private boolean isClicked;
    
    	// declare instance variables
    	// initialize instance variable with an array of Color objects
    	Color [][] a;
    	Color randC;
    	int x, y;
    	int myClickX;
    	int myClickY;
    
    
        // generates a color
        public Color Color(Color randC2) 
        {
       	// omitted
    	}
    	
        public void init ()
        {
        	// sets window size of the applet viewer
        	 setSize(800,800);
        	
            // fill entire applet with background color
            setBackground(Color.BLACK);
            
    		// fill array with 8x8 color objects 
    		a = new Color[8][8]; 
            
    		for (int x = 0; x < a.length; x++)
    			for (int y = 0; y < a.length; y++)
    				a[x][y] = Color(randC);
        	             
            // implement MouseListener interface
    		this.addMouseListener(this);        <--- I think this line is WRONG
        } // end init method
        
    
        public void paint(Graphics g)
        {	   	
       
        	// rectangle size is determined by applet size
        	// leave space at the bottom to draw a string
        	int rowHeight = (getHeight() / 8) - 5;
        	int colWidth = (getWidth() / 8);
    
            // loop through each array object and paint a rectangle
            for (int row = 0; row < a.length; row++)
    	     {
    		    for (int col = 0; col < a.length; col++)
    		    {
    
    		    	g.setColor(a[row][col]);
    		        g.fillRect(col*colWidth, row*rowHeight, colWidth, rowHeight );	
    		       
    		        
    		    }
    	     }
    
    
    	   	// if mouseClicked is true, then change the color of the square that was clicked on 
    	   	if (evt != null && isClicked == true)
    	   	{
    
    	    	// if a square is clicked on
    	        // the square turns red
    	   		 g.setColor(Color.red);
    	         int col = 0;
    			 int row = 0;
    			 g.fillRect(col*colWidth, row*rowHeight, colWidth, rowHeight );
    	   	}
        	
        } // end paint method
     
    
       
    	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();
    		 		
    
                     NOT sure what to do HERE V V below V V
    
    
    		 // if an array element is clicked
    		 //		return isClicked = true
    		 // else 
    		 //		return isClicked = false
    
    
    	      
    		repaint();
    	} // end mouseClicked method
    	
    	public void mouseExited(MouseEvent evt)	{}
    	public void mouseEntered(MouseEvent evt) {}
    	public void mouseReleased(MouseEvent evt) {}
    	public void mousePressed(MouseEvent evt) {}
    	
    	
    } // end class
    What I want to happen is when I click on one of the squares in the applet, I want to change the color of the array element corresponding to that square to RED.

    I don't believe any other method is to be added-- use the methods already stated.

    How do I do that? @_@ I'm really confused with mouse listeners. I know I need to do something like a[row][col].addmouseListener???
    Any help would be much appreciated.
    Last edited by IcedWings; 03-08-2013 at 05:00 AM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: mouseClicked and arrays

    Custom painting should NOT be done on the applet itself.

    Create a JPanel and overrid the paintComponent(...) method with your painting logic. Then you add the panel to the applet.

    Then you add the MouseListener to the panel.

    In your mouseClicked logic you then need to determine which sqare was clicked. Basically the row is given by taking the mouse clicked Y position and dividing this number by the height of each square. Do the same for the column. Then you can update your Color array with the appropriate color and repaint the panel.

    // leave space at the bottom to draw a string
    Don't do this. Instead you Create a JLabel and add it your your applet, probably at BorderLayout.SOUTH.

  3. #3
    IcedWings is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Exclamation Re: mouseClicked and arrays

    Quote Originally Posted by camickr View Post
    Custom painting should NOT be done on the applet itself.

    Create a JPanel and overrid the paintComponent(...) method with your painting logic. Then you add the panel to the applet.

    Then you add the MouseListener to the panel.

    In your mouseClicked logic you then need to determine which sqare was clicked. Basically the row is given by taking the mouse clicked Y position and dividing this number by the height of each square. Do the same for the column. Then you can update your Color array with the appropriate color and repaint the panel.



    Don't do this. Instead you Create a JLabel and add it your your applet, probably at BorderLayout.SOUTH.
    Unfortunately, I haven't learned about JPanels in my course yet. >_< Thus, those shouldn't be used in my applet. I'm not looking to change what already works. I'd like some help in figuring out what needs to be written in the mouseClicked method.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,192
    Rep Power
    19

    Default Re: mouseClicked and arrays

    Quote Originally Posted by IcedWings View Post
    I'm not looking to change what already works.
    Good luck with that, then.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: mouseClicked and arrays

    Did you try and put print statements in your mouseClicked routine to see if was picking up events?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Menu Events actionPerformed vs mouseClicked
    By alfredlok in forum New To Java
    Replies: 2
    Last Post: 02-18-2012, 05:46 PM
  2. Replies: 4
    Last Post: 01-01-2012, 09:55 PM
  3. Java MouseClicked problem.
    By Love Like Rockets. in forum AWT / Swing
    Replies: 12
    Last Post: 10-30-2011, 01:58 PM
  4. MouseClicked Stop Animation
    By xpngamer in forum New To Java
    Replies: 1
    Last Post: 04-14-2009, 09:38 AM

Tags for this Thread

Posting Permissions

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