2
\$\begingroup\$

I've made a button class that can be drawn and clicked on. How can I write the text of that button (using the slick drawing API, g.drawString()) with a Centered alingment (so that the text is exactly centered in a rect with defined x,y,widht and height). Thanks

\$\endgroup\$
1
\$\begingroup\$

If I understand this question correctly you want to create a class that writes text on a button for you centered to a rect.

public class DrawCentered(){
    public DrawCentered(String chars, Rect r){
        TrueTypeFont font = new TrueTypeFont(new Font(...));
        int width = font.getWidth(chars);
        int height = font.getHeight(chars);

        g.drawString((r.x + r.width / 2) - (width / 2), 
                     (r.y + r.height / 2) - (height / 2), 
                     chars);
    }
}

Note Above code is untested

I hope this helps, and if you have any further questions, don't hesitate to ask.
JavaDocs such as:
Slick2D TrueTypeFont JavaDoc
JavaAwtFont JavaDoc are great tools to help you when you are stuck.

Cheers!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.