aspose file tools*
The moose likes Applets and the fly likes centering g.drawString Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "centering g.drawString" Watch "centering g.drawString" New topic
Author

centering g.drawString

Chant Dhames
Greenhorn

Joined: Jun 22, 2002
Posts: 8
I'm having trouble centering my text on an applet window. For whatever reason, I believe it's reporting the width incorrectly, whether I use stringWidth() or assign a bounding box to a 2d rectangle. If you run the code, you'll see what I'm talking about. Any help is appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class Shapes extends JApplet implements Runnable
{
private Dimension wh;
Thread firstThread;
String sS;
FontMetrics fm;
int textWidth;
int textHeight;
int sleep;
Rectangle2D rect;

private Image backImage;
private Graphics backGraphics;
public void init()
{
wh = getSize();
sleep = 50;
sS = "SIM STUDIO";
}
public void paint(Graphics g)
{
if(backGraphics == null)
{
backImage = createImage(wh.width, wh.height);
backGraphics = backImage.getGraphics();
}

fm = g.getFontMetrics();
rect = fm.getStringBounds(sS, g);

backGraphics.setFont(new Font("SansSerif", Font.BOLD, 26));
textWidth = (wh.width - ((int) (rect.getWidth())))/2;
textHeight = (wh.height + fm.getAscent())/2;
backGraphics.setColor(Color.blue);
backGraphics.fillRect(0, 0, wh.width, wh.height);
backGraphics.setColor(Color.white);
backGraphics.drawString(sS, textWidth + 2, textHeight + 2);
backGraphics.setColor(Color.lightGray);
backGraphics.drawString(sS, textWidth, textHeight);

showStatus("font width is " + fm.stringWidth(sS) + "and" + ((int)
(rect.getWidth())) + " total width " + wh.width);
backGraphics.fillRect(300, 100, 5, 5);
g.drawImage(backImage, 0, 0, this);
}
public void destroy()
{
}
}
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
I see a couple of problems.
First, you're getting a Dimension of the applet in the init() method, and then using this in paint(). I wouldn't assume that you are getting the valid dimension of the applet in init(). Until a Component (an Applet is a Component) is made displayable (isDisplayable() returns true), the size information is NOT correctly initialized for the Component. I'd suggest you lazily initialize this in your paint() method like you're trying to do with backGraphics. By the time paint() is called , you can be certain your Component is in a proper state to have meaningful values for it's size, bounds, etc.
Also, you get a fontMetrics object for the CURRENT Graphics font, then you CHANGE the actual font you're using, so of course you're getting the wrong stringWidth - you're getting the width for the Old font, not the one you changed it to.


Rob
SCJP 1.4
Chant Dhames
Greenhorn

Joined: Jun 22, 2002
Posts: 8
thx. The Dimension is fine, or at least in my case it's fine and gives the correct width/height of my applet. I'll keep it in mind though and change it if it ever becomes a problem. thx for helping with the font problem.
Chant Dhames
Greenhorn

Joined: Jun 22, 2002
Posts: 8
actually, that didn't make any difference. I moved my Dimension down into paint right above the if() statement, and then moved my font declaration above the Font Metrics statement but the same problem exists. Any other suggestions?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
You're changing the font of your backGraphics object, not the original Graphics that is passed into your method via the 'g' argument variable. Make sure you're calling getFontMetrics on the right Graphics object.
 
 
subject: centering g.drawString