List of usage examples for java.text AttributedString addAttributes
public void addAttributes(Map<? extends Attribute, ?> attributes, int beginIndex, int endIndex)
From source file:Main.java
/** * Reads a <code>AttributedString</code> object that has been serialised by * the {@link SerialUtilities#writeAttributedString(AttributedString, * ObjectOutputStream)} method.//from w w w . j av a 2s. c o m * * @param stream the input stream (<code>null</code> not permitted). * * @return The attributed string object (possibly <code>null</code>). * * @throws IOException if there is an I/O problem. * @throws ClassNotFoundException if there is a problem loading a class. */ public static AttributedString readAttributedString(ObjectInputStream stream) throws IOException, ClassNotFoundException { if (stream == null) { throw new IllegalArgumentException("Null 'stream' argument."); } AttributedString result = null; final boolean isNull = stream.readBoolean(); if (!isNull) { // read string and attributes then create result String plainStr = (String) stream.readObject(); result = new AttributedString(plainStr); char c = stream.readChar(); int start = 0; while (c != CharacterIterator.DONE) { int limit = stream.readInt(); Map atts = (Map) stream.readObject(); result.addAttributes(atts, start, limit); start = limit; c = stream.readChar(); } } return result; }
From source file:MainClass.java
public void paint(Graphics g) { Dimension size = getSize();/* ww w . j ava 2 s .com*/ String s = "To java2s.com or not to java2s.com, that is a question"; Hashtable map = new Hashtable(); map.put(TextAttribute.SIZE, new Float(32.0f)); AttributedString as = new AttributedString(s, map); map = new Hashtable(); map.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); as.addAttributes(map, 33, 52); AttributedCharacterIterator aci = as.getIterator(); int startIndex = aci.getBeginIndex(); int endIndex = aci.getEndIndex(); LineBreakMeasurer measurer; measurer = new LineBreakMeasurer(aci, new FontRenderContext(null, false, false)); measurer.setPosition(startIndex); float wrappingWidth = (float) size.width; float Y = 0.0f; while (measurer.getPosition() < endIndex) { TextLayout layout = measurer.nextLayout(wrappingWidth); Y += layout.getAscent(); float X = 0.0f; switch (justify) { case LEFT: if (layout.isLeftToRight()) X = 0.0f; else X = wrappingWidth - layout.getAdvance(); break; case RIGHT: if (layout.isLeftToRight()) X = wrappingWidth - layout.getVisibleAdvance(); else X = wrappingWidth; break; case CENTER: if (layout.isLeftToRight()) X = (wrappingWidth - layout.getVisibleAdvance()) / 2; else X = (wrappingWidth + layout.getAdvance()) / 2 - layout.getAdvance(); break; case EQUALITY: layout = layout.getJustifiedLayout(wrappingWidth); } layout.draw((Graphics2D) g, X, Y); Y += layout.getDescent() + layout.getLeading(); } }