Noob Questions From Nickropheliac: First game String drawing issues

In the single digits, the string is centered. How would I do this for double digits. I was thinking about offsetting x, but… I don’t understand offsets to their full extent. I’m not entirely sure if that would work. I’m too big of a noob, but I’m new and you guys are always willing to help! :smiley:

public void paintComponent(Graphics g) {

		super.paintComponent(g);
		Random rand = new Random();
	
		for (int lineY = 0; lineY < getHeight(); lineY += 50) {
			g.setColor(new Color(rand.nextInt(0xFF4AF6)));
			g.fillRoundRect(400, lineY, 20, 800, 0, 0);
		}

		g.setColor(Color.WHITE);
		g.fillRoundRect(ballX, ballY, 25, 25, 5, 25);
	for (int tick = 0; tick > getHeight(); tick--){
	
            g.setColor(new Color (rand.nextInt(0xFF4AF6)));
	}
	g.setColor(new Color(rand.nextInt(0xFF4AF6)));
	g.fillRoundRect(playerOneX, playerOneY, playerOneWidth,
					playerOneHeight, 50, 5);

			g.fillRoundRect(playerTwoX, playerTwoY, playerTwoWidth,
					playerTwoHeight, 50, 5);
	
			g.setColor(Color.WHITE);
			g.fillRoundRect(335, 180, 150, 150 , 50, 50);
			  g.setFont(new Font(Font.DIALOG, Font.BOLD, 150));
		      String rally = " " + rallyScore;
			  g.setColor(new Color (0xFF2172));
		      g.drawString(rally, 328, 310);
			  g.dispose();	
	}

Well, just make an if to verify if the score is made by double digits and if it is to render the string with a generalized x for double digits


if(rally > 9){
	g.drawString(rally, 290, 310);
}else{
	g.drawString(rally, 328, 310);
}

I hope this helps. :smiley:

The best way would be to get the width of the rendered string and center it around the point you want to render it.

not sure if that’s what you need but you could work with the bounds of a drawn string in advance :


// [...]
Font font = new Font(Font.DIALOG, Font.BOLD, 150);
String text_to_draw = "sometext";

g.setFont(font);

FontRenderContext context;

// if "g" is available you can use :
context = g.getFontRenderContext();

// if not (with antialiasing) :
// context = new FontRenderContext(new AffineTransform(),RenderingHints.VALUE_TEXT_ANTIALIAS_ON,RenderingHints.VALUE_FRACTIONALMETRICS_ON);

Rectangle2D.Float bounds = (Rectangle2D.Float)font.getStringBounds(text_to_draw, context);

// use bounds to figure proper position where to draw the text.
// [...]

First, You need to set the position where the rallyScore is rendered dynamically. Based on your GUI height/width. Then “offset” it in a if/else statement, Where “if (rallyScore == doubledigits )”:


guiWidth = 800;
guiHeight = 600;

if (rallyScore.length() > 1) {
   g.drawString(rally, (guiWidth / 2) - (rallyScore.length() / 2), guiHeight / 2);
} else {
   g.drawString(rally, guiWidth / 2, guiHeight / 2);
}

Hope this makes sense, You’ll have to adjust the values to position the score according to how you want it.

Only way to do it that isn’t a total hack is basil’s method: get the rectangle that represents the drawn string’s bounds, offset by half the rect’s width to center the string.