public class LineBreakExample extends JPanel { private LineBreakMeasurer lineBreakMeasurer; private int start, end; private static Hashtable hash = new Hashtable();
private static AttributedString attributedString = new AttributedString( "Java is an Object Oriented Programming Language which has " + " an extensive class library available in the core language packages. Java "+ "was designed with networking in mind and comes with many classes " +" to develop sophisticated Internet communications. ", hash); public LineBreakExample() { AttributedCharacterIterator attributedCharacterIterator = attributedString.getIterator(); start = attributedCharacterIterator.getBeginIndex(); end = attributedCharacterIterator.getEndIndex(); lineBreakMeasurer = new LineBreakMeasurer(attributedCharacterIterator, new FontRenderContext(null, false, false)); } public void paintComponent(Graphics g) { Graphics2D graphics2D = (Graphics2D) g; Dimension size = getSize(); float width = (float) size.width; float X, Y = 0; lineBreakMeasurer.setPosition(start); while (lineBreakMeasurer.getPosition() < end) { TextLayout textLayout = lineBreakMeasurer.nextLayout(width); Y += textLayout.getAscent(); X = 0; textLayout.draw(graphics2D, X, Y); Y += textLayout.getDescent() + textLayout.getLeading(); } } public static void main(String[] args) { JFrame frame = new JFrame("Show Line Break"); LineBreakExample controller = new LineBreakExample(); frame.getContentPane().add(controller,"Center"); frame.setSize(new Dimension(200, 200)); frame.show(); } }
Attachments:
File comment: line breaks in java lineBreak.gif [ 4.96 KiB | Viewed 5225 times ]
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )