Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By spring
  • 1 Post By camickr

Thread: trouble with: FontMetrics.getStringBounds() and Rectangle.contains()

  1. #1
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default trouble with: FontMetrics.getStringBounds() and Rectangle.contains()

    Ultimately, I want to have strings moving around a space (for which I presume JPanel is best) as targets for the user to click on. A string will occupy a rectangular space and my code will need to determine whether the user clicked inside that rectangle, i.e. hit the target.

    Towards that end, I'm just trying to write some simple code to confirm that I can indeed correctly identify a point as being within a rectangle. My code draws a string ("hallo world") at (x,y)-position (50,50). Therefore, the point (51,51) should definitely (I think) lie *within* the rectangle bounding my string. But my code produces the output "No!" when it should be "Yes!".

    Please help. Thanks.

    Java Code:
           public void paintComponent(Graphics g){
                Rectangle stringBounds;
                
                super.paintComponent(g);
                g.setFont(myFont);
                myGraphics = g;
                myFontMetrics = g.getFontMetrics();
                stringToDraw = "hallo world";
                myGraphics.drawString(stringToDraw, 50, 50);
                stringBounds = myFontMetrics.
                        getStringBounds(stringToDraw, myGraphics).getBounds();
                if (stringBounds.contains(new Point(51,51))){
                    messageBox.setText("Yes!");
                }
                else  messageBox.setText("No!");
            }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: trouble with: FontMetrics.getStringBounds() and Rectangle.contains()

    Your assumptions are wrong. The Graphics reference uses the String bounds from FontMetrics to lay out the text. The FontMetrics instance has no clue that the String was translated to [50, 50] by the Graphics drawString method.

    It rather looks like you're a victim of a very common but erroneous interpretation of a Graphics object as a painting surface. Try thinking of the Graphics as a brush or pen that paints on a surface -- which is the GraphicsContext.

    And since this topic is related to Java 2D rather than Swing, I shall move it there.

    db

  3. #3
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Re: trouble with: FontMetrics.getStringBounds() and Rectangle.contains()

    Thanks. I'd be very grateful if you (or anyone) could show me some code that would achieve what I'm trying to.

  4. #4
    spring is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Re: trouble with: FontMetrics.getStringBounds() and Rectangle.contains()

    trouble with: FontMetrics.getStringBounds() and Rectangle.contains()-hh.gif

    you "hello world" shoud in that place, (50,50) is the baseline of the str, the Rectangle of "hello world" on Container is (50, 50+stringBounds.getY(),stringBounds.getWidth(),str ingBounds.getHeight()), stringBounds.getY() is negative。
    Norm likes this.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: trouble with: FontMetrics.getStringBounds() and Rectangle.contains()

    Wouldn't it be easier if you knew the text position before-hand to keep track of it and check if it intersects with a mouse click?

    1. Create your font

    Font f = new Font("Tahoma", Font.BOLD, 24);

    2. Create glyph vectors from the some text String

    GlyphVector gv = f.createGlyphVector(getFontMetrics(f).getFontRende rContext(), "Some text message");

    3. Convert glyph vectors to a shape

    Shape s = gv.getOutline();

    4. Throw section 2-3 into a method you can call back to return the shape

    Java Code:
    private Font f = new Font(...);
    
    public Shape getShapeFromString(String message) {
        //methods 2-3;
        ...
        return s;
    }
    5. Create a new method to test whether the mouse intersected with a shape

    Java Code:
    public boolean hitMessage(Shape s, int mouseX, int mouseY) {
        //allow some in-accuracy by converting the point mouseX, mouseY into a 10 by 10 Rectangle2D
        Rectangle2D r2d = new Rectangle2D.Double(mouseX-5, mouseY-5, 10, 10);
        return s.intersects(r2d);
    }
    I think the rest is fairly simple for you.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: trouble with: FontMetrics.getStringBounds() and Rectangle.contains()

    Just use a JLabel and add the MouseListener to the label. You move the label around the screen by invoking setLocation().
    DarrylBurke likes this.

Similar Threads

  1. Replies: 6
    Last Post: 11-30-2010, 11:29 PM
  2. Java Rectangle
    By java_beginner_ in forum New To Java
    Replies: 9
    Last Post: 10-31-2010, 07:15 PM
  3. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 03:39 PM
  4. Rounded Rectangle
    By Arnold in forum Android
    Replies: 0
    Last Post: 05-27-2010, 04:06 PM
  5. Rectangle Intersection
    By Gwindow in forum Java 2D
    Replies: 1
    Last Post: 04-24-2008, 03:53 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
  •