Page 1 of 3 123 LastLast
Results 1 to 20 of 46
Like Tree1Likes

Thread: Is it possible to get the covered area of an object?

  1. #1
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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.
    Last edited by jiffi; 01-09-2012 at 10:26 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Is it possible to get the covered area of an object?

    Moved from 'New to Java'

    Were you looking for SwingUtilities#getDeepestComponentAt(...)?

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

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  6. #6
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Is it possible to get the covered area of an object?

    oh sorry, a sea battle

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Is it possible to get the covered area of an object?

    Quote Originally Posted by jiffi View Post
    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.
    DarrylBurke likes this.

  8. #8
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Is it possible to get the covered area of an object?

    Quote Originally Posted by jiffi View Post
    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.

  10. #10
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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.

  11. #11
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Is it possible to get the covered area of an object?

    Your MVC is really making it more complicated for me.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  13. #13
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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.

  14. #14
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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.

    Java 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");
            }
        }

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Is it possible to get the covered area of an object?

    Quote Originally Posted by jiffi View Post
    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.

  16. #16
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  18. #18
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default 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

  19. #19
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Is it possible to get the covered area of an object?

    Do you have any idea why it would not work?

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Is it possible to get the covered area of an object?

    Quote Originally Posted by jiffi View Post
    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.

Page 1 of 3 123 LastLast

Similar Threads

  1. Text Area
    By pabloma2002 in forum CLDC and MIDP
    Replies: 1
    Last Post: 02-11-2011, 04:56 PM
  2. Area Add
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:07 PM
  3. Area Subtract
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:07 PM
  4. Area Intersect
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:07 PM

Posting Permissions

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