I'm trying to draw a string at the center of an applet. The problem is that it's STARTING to draw at the center of the applet and going off to the right. I need for the center of the string to be centered. Here's my code so far:

/**
 * @(#)CIS226_Assignment_03_centerString.java
 *
 * CIS226_Assignment_03_centerString Applet application
 *
 * @author
 * @version 1.00 2010/9/24
 */

import java.awt.*;
import java.applet.*;

public class CIS226_Assignment_03_centerString extends Applet {

	public void init() {
	}

	public void centerString(Graphics g)
	{
		int x = getSize().width;
		int y = getSize().height;

		int c1 = x/2;
		int c2 = y/2;

		g.drawString("Welcome to Java!!", c1, c2 );
	}

	public void paint(Graphics g) {

		centerString(g);

	}
}

Can anyone give me some insight as to what I'm doing wrong?

Recommended Answers

All 8 Replies

I should mention that it needs to work for any size string... I can't just offset it a bit to center that one string.

Take a piece of paper and draw a rectangle representing the applet's area.
Place your String where you want it in that rectangle. Draw some lines from the left side of the rectange to the begining of the String and from the top of the rectangle to where the String is to draw.
Now do some elementary math to compute the length of those lines.
Some hints:
the center point of the rectangle is the width / 2
The center of the string is its width / 2
If you want the center of the string at the center of the rectangle, do the math to find where the beginning of the string must be to have the string's center be at the center of the rectangle.

Since the number of characters in a String is not the number of pixels it will take to draw the String the above is only an approximation. You need to get the pixel length of the String to get its length in pixels. This involves using the Font that you are using and getting the FontMetrics object and using its stringWidth method. Search for code samples for these, I'm not 100% sure on they're usage.

NormR1, if you'll notice, I already have the center points... please don't patronize me. The problem that I explained is that the string's beginning point draws at the center, rather than the entire string centering in the applet.

Did you take a piece of paper and make the drawings with the lines and compute the lengths of the lines as I suggested? This is really simple arithmetic.

I don't know how to compute the width of a string... maybe I could start with that? Can someone find me an article or something?

Use the Graphics getFontMetrics method and then the stringWidth method I mentioned earlier.

I just got a response email from my professor. It looks like he just wanted it to start printing at the center, not actually be centered. I'll still look into the Font Metrics, but it looks like I already have the solution.

Centering the text would look nicer.
But it depends on the program requirements and what else is being displayed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.