Solved

Using Graphics::drawString() to center some text

Posted on 2007-11-29
6
4,057 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 90
Java Jpanels and Jframe 8 28
Java Options in Zimbra Server 2 44
why cannot we forward request once the response is committed 2 33
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now