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

Related Questions

I am using gwt graphics and gwt dnd for a program. I have a
public void onListItemClick(ListView parent, View v, int position, long id) { super.onListItemClick(parent, v, position,
This program runs just fine, but for some reason when I ask for an
The program I'm coding now makes a pretty huge list of data items. Now,
In my program I want to draw a simple score line graph. I have
I have this problem bothering me for days. I'm making a special paint program.
This is my simple program: public class Test { public static Object obj =
I have a sample program for paint. package deck_flight.com; import android.app.Activity; import android.content.Context; import
I'm trying to to program the Mandelbrot set for Android. I try to draw
I am writing an info-screen program. I created a full-screen widget and draw contents

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.