Page 3 of 3 FirstFirst 123
Results 41 to 46 of 46
Like Tree1Likes

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

  1. #41
    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 how would that work? Through parameter passing?
    You could get information into the class with parameter passing and with listeners. Have a look at my code above and the jar file to see what I mean. The code above adds a MouseListener to the grid panel and then uses the PropertyChangeSupport that comes with Swing JComponents to allow other classes to respond to changes in state brought on by the internal MouseListener.

    I have literally no idea how to do neither of those. I'll try to look it up later (although I already tried to understand HashMap and wasn't successful ).

    It seems a little more complicated to me than using a JButton. I would add a mouse listener to every JLabel right?
    No, you could add one MouseListener to the JPanel that holds all the JLabels. Again, look at my code immediately above for an example.

  2. #42
    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?

    Wait so are you saying that the only thing that links Battleshipmainpanel and battleshipgridpanel is the property changed event?

    (Sorry for the kinda late response, it is taking me a while to get used to your way of coding)
    Last edited by jiffi; 01-16-2012 at 03:41 PM.

  3. #43
    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
    Wait so are you saying that the only thing that links Battleshipmainpanel and battleshipgridpanel is the property changed event?
    Well, the main panel currently holds two of the grid panel objects, so it has a "has-a" relationship with the two grid panels and is thus able to call any of the objects methods it sees fit. The grid panel only communicates with its containing panel right now via the property change. This all might change as this is very preliminary code.

  4. #44
    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?

    Java Code:
    this.img = img;
    Java Code:
    @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (img != null) {
             int w0 = getWidth();
             int h0 = getHeight();
             int x = w0 / 11;
             int y = h0 / 11;
     
             int iW = img.getWidth(null);
             int iH = img.getHeight(null);
     
             g.drawImage(img, x, y, w0, h0, 0, 0, iW, iH, null);
          }
       }
    could you please explain what you are doing here?

    also what is the null argument in getWidth(null) used for?

  5. #45
    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
    could you please explain what you are doing here?

    also what is the null argument in getWidth(null) used for?
    Java Code:
       protected void paintComponent(Graphics g) {
    
          // the super's method redraws the background, erasing things that need to be erased
          super.paintComponent(g);
          if (img != null) {
    
             // get the dimensions of the current JPanel
             int w0 = getWidth();
             int h0 = getHeight();
    
             // The grid will have 11 rows and 11 columns. I want to draw the image
             // starting at column 1 and row 1 (0 based, so the second row and column)
             int x = w0 / 11;
             int y = h0 / 11;
      
             // get the image dimensions. The ImageObserver parameter is ignored (see the API) 
             // and thus should be null.
             int iW = img.getWidth(null);
             int iH = img.getHeight(null);
      
             // draw the image from column 1, row 1 to the width and height of the JPanel.
             // but draw the whole image in this spot compressing or expanding if need be.
             // the Graphics API will tell you the details of this method.
             g.drawImage(img, x, y, w0, h0, 0, 0, iW, iH, null);
          }
       }

  6. #46
    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 don't quite understand what the hashmap does

Page 3 of 3 FirstFirst 123

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
  •