Is it possible to get the covered area of an object?
Hello,
I was wondering if it were possible to get the any point covered by an object.
I don't have any codes to show, but here is basically what I want to do:
A Frame of size 20 by 20 contains a JLabel of size 10 by 10 who's origins are 0,0.
If the user enters any coordinates between 0,0 and 10,10 , the program returns true; else it returns false.
note: in my actual program there will be more than 1 JLabel, and none of their origins will be known.
Thanks.
edit: Also in my actual program the user will "enter positions" by clicking on JButtons, and some of the JButtons will be located over the JLabels, except their positions will be randomized at each beginning.
Re: Is it possible to get the covered area of an object?
Moved from 'New to Java'
Were you looking for SwingUtilities#getDeepestComponentAt(...)?
db
Re: Is it possible to get the covered area of an object?
Perhaps there's a (much) better way to do whatever it is you're trying to do, instead of detecting the component at a point. What exactly is that?
db
Re: Is it possible to get the covered area of an object?
I'm not sure what component needs to be put into the swing utilities. Is it the JButton that was used to guess a position, or is it the JLabel i am trying to locate?
I'm trying to think of another way, might have a small idea but I still have to develop it.
Re: Is it possible to get the covered area of an object?
You still haven't described the underlying thing you're trying to accomplish.
Re: Is it possible to get the covered area of an object?
Re: Is it possible to get the covered area of an object?
Quote:
Originally Posted by
jiffi
oh sorry, a sea battle
Again you are limiting what you tell us. Do you mean a game like Battleship?
If so, then the solution is to keep your model (the game logic including position of the ships) and your view (the GUI including the JLabels) separate.
The model could be a 10 x 10 grid of some sort which would hold Ship objects. Each grid cell would have a "hit" property to tell if the opponent has shot that cell. Then you would use the data held by the model to draw appropriately to the GUI. So you see you really don't need to know whether a JLabel is present at a location or not.
Re: Is it possible to get the covered area of an object?
I still need to know whether there is a ship on the space or not though at some point right?
Re: Is it possible to get the covered area of an object?
Quote:
Originally Posted by
jiffi
I still need to know whether there is a ship on the space or not though at some point right?
Right, but that's what the model is for, not the GUI. The GUI just displays the ship, but the model holds all the data and allows you to give your GUI intelligence. Look up MVC (model-view-controller) and MVP (model-view-presenter) for more on this important distinction.
Re: Is it possible to get the covered area of an object?
I never intended to put it in the GUI, I am just looking for a way to know whether a ship (JLabel) is there or not.
Re: Is it possible to get the covered area of an object?
Your MVC is really making it more complicated for me.
Re: Is it possible to get the covered area of an object?
If still stuck, post an SSCCE, and then let's see what we can do with it to help you out.
Re: Is it possible to get the covered area of an object?
I was actually thinking that the getComponentAt() might work, or maybe the findComponentAt() depending on how I the code is done.
Re: Is it possible to get the covered area of an object?
so the getComponentAt() works only if I compare it right after the object has been created.
Code:
public void actionPerformed(ActionEvent event)
{
JButton source;
source = (JButton)event.getSource();
JLabel image = new JLabel ();
if (x == 0)
{
image.setBounds(source.getX(),source.getY(), source.getWidth() *4, source.getHeight());
System.out.println(image);
image.setIcon(new ImageIcon("size4.png"));
shipBackground[0].add(image);
shipBackground[0].moveToFront(image);
x = 1;
//this.doClick(1);
}
if (shipBackground[0].getComponentAt(source.getX(),source.getY()) == image)
{
System.out.println("found");
}
}
Re: Is it possible to get the covered area of an object?
Quote:
Originally Posted by
jiffi
so the getComponentAt() works only if I compare it right after the object has been created.
In a word: no.
There's no truth to your statement above.
Re: Is it possible to get the covered area of an object?
Well in the example I provided it is. The first time I click on the button, found is printed out. Now if I click again, it is not.
Re: Is it possible to get the covered area of an object?
Your code looks a bit of an oddball solution. Why all the gymnastics?
Re: Is it possible to get the covered area of an object?
I am just trying out things to get an idea of how I will make it work later
Re: Is it possible to get the covered area of an object?
Do you have any idea why it would not work?
Re: Is it possible to get the covered area of an object?
Quote:
Originally Posted by
jiffi
Do you have any idea why it would not work?
I'm not good at guessing at things I can't see. All I know is that the method has worked fine in my code when I've used it.