简体   繁体   中英

How to render text on right side of screen in Java

I'm currently making a Minecraft mod, and I need to render text so that it aligns with the right side of the screen. I know the way to do this in CSS is by setting the right property to 0, or however many pixels you need the text off the right side of the screen.

However, I can't seem to find anything relating to this in Java. Minecraft comes with some default functions you can use to render text. I am using mc.fontRendererObj.drawString(<text>, <x>, <y>, <color>) to draw text. You can also use the variable GlStateManager to modify the text using it's variables and functions.

I'm fairly certain that with these I can achieve what I'm trying to do, but I can't find a way to do it. Does anyone know how I can do this?

Using the drawString method, you need to calculate <x> and <y> such that the end of the text aligns with the right side of the area in which you are drawing the text. So you need to obtain the width of that area as well as the width of the text you are writing. Then you can calculate <x>. Of-course the <y> does not affect right-alignment. In pure java there are methods for obtaining the width and for the length of the text, both in pixels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM