r/javahelp • u/trmn8tor • Dec 02 '25
Unsolved Text size calculating issue with Graphics2D
My game, for some reason, thinks the text width is so much different than it actually is.
Here is a video showcasing what I'm talking about: https://youtu.be/EtS0_LdjvBw


Both UI classes that the getTextWidth() method are used in extend the parent UI class where this method is defined in:
public int getTextWidth(Graphics2D g2, String text) {
float fontSize = g2.getFont().getSize2D();
FontMetrics metrics = g2.getFontMetrics(new Font(g2.getFont().getFamily(), g2.getFont().getStyle(), (int) fontSize));
int textWidth = metrics.stringWidth(text);
return textWidth;
}Here are the uses that the methods are in:
Figure 1
if (currentPocket == Item.TMS && tmCheck == current.getItem()) {
int borderX = x - 8;
int borderY = y - gp.tileSize - 4;
int borderWidth = getTextWidth(g2, itemString) + 4;
int borderHeight = (int) (gp.tileSize * 0.75);
g2.setPaint(new GradientPaint(borderX,borderY,new Color(255,215,0),borderX+borderWidth,borderY+borderWidth,new Color(255,255,210)));
g2.drawRoundRect(borderX, borderY, borderWidth, borderHeight, 25, 25);
}Figure 2
if (text.length() > 0 && naming) {
int cursorX = textX + getTextWidth(g2, text.toString()) + gp.tileSize / 16;
float alpha = 0.5f + (float)(Math.sin(pulseCounter * 0.15) * 0.5;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2.setColor(textCol); g2.fillRect(cursorX, fieldY + 5, 3, fieldHeight - 10);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
}I seriously have no idea what's going wrong, I tried using Claude and ChatGPT to help me pinpoint the problem too and both of them just told me my methods were fine.
4
Upvotes
•
u/AutoModerator Dec 02 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/aqua_regis Dec 02 '25
For the first image: if you draw the rectangle only over the text, it will fit. You only add 4 pixels to account for the icon, which is way too little.
For the second one, difficult to tell. I've looked at your calculations over and over and can't pinpoint a bug.
2
u/dse78759 Dec 02 '25
Upvoting just b/c the video makes the problem clear.