Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Using Graphics::drawString() to center some text

Posted on 2007-11-29
6
4,061 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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 500 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

828 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