This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi friends, I have a problem.I want to superimpose an image on a grid.Pleas help me how to build a grid with constant width and height and grid cells with constant size.When I superimpose an image on this grid it should be devided into cells.When I click at the intersection of cells on this image grid I should be able to catch the position that is x,y coordinates. I want to use a Jpanel in the JFrame as container for this image grid. Can anyone help me in solving this problem.
Yes, you can do this... But your explaination really needs to be more specific... Do you need to actually split the image into smaller pieces? Do you just need to draw the grid on top of the image? Do you just need to capture the coordinates? Do you need to capture something in addition to the coordinates ( like the piece of the image that is at this coordinate )? Answers to these questions would help us to know what you really need this thing to do...
Here are some pointers that may help you with what you have described so far...
Subclass Canvas... give it an Image attribute, and some attributes to control the grid, let's say gridWidth, gridHeight, gridX, and GridY... make methods to get and set the Image and the grid attributes... override the getPreferredSize() method to make it the correct size in the GUI... in the paint() method draw the Image, then use the drawLine() method in Graphics to draw the grid, using the grid attributes you have set before. Add a mouseListener to the component. Let this mouseListener test if it is on a grid intersection, if it is, call a method that does whatever you need with the current mouse coordinates. The above assumes that all you need to do is capture the coordinates. It would be much more difficult to actually cut the image into parts ( but still possible to do ), but you did not specify if you needed to do this or not... If you do I will try to help you out with that problem too...
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joined: Jun 11, 2001
Thanks for your suggestion. But I don't want to create a new image.I want to superimpose an existing image on a grid structure.If you mean it as splitting the image into cells I want the same. Nageswara rao