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 63669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:40:08+00:00 2026-05-10T18:40:08+00:00

My Program overrides public void paint(Graphics g, int x, int y); in order to

  • 0

My Program overrides public void paint(Graphics g, int x, int y); in order to draw some stings using g.drawString(someString, x+10, y+30);

Now someString can be quite long and thus, it may not fit on one line.

What is the best way to write the text on multiple line.
For instance, in a rectangle (x1, y1, x2, y2)?

  • 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. 2026-05-10T18:40:08+00:00Added an answer on May 10, 2026 at 6:40 pm

    Thanks to Epaga’s hint and a couple of examples on the Net (not so obvious to find! I used mainly Break a Line for text layout), I could make a component to display wrapped text. It is incomplete, but at least it shows the intended effect.

    class TextContainer extends JPanel {     private int m_width;     private int m_height;     private String m_text;     private AttributedCharacterIterator m_iterator;     private int m_start;     private int m_end;      public TextContainer(String text, int width, int height)     {         m_text = text;         m_width = width;         m_height = height;          AttributedString styledText = new AttributedString(text);         m_iterator = styledText.getIterator();         m_start = m_iterator.getBeginIndex();         m_end = m_iterator.getEndIndex();     }      public String getText()     {         return m_text;     }      public Dimension getPreferredSize()     {         return new Dimension(m_width, m_height);     }      public void paint(Graphics g)     {         super.paintComponent(g);          Graphics2D g2 = (Graphics2D) g;         FontRenderContext frc = g2.getFontRenderContext();          LineBreakMeasurer measurer = new LineBreakMeasurer(m_iterator, frc);         measurer.setPosition(m_start);          float x = 0, y = 0;         while (measurer.getPosition() < m_end)         {             TextLayout layout = measurer.nextLayout(m_width);              y += layout.getAscent();             float dx = layout.isLeftToRight() ?                     0 : m_width - layout.getAdvance();              layout.draw(g2, x + dx, y);             y += layout.getDescent() + layout.getLeading();         }     } } 

    Just for fun, I made it fitting a circle (alas, no justification, it seems):

    public void paint(Graphics g) {     super.paintComponent(g);      Graphics2D g2 = (Graphics2D) g;     FontRenderContext frc = g2.getFontRenderContext();      LineBreakMeasurer measurer = new LineBreakMeasurer(m_iterator, frc);     measurer.setPosition(m_start);      float y = 0;     while (measurer.getPosition() < m_end)     {         double ix = Math.sqrt((m_width / 2 - y) * y);         float x = m_width / 2.0F - (float) ix;         int width = (int) ix * 2;          TextLayout layout = measurer.nextLayout(width);          y += layout.getAscent();         float dx = layout.isLeftToRight() ?                 0 : width - layout.getAdvance();          layout.draw(g2, x + dx, y);         y += layout.getDescent() + layout.getLeading();     } } 

    I am not too sure about dx computation, though.

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

Sidebar

Ask A Question

Stats

  • Questions 81k
  • Answers 81k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, that's a good way to do it. The user… May 11, 2026 at 4:30 pm
  • Editorial Team
    Editorial Team added an answer First of all, the keyword is elsif, second of all… May 11, 2026 at 4:30 pm
  • Editorial Team
    Editorial Team added an answer In the HTML, put the full value in a custom… May 11, 2026 at 4:30 pm

Related Questions

I have in my Form constructor, after the InitializeComponent the following code: using (WebClient
I need advice on how to have my C# console application display text to
Ok, bear with me guys and girls as I'm learning. Here's my question. I
I'm writing a program that allows developers to write AddIn's for it and I'd

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.