Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:26:29+00:00 2026-06-04T18:26:29+00:00

I have a Jpanel which for example is 500 width and 100 height. i

  • 0

I have a Jpanel which for example is 500 width and 100 height. i have the formula to calculate the center of the screen vertically (y) and horizontally (x). Which i use to drawString(myText, x, y) but it writes the text above the bounds of the jpanel. Here is my paintComponent code i am using.

  protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        String MenuString = Menu.get(MenuChannel);
        fontMetrics = g2d.getFontMetrics(realFont);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setBackground(Color.PINK);
        g2d.fillRect(0,0,getWidth(),getHeight());
        g2d.setColor(Color.WHITE);
        g2d.setFont(realFont);

        java.awt.geom.Rectangle2D rect = fontMetrics.getStringBounds(MenuString, g2d);
        int textHeight = (int)rect.getHeight();
        int textWidth = (int)rect.getWidth();
        int panelHeight = getHeight();
        int panelWidth = getWidth();
        int x = (panelWidth - textWidth) / 2;
        int y = (panelHeight - textHeight) / 2;
        System.out.println("tH:" + textHeight + "\ntW:" + textWidth + "\npH:" + panelHeight + "\npW:" + panelWidth);
        g2d.drawString(MenuString,x,y);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:26:30+00:00Added an answer on June 4, 2026 at 6:26 pm

    Yes – this is pain – drawString will draw your String using y as the bottom line (or base text line). If you want to center String in Rectangle you should just implement it using FontMetrics

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JPanel which contains some fields. The height of the JPanel is
I have a JFrame with an associated JPanel which fill the screen, both having
In the example below I have a JPanel with BoxLayout which contains another JPanel
I have JPanel Which will load images. Since images will not have the same
I have a JPanel which contains two JLists - both can have an item
I have a JPanel which uses a BoxLayout in the X_AXIS direction. The problem
Okay, I have a Jpanel which is shown as white in the below image
I have a JPanel in which I need to add bunch of JLabel s
I have a JPanel to which when a button is pressed I want to
I have a JPanel, which I would like to detect the following events (1)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.