I was creating a showMessageDialog, just like component from JOptionPane.showMessageDialog
and I tried to use UnicodeFont to compute total width of the string, so that I can check if the String line goes above max width of the panel. However, my frames have dropped from 2000 fps on to 30. I thought the reason could be UnicodeFont, I commented try/catch block where I create font, and frames are back to normal. So what do you think could be the problem, font or list of the methods I call to make an effects to a font instance?
public static void showMessageDialog(GameContainer container, Graphics g, String title, String text) throws SlickException {
UnicodeFont textFont;
try {
textFont = new UnicodeFont("res/font/CharlemagneStd-Bold.ttf", 14, false, false);
textFont.addAsciiGlyphs();
textFont.getEffects().add(new ColorEffect());
textFont.loadGlyphs();
} catch (SlickException ex) {
System.err.println("Font error...");
return;
}
g.setFont(textFont);
// title, drawLine...
// render text
float textX = panelMinX + panelInPaddingLeft + strFixHorizontalPadding;
float textY = Math.max(lineY1, lineY2) + panelInPaddingTop;
String[] sentence = text.split(" ");
ArrayList<String> strLineList = new ArrayList<>();
String strLine = "";
float currentTextWidth;
float maxLineWidth = (panelMaxX - panelMinX) - (panelInPaddingLeft + panelInPaddingRight + strFixHorizontalPadding);
for (int word = 0; word < sentence.length; word++) {
currentTextWidth = textFont.getWidth(strLine) + textFont.getWidth(sentence[word]);
if (currentTextWidth > maxLineWidth) {
strLine = strLine + "\n";
strLineList.add(strLine);
strLine = "";
word--;
} else {
strLine = strLine + sentence[word] + " ";
}
if (word == sentence.length - 1) {
strLineList.add(strLine);
strLine = "";
}
}
for (String line : strLineList) {
strLine = strLine + line;
}
g.setColor(Color.white);
g.drawString(strLine, textX, textY);
}
p.s. If there’s solution for getting string width/height, or some other way of coding font to instantiation constructor with calling width/height method and ofc avoiding this frames drop down …