Switch to full style
General Java code examples
Post a reply

java Break Line for text

Tue Nov 11, 2008 8:22 pm

java Break Line for text
Code:
import java.awt.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.font.*;
import java.util.Hashtable;

public class 
LineBreakExample extends JPanel {
  private 
LineBreakMeasurer lineBreakMeasurer;
 private 
int startend;
  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(nullfalsefalse));
  }
  public 
void paintComponent(Graphics g) {
  
Graphics2D graphics2D = (Graphics2Dg;
    
Dimension size getSize();
    
float width = (float) size.width;
  
float  X0;
    
lineBreakMeasurer.setPosition(start);
    while (
lineBreakMeasurer.getPosition() < end) {
    
TextLayout textLayout lineBreakMeasurer.nextLayout(width);
    
+= textLayout.getAscent();
    
0;
    
textLayout.draw(graphics2DXY);
  
+= 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(200200));
    
frame.show();
  }



Attachments
lineBreak.gif
line breaks in java
lineBreak.gif (4.96 KiB) Viewed 10328 times

Post a reply
  Related Posts  to : java Break Line for text
 the text file i saved all in one line. How can i solve it?     -  
 Line Styles in java     -  
 Reading a File Line by Line in php     -  
 Java- Copy text area into disabled text area     -  
 Break in Nested Loops     -  
 Text to speech in java     -  
 Text Texture in java     -  
 change Font of text in java     -  
 draw text effects in java     -  
 search for coding java/jsp for translation text     -  

Topic Tags

Java Graphics