Results 1 to 4 of 4
  1. #1
    saima is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default location of click

    hi evryone.. am new here so please b nice :)

    i am trying to create a java program which will randomly display a 2d blob or arrays with random colour( only 3 colours)
    a 10x10 square of blobs is made... when i click on a blob.. i want it to cycle through to the next colour, but i dont know how to work out which blob was clicked from the X and Y coordinates :confused: how do i do it?

    here is the code

    thank you

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.*;
    
    
    public class BlobSkeleton extends JPanel implements MouseListener
    {
    	JPanel canvas;		// Panel for drawing on
        	int x,y;  		// Mouse click location
    	int Blobx, Bloby;
    	int col;
    	int noofblobs = 10;
    	// Location in Blob array
    
        	Blob [][] BlobArray = new Blob[noofblobs][noofblobs];
    
        	public BlobSkeleton() 
    	{
            	super(true); // Call constructor of parent
      
    		// Standard layout (flow)
            	setLayout(new FlowLayout());
    
    		// Set up panel - canvas
    		canvas = new JPanel(true);
            	canvas.setPreferredSize(new Dimension(200, 200));
    		canvas.setBorder(BorderFactory.createLineBorder(Color.blue));
    
            	add(canvas);
    		canvas.addMouseListener(this);
    
    		for (int Blobx=0; Blobx<noofblobs; Blobx++)
    		{
    			for (int Bloby=0; Bloby<noofblobs; Bloby++)
    			{
    				col= (int)(Math.random()*3);
    				int X = Blobx*20;
    				int Y = Bloby*20;
    		BlobArray[Blobx][Bloby] = new Blob(col,X,Y);	
    			}
    		}
    		// YOU ADD:
    		// CREATE AND INITIALISE 2D ARRAY OF BLOBS
        	}
    
    	public void Draw ()
    	{
    		Graphics g = canvas.getGraphics();
    
    		for (int Blobx=0; Blobx<10; Blobx++)
    		{
    			for (int Bloby=0; Bloby<10; Bloby++)
    			{
    		BlobArray[Blobx][Bloby].Draw(g);
    		                 }}
    		
    		
    		
    		// YOU ADD:
    		// DRAW ARRAY WE INITIALISED EARLIER
    	}
        
    	// Empty definitions to fulfil requirements of interface
    	public void mouseEntered(MouseEvent e) 
    	{
    		// DRAW ARRAY AS SOON AS POINTER ENTERS WINDOW
    		Draw();
        	}
        	public void mouseExited(MouseEvent e) 
    	{
        	}
        	public void mousePressed(MouseEvent e)
    	{
        	}
        	public void mouseReleased(MouseEvent e) 
    	{
        	}
    
        	public void mouseClicked(MouseEvent e)
        	{
    		// THIS IS WHERE THE MAIN ACTION HAPPENS
    
    		// Get graphics object for canvas
    	        Graphics g = canvas.getGraphics();
    
    		// Get the location of the click
    		x=e.getX();
    		y=e.getY();
    		
    		
    		
    		
    		
    		
    		// YOU ADD:
    		// 1. WORK OUT WHICH BLOB WAS CLICKED, FROM X,Y
    		// 2. CYCLE THAT BLOB
    		// 3. REDRAW THE ARRAY
        	}
    
        	public static void main(String[] args) 
    	{
    		// YOU WILL NOT NEED TO EDIT THIS METHOD
    
            	// Create a Blob entity
            	BlobSkeleton b = new BlobSkeleton();
            
    		// Set up outer frame, and its exit behaviour
            	JFrame frame = new JFrame("Blob Skeleton App");
            	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    		// Set the main content frame to be the BlobSkeleton,
    		// size the frame (pack) and make it visible
            	frame.setContentPane(b);
            	frame.pack();
            	frame.setVisible(true);
        	}
    }

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Please review Lesson: Performing Custom Painting

    When the code draws a Blob, it puts it at an x,y location
    with a given width and height.
    One (poor) solution to the query is to visit every Blob
    and ask if the x,y coordinates of the hit are within the Blob's screen rectangle.
    It is faster, and less code, to find the Blob's
    column from (x-xForFirstBlob)/blobWidth and similarly for the row.

    Some notes so that the code looks and behaves like Java code:
    • Change name of BlobArray to blobArray. It is not a class.
    • Implement repaintComponent(Graphics g) instead of Draw().
    • Call repaint() instead of Draw().
    • Remove call on Draw() from mouseEntered.
    • Put right curly braces at the end of preceding line.
    • Align each left curly braces with the start of its partner's line.
    • Eliminate calls on canvas.getGraphics().

  3. #3
    0053wr is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default

    Thanks for this site very helpful.

  4. #4
    saima is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by 0053wr View Post
    Thanks for this site very helpful.
    itis if you know how to code it.. which i dont lol

Similar Threads

  1. how to set the location of a panel?
    By jboy in forum New To Java
    Replies: 5
    Last Post: 10-27-2009, 07:24 PM
  2. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 09:54 AM
  3. File Location
    By bcbird in forum New To Java
    Replies: 1
    Last Post: 09-17-2009, 08:03 AM
  4. Replies: 21
    Last Post: 08-01-2008, 11:13 AM
  5. how to get the location of some button
    By mary in forum Java 2D
    Replies: 2
    Last Post: 08-05-2007, 04:02 AM

Posting Permissions

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