Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using Graphics::drawString() to center some text

Posted on 2007-11-29
6
Medium Priority
?
4,070 Views
Last Modified: 2012-08-13
Hi,

Is there  a way to render text centered, using Graphics::drawString()?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
6 Comments
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20376113
You want to center text inside some rectangle for example?
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20376139
drawString(String str, int x,  int y)

try something like
x = widthOfApplet/2 - str.length()/2
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20376186
@UrosVidojevic:
yes i want to center it inside a rectangle. I can't find anything like we have under win32 development!

@contactkarthi:
doesn't that assume a fixed width font?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20376232
Here is an example of how you can center text inside a panel.

http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/text.html
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 20376344
Use the FontMetrics for your font, to calculate the width of the string.


Font myFont = new Font("Verdana", Font.PLAIN, 11);
FontMetrics myFontMetrics = getFontMetrics(myFont);
..
String str="I am in the centre!";

int stringWidth=myFontMetrics.stringWidth(str);
int stringHeight=myFontMetrics.getHeight();

g.setFont(myFont);
g.drawString(str, panelWidth/2 - stringWidth/2, panelHeight/2+stringHeight/2);
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20376404
Example of centring text inside specified rectangle.

import java.awt.*;
import javax.swing.*;

class CenterPanel extends JPanel {
      private int x, y, w, h;
      private String txt;
      
      CenterPanel(int x, int y, int w, int h, String txt) {
            this.x = x; this.y = y; this.w = w; this.h = h; this.txt = txt;
      }
      
      public void paintComponent(Graphics g) {
            super.paintComponent(g);
            
            /* this part draws centred text */
            FontMetrics fm = g.getFontMetrics ();
            
            int txt_width = fm.stringWidth (txt);
            int ascent = fm.getMaxAscent ();
            int descent= fm.getMaxDescent ();

            int txt_x = x + (w-txt_width) / 2;
          int txt_y = y+h/2 - descent/2 + ascent/2;

          g.drawString (txt, txt_x, txt_y);
          g.drawRect(x, y, w, h);
            
      }
}

public class CenterText extends JFrame {
      public CenterText() {
            super("Center Text");
            
            getContentPane().add(new CenterPanel(100, 100, 300, 200, "Center this text"));
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            setSize(600, 400);
      }
      
      public static void main(String[] args) {
            new CenterText();
      }
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question