Results 1 to 4 of 4
Thread: location of click
- 11-19-2009, 03:12 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
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); } }
- 11-19-2009, 05:31 PM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
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().
- 11-22-2009, 09:23 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
Thanks for this site very helpful.
- 11-22-2009, 12:06 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
how to set the location of a panel?
By jboy in forum New To JavaReplies: 5Last Post: 10-27-2009, 07:24 PM -
location provider returns no location
By sandeeprao.techno in forum CLDC and MIDPReplies: 0Last Post: 09-24-2009, 09:54 AM -
File Location
By bcbird in forum New To JavaReplies: 1Last Post: 09-17-2009, 08:03 AM -
i click on it,then first time error page comes,second time click then product page co
By 82rathi.angara in forum New To JavaReplies: 21Last Post: 08-01-2008, 11:13 AM -
how to get the location of some button
By mary in forum Java 2DReplies: 2Last Post: 08-05-2007, 04:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks