Java by API/java.text/TextAttribute

Материал из Java эксперт
Версия от 14:44, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

TextAttribute.BACKGROUND

  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.BACKGROUND, Color.LIGHT_GRAY, 2, 9);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new Main());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.CHAR_REPLACEMENT

 
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.font.GraphicAttribute;
import java.awt.font.ImageGraphicAttribute;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.text.AttributedString;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass {
  public static void main(String[] args) {
    JFrame jf = new JFrame("Demo");
    Container cp = jf.getContentPane();
    MyCanvas tl = new MyCanvas();
    cp.add(tl);
    jf.setSize(300, 200);
    jf.setVisible(true);
  }
}
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font serifFont = new Font("Serif", Font.PLAIN, 32);
    AttributedString as = new AttributedString("www.jexp.ru");
    as.addAttribute(TextAttribute.FONT, serifFont);
    Image image =  createImage();
    ImageGraphicAttribute imageAttribute = new ImageGraphicAttribute(image,
        GraphicAttribute.TOP_ALIGNMENT);
    as.addAttribute(TextAttribute.CHAR_REPLACEMENT, imageAttribute, 5, 6);
    g2.drawString(as.getIterator(), 20, 120);
  }
  private BufferedImage createImage() {
    BufferedImage bim;
    Color[] colors = { Color.red, Color.blue, Color.yellow, };
    int width = 8, height = 8;
    bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bim.createGraphics();
    for (int i = 0; i < width; i++) {
      g2.setPaint(colors[(i / 2) % colors.length]);
      g2.drawLine(0, i, i, 0);
      g2.drawLine(width - i, height, width, height - i);
    }
    return bim;
  }
}





TextAttribute.FONT

  
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 40);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.FONT, font);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new Main());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.FOREGROUND

  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.FOREGROUND, Color.red, 0, 2);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new Main());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.SIZE

  
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.SIZE, 40);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new Main());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.STRIKETHROUGH

  
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 2, 8);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new Main());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.SUPERSCRIPT

  
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 5, 7);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new Main());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.UNDERLINE

  
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 3, 4);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new Main());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}