Check if a point lies inside a rectangle

Hi im making an application that displays a shape on the screen at different locations. The shape is a combination of an ellipse and a line. My problem is when i want to draw the shapes i created a method to check if the point given is contained by any other shape and if it is it should create a new point. However when the .contains(Point) method is run it doesnt reply true or false so my code just goes into an infinite loop and crashes.
Here is the checkpoint method that loops infintly
private void setPoint(Point pow) {
          boolean anythingthere = false;
          while (anythingthere==!true) {
               Rectangle test = list.getBounds();
               if (test.contains(pow)) {
                    int f = pow.x;
                    f = f+200;
                    int g = pow.y;
                    pow.setLocation(f, g);
                    System.out.println(pow);
                    f = 0;
               else {
                    if (pow.x+150 > getWidth()) {
                         pow.setLocation(50, pow.y+180);
                    anythingthere = true;
                    i++;
     }And because my shape is a combination of graphics2D shapes i created this method in its class to return the rectangle that bounds the shapepublic Rectangle getBounds() {
     bound = new Rectangle(location.x,location.y,size,size);
     return bound;
}Thanks for any help you can give im really stumped!!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Hi thanks for your reply's. Thanks for tips on making my code nicer and efficient ive tried to do it throughout my project so hopefully its more readable now. The problem was to do with scope like one of you said so i moved a few things round and got it working. The problem was when i called the bounds of the rectangle it was returning the bounds using the last changed point which meant that every time i tested the loop the point was inside that bound so it basically looped infinitely. One line moved and it worked perfectly.
Thanks everyone

Similar Messages

  • How to find a point lies inside circle

    Hi,
    How do I find a point (x,y) is lies inside a circle? I trying to do a small shooting game.
    If the user clicks inside the circle they get a point. I am using the Pythagoras theorem to achieve this. But I can't.
    Please advice me.
    Here is my code
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Shoot extends JFrame implements MouseListener, MouseMotionListener
        private int winWidth = 300;
        private int winHeight = 300;
        private MyCanvas canvas = null;
        private int ballX = 50;
        private int ballY = 50;
        private int ballW = 50;
        private int ballH = 50;
        private int ballR = ballW / 2;
        private int curX = 0;
        private int curY = 0;
        private int left = 0;
        private int top = 0;
        public Shoot()
            Cursor c = new Cursor(Cursor.CROSSHAIR_CURSOR);
            this.setCursor(c);
            canvas = new MyCanvas();
            packIt();
            left = getInsets().left;
            top = getInsets().top;
        private void packIt()
            setTitle("My first game!");
            setPreferredSize(new Dimension(winWidth, winHeight));
            setSize(new Dimension(winWidth, winHeight));
            setContentPane(canvas);
            addMouseListener(this);
            addMouseMotionListener(this);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //setUndecorated(true);
            setLocation(300, 0);
            setVisible(true);
        public void mouseClicked(MouseEvent e)
            int x = e.getX() - left - ballX;
            int y = e.getY() - top - ballY;
            int r = ballR;
            System.out.println("x :" + x + "\ty :" + y);
            //Trying to implememt Pythagoras theorem. But I am missing something.
            System.out.println(((x * x) + (y * y)) + "\t=\t" + (r * r));
            System.out.println();
        public void mousePressed(MouseEvent e)
        public void mouseReleased(MouseEvent e)
        public void mouseEntered(MouseEvent e)
        public void mouseExited(MouseEvent e)
        public void mouseDragged(MouseEvent e)
        public void mouseMoved(MouseEvent e)
            curX = e.getX() - left;
            curY = e.getY() - top;
            canvas.repaint();
        private class MyCanvas extends JPanel
            public MyCanvas()
                setBackground(Color.BLACK);
                setBorder(BorderFactory.createLineBorder(Color.yellow));
            public void paintComponent(Graphics g)
                super.paintComponent(g);
                g.setColor(Color.WHITE);
                g.drawString("x :" + curX, 10, 20);
                g.drawString("y :" + curY, 10, 30);
                g.setColor(Color.RED);
                g.fillOval(ballX, ballY, ballW, ballH);
        public static void main(String[] asd)
            new Shoot();
    }

    public boolean isPointInCircle(){
        double x= circle.getWidth/2.0+circle.getX();
        double y= circle.getHeight/2.0+circle.getY();
        double distance=getDistance(x,y,pointX,PointY);
        return (distance<= circle.getWidth/2.0); //if the distance from the point to the center of the circle is less than the radius of the circle return true
    double getDistance(double x1, double y1, double x2, double y2){
        return math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));
    }Edited by: ghostbust555 on Jan 23, 2011 3:20 PM

  • How to access a placed PDF inside a rectangle and move it around

    Hi, I have the following code which places a PDF inside a rectangle var f = new File("C:/pdf.pdf");    var doc = app.activeDocument;  var thepdf =doc.pages[0].rectangles[0].place(f, false);  doc.pages[0].rectangles[0].fit(FitOptions.FILL_PROPORTIONALLY);  Now I want to move "thepdf" (which is of object type PDF) but I can only find a way to move the rectangle around it. I need to move the inner rectangle (the PDF), like I would do with the Direct selection tool. is this possible?

    Yes. That's possible.
    The PDF placed inside the rectangle is the graphics[0] object of the rectangle.
    Or, if you look it up in the links collection, the parent of that link. The parent of that parent is the container object, the rectangle.
    myRectangle.graphics[0].move(/*insert your arguments here*/)
    Would move the PDF inside. Where the move() method could have different arguments:
    Adobe InDesign CS6 (8.0) Object Model JS: Graphic
    Uwe

  • Check CPU & Change Pointer Utilization

    Hi All,
    Can anyone let  me know in details whats the below functional module is used for :>
    Check CPU & Change Pointer Utilization
    CALL FUNCTION 'Y_W_CHECK_CPU_CHNGPTRS'
       EXPORTING
        REFMESTYP              =
         syrepid                = l_syrepid
         input_file_count       = total_recs1
       TABLES
         ttbdme                 = l_tbdme
       EXCEPTIONS
         error1                 = 1
         error2                 = 2
         error3                 = 3
         error4                 = 4
         OTHERS                 = 5.
    thanks
    sandy

    hi,
    I think that is a specific function, therefore it's  impossible to know what used for.
    Could you  past the code of function for a best  answer ?
    regards,
    karim

  • Hello, I am on an iMac. I recently changed my password using the start up disc, now I have a thick lined rectangle, transparent, following my wireless track pad pointer all arount my moniter. The pointer is inside of it?

    Hi, I think I covered everything in my subject?
    Thanks,
    Blueheelercd

    Open Universal Access preferences to turn it off. Should be part of Voice Over.

  • Check current report user security inside CLR code

    Hi,
    I am writing a CLR stored procedure which will be invoked via a SSRS report.This SP will call a web service to fetch secure data. As this data is critical, we are implementing many security measures, one of the measure is to check inside CLR assembly,whether
    current report user is a part of an AD group (only this group will have access to read secure data) or not before I call web service .
    I am not sure if there is a way to check individual users inside CLR code . Any suggestion /input is appreciated.
    Thanks!
    Gaur

    Hi Gaur,
    Since this issue is related to Common Language Runtime (CLR), I will move this thread to .NET Framework forum. Sometime delay might be expected from the job transferring. Your patience is greatly appreciated. 
    Thank you for your understanding and support.
    Regards,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • ISDN: How to check whether its point-to-point or multipoint

    Hi, Is there any show/debug command to see whether the isdn link is point-to-point or multipoint?

    Hi,
    we have ISDN2e. I want to check whether this ISDN is p-to-p or p-to-mp. From the configuration, i have not configured it manullay as a point to point or point to multipoint.
    On this ISDN we have two ISDN numbers. How can i check it? below is the show isdn status command.
    show isdn status
    Global ISDN Switchtype = basic-net3
    ISDN BRI0 interface
    dsl 0, interface ISDN Switchtype = basic-net3
    Layer 1 Status:
    ACTIVE
    Layer 2 Status:
    TEI = 115, Ces = 1, SAPI = 0, State = MULTIPLE_FRAME_ESTABLISHED
    Layer 3 Status:
    0 Active Layer 3 Call(s)
    Active dsl 0 CCBs = 0
    The Free Channel Mask: 0x80000003
    Total Allocated ISDN CCBs = 0

  • MFC: Draw rectangle inside a rectangle

    Hi,
    I have drawn a rectangle:
    dc.Rectangle(10,10,200,100);
    Now i want to draw rectangle inside it by reducing 10 from each side. i.e the new rectangle will be: CRect(20,20,190,90).
    I am manually adjusting the co-ordinate, Is there any API to do this.
    offsetRect() is not solving my issue.
    Thanks

    Hi,
    I have drawn a rectangle:
    dc.Rectangle(10,10,200,100);
    Now i want to draw rectangle inside it by reducing 10 from each side. i.e the new rectangle will be: CRect(20,20,190,90).
    I am manually adjusting the co-ordinate, Is there any API to do this.
    offsetRect() is not solving my issue.
    Thanks
    Just write yourself a function and use it as needed. Programming is not manual work; the computer does the heavy lifting.
    Edit: Actually, I think CRect::DeflateRect() does what you want.
    David Wilkinson | Visual C++ MVP

  • Check whether there is nodes inside a xml node

    I am trying a few different way, but i can't get the point,
    hope some expert can help me on this.
    eg:
    <product>
    <item>
    </item>
    </product>
    <cfscript>
    <!--- after xml parse to xmlElement --->
    if (xmlElement.product.item neq ""){
    writeOutput(xmlElement.product.item[1]);
    But this wont work for me, anyone know how to check whether a
    xml node contains child nodes or not?

    xmlSearch()?
    Adam

  • How to check collection.contains(..) inside a tag ?

    Hi,
    I'm using both struts and jtsl tags in my jsp. JSP session has a Collection named "responses" that contains string objects.
        <c:forEach items="${page.questions}" var="question" varStatus="i">
              <tr>
                   <th align="left"><c:out value="${question.questionId}. ${question.text}"></c:out></th>
              </tr>
            <c:forEach items="${question.answers}" var="answer" varStatus="j">
              <tr>
               <td>     
                         <c:set var="aKey" value="${question.OID}_${answer.OID}"></c:set>
                      <c:if test="${question.type == 1}">
                         <c:out value="${answer.answerId}"/>
                         <input id="<c:out value="KEY_${question.OID}"/>" name="<c:out value="KEY_${question.OID}"/>" type="radio" <c:if test="??? session.getAttribute("responses").contains(${question.OID}_${answer.OID}) ???" ><c:out value="selected=true"/></c:if> value="<c:out value="${question.OID}_${answer.OID}"/>"><c:out value="${answer.text}"/>                 
                      </c:if>
                      <c:if test="${question.type == 2}">
                         <c:out value="${answer.answerId}"/>
                         <input id="<c:out value="KEY_${answer.OID}"/>" name="<c:out value="KEY_${answer.OID}"/>" type="checkbox"  value="<c:out value="${question.OID}_${answer.OID}"/>"><c:out value="${answer.text}"/>                 
                      </c:if>                  
                 </td>
            </tr>                  
            </c:forEach>
         </c:forEach>Please see the following line in the code
    <c:if test="??? session.getAttribute("responses").contains(${question.OID}_${answer.OID}) ???" ><c:out value="selected=true"/></c:if>I need to check if a collection contains a string value or not. If contains I'm selecting the radio button otherwise not.
    Any help will be appreciated!
    Thanks

    There is no way to tell if a collection contains a value.
    If you changed your datastructure to a Map, you can check for the existence of a key in a map, but thats about it.
    Alternatively write a static function for this operation, and import it into the jsp as an EL function.
    Cheers,
    evnafets

  • "1 item" and pointed finger inside message box. Is there a fix for this problem?

    When I'm in e-mail, I get a hand with the pointed finger and a message box saying "1 item". The hand and box follow my pointer around the screen but I can't open up any e-mails. The only way out of it is to go up to Hotmail on top, select it and them select "In-Box." This only occurs in Firefox. Internet Explorer works fine and I never have the problem.

    The Reset Firefox feature can fix many issues by restoring Firefox to its factory default state while saving your essential information.
    Note: ''This will cause you to lose any Extensions, Open websites, and some Preferences.''
    To Reset Firefox do the following:
    #Go to Firefox > Help > Troubleshooting Information.
    #Click the "Reset Firefox" button.
    #Firefox will close and reset. After Firefox is done, it will show a window with the information that is imported. Click Finish.
    #Firefox will open with all factory defaults applied.
    Further information can be found in the [[Reset Firefox – easily fix most problems]] article.
    Did this fix your problems? Please report back to us!

  • All of the menus on my macbook pro how a question mark inside a rectangle.  It looks like unsupported characters.  How do I change it back to regular text?

    On my early-2011 MBP running Mavericks, all of the text of menus has turned into a question mark surrounded by a rectangle.  As a result, I cannot read any menus.  What do I do to change the character set the OS is using back to a normal charset?

    Startup in Safe Mode. http://support.apple.com/kb/PH14204
    If this does not help, reinstall OS X.
    Reinstall OS X  10.9 Mavericks
    http://support.apple.com/kb/PH13871

  • Aligning text horizontally inside a rectangle shape?

    I have text on top of a rectangle made with a shape layer. I now want to align the text in the center of the rectangle shape. I select both layers, though the option to align the text sometimes shows and other times does not. When it does show the horizontal option is greyed out. How is this done?
    Thanks.

    With the Move Tool selected and no Selection active it should be no problem, could you please post a screenshot with the pertinent Panels visible?

  • How to create the Check box and Text Area inside the Assignment block

    Dear all,
            I am new to web UI development. my requirement is to create the Check box and Text box in the Assignment block. Can anyone help me to do this requirement.
    Thanks & Regards,
    Ashok.

    Hi,
      Please place the below code in the GET_P methods of the fields.
      For Text Area 
    CASE iv_property.
        WHEN if_bsp_wd_model_setter_getter=>fp_fieldtype.
          rv_value = cl_bsp_dlc_view_descriptor=>field_type_textarea.
      ENDCASE.
    For Check Box 
    CASE iv_property.
        WHEN if_bsp_wd_model_setter_getter=>fp_fieldtype.
          rv_value = cl_bsp_dlc_view_descriptor=>field_type_checkbox.
      ENDCASE.
    Regards,
    Lakshmi.Y

  • Checking Unchecking of JCheckBox value  inside JTable cell

    My problem is that i have jtable which has table data. In that first column has jcheckbox values which are there because of the tabledata has firstcolumn values as boolean, to know the no of checks in jcheckboxes in jtable, there is count variable which needs to be dynamic. When i first check the box the variable that i have set to know the check i.e count increases by one, then next jcheckbox if i check then count goes to 2, but real problem is when i do some 2 events on same checkbox then ie. if i check the jcheckbox , the count increases but after that the jcheckbox doesnot listen to events fired. Any help is appreciated

    The example from above shows how to add Objects to a table. If you want a column of checkBoxes then simply replace the Objects in one of the columns with:
    new Boolean(false)
    Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]How to Use Tables for more information.

Maybe you are looking for