0

I am trying to center the text output from drawString on the X coordinate in a program. I am trying to get the width of my window and devide by two to get the center but to no avail. Here is my code:

package net.minecraft.src;
import java.awt.Color;
import java.awt.FontMetrics;
import org.lwjgl.input.Keyboard;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import net.minecraft.client.Minecraft;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

public class GuiIngame extends Gui
{
//lots of other code here
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(0);
String xCords = nf.format(mc.thePlayer.posX);
String yCords = nf.format(mc.thePlayer.posY);
String zCords = nf.format(mc.thePlayer.posZ);
drawString(fontrenderer, (new StringBuilder()).append("X: ").append(xCords).toString(), 20, 2, 0xe0e0e0);
drawString(fontrenderer, (new StringBuilder()).append("Y: ").append(xCords).toString(), 40, 2, 0xe0e0e0);
drawString(fontrenderer, (new StringBuilder()).append("Z: ").append(xCords).toString(), 60, 2, 0xe0e0e0);
}

It only needs to be centered on the x axis.

6
  • First example here? Commented Apr 11, 2012 at 13:04
  • 1
    Is it on purpose that you draw 3 times xCords? Any reason for not using String.format? Why do you create StringBuilder when you can use the '+' sign? What is the hierarchy of Gui? Commented Apr 11, 2012 at 13:05
  • @Guillaume Polet I drew xCords 3 times for testing my code. I also am slightly new at Java. This code was taken from other code already written. Commented Apr 11, 2012 at 13:46
  • @Richante Correct me if I am wrong but isn't that to get the length of the text, not the center of the window? Commented Apr 11, 2012 at 13:47
  • I am just throwing some improvements to your code (in terms of readability, mainly). Now, my question still remains "What is the type hierarchy of Gui?". what is the type of fontrenderer? We can't guess the context of your code, so you need to provide it in order for us to help you Commented Apr 11, 2012 at 13:48

3 Answers 3

2

There was a integer that I didn't know of that did the job. My final code to display each line is:

drawCenteredString(fontrenderer, (new StringBuilder()).append("X: ").append(xCords).toString(), mc.displayWidth / 4, 2, 0xe0e0e0);
Sign up to request clarification or add additional context in comments.

Comments

2

This should do what you want:

String coord_text = new StringBuilder()).append("X: ").append(xCords).toString();
gui.drawString(fontrenderer, coord_text, gui.width/2 - fontrenderer.getStringWidth(coord_text)/2, 2, 0xe0e0e0);

Comments

2

There is a specific function for doing this, i beleive it is called drawCenterString

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.