Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Multiline text drawing with Graphics2D?

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Thanked 0 Times in 0 Posts

    Default Multiline text drawing with Graphics2D?


    I am going to draw some text with Graphics2D.
    I referenced the topic at Oracle web site "Drawing Multiple Lines of Text (The Java™ Tutorials > 2D Graphics > Working w".

    But I have some problem that if my String contains different fonts, how to determine the line break with a specific dimension of drawing region?

    For example,

    String s = "Hello World, It is a Java Duke!";

    In above string, Hello World uses Font "Arial", and the remaing parts of the string use Font "Courier".

    How to use "AttributedString", "TextLayout", etc to draw multiple lines with this case?
    Would you please to teach me, please?

    Best regards,

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: Multiline text drawing with Graphics2D?

    You have to do the calculations yourself. Check out the FontMetrics class.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Force Sync on Graphics2D?
    By Gerp in forum Java Theory & Questions
    Replies: 2
    Last Post: April 13th, 2011, 09:13 PM
  2. question about transforming Graphics2D
    By gib65 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 7th, 2010, 09:53 PM
  3. java graphics2d issue
    By nana-j13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2010, 03:49 PM
  4. Replies: 4
    Last Post: July 20th, 2010, 07:15 AM
  5. Replies: 1
    Last Post: November 14th, 2008, 03:00 PM