Results 1 to 6 of 6
- 10-17-2011, 11:16 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
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!"); }
- 10-17-2011, 11:37 PM #2
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
- 10-18-2011, 12:03 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
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.
- 12-24-2011, 09:26 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Re: trouble with: FontMetrics.getStringBounds() and Rectangle.contains()
-
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
5. Create a new method to test whether the mouse intersected with a shapeJava Code:private Font f = new Font(...); public Shape getShapeFromString(String message) { //methods 2-3; ... return s; }
I think the rest is fairly simple for you.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); }
- 12-27-2011, 08:09 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
Similar Threads
-
does rectangle contain or overlap another rectangle?
By Xycose in forum New To JavaReplies: 6Last Post: 11-30-2010, 11:29 PM -
Java Rectangle
By java_beginner_ in forum New To JavaReplies: 9Last Post: 10-31-2010, 07:15 PM -
Wrong with Rectangle res = new Rectangle(0,0,0,0);???
By jiapei100 in forum AWT / SwingReplies: 3Last Post: 09-25-2010, 03:39 PM -
Rounded Rectangle
By Arnold in forum AndroidReplies: 0Last Post: 05-27-2010, 04:06 PM -
Rectangle Intersection
By Gwindow in forum Java 2DReplies: 1Last Post: 04-24-2008, 03:53 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks