Example usage for java.awt RenderingHints KEY_ANTIALIASING

List of usage examples for java.awt RenderingHints KEY_ANTIALIASING

Introduction

In this page you can find the example usage for java.awt RenderingHints KEY_ANTIALIASING.

Prototype

Key KEY_ANTIALIASING

To view the source code for java.awt RenderingHints KEY_ANTIALIASING.

Click Source Link

Document

Antialiasing hint key.

Usage

From source file:IteratorUnderStrike.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    String s = "\"www.java2s.com\" is great.";

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font plainFont = new Font("Times New Roman", Font.PLAIN, 24);

    AttributedString as = new AttributedString(s);
    as.addAttribute(TextAttribute.FONT, plainFont);
    as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 1, 15);
    as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 18, 25);

    g2.drawString(as.getIterator(), 24, 70);
}

From source file:FontDerivation.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Create a 1-point font.
    Font font = new Font("Serif", Font.PLAIN, 1);
    float x = 20, y = 20;

    Font font24 = font.deriveFont(24.0f);
    g2.setFont(font24);//from  w  w w  .j av a  2  s  .  com
    g2.drawString("font.deriveFont(24.0f)", x, y += 30);

    Font font24italic = font24.deriveFont(Font.ITALIC);
    g2.setFont(font24italic);
    g2.drawString("font24.deriveFont(Font.ITALIC)", x, y += 30);

    AffineTransform at = new AffineTransform();
    at.shear(.2, 0);
    Font font24shear = font24.deriveFont(at);
    g2.setFont(font24shear);
    g2.drawString("font24.deriveFont(at)", x, y += 30);

    Hashtable attributes = new Hashtable();
    attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
    Font font24bold = font24.deriveFont(attributes);
    g2.setFont(font24bold);
    g2.drawString("font24.deriveFont(attributes)", x, y += 30);
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    Font f = new Font("Arial", Font.PLAIN, 48);
    g2.setFont(f);/*from ww  w  . java2 s. c om*/

    g2.drawString("www.java2s.com", 10, 60);

    RenderingHints rh = g2.getRenderingHints();
    rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHints(rh);

    g2.drawString("Antialiased text", 10, 120);
}

From source file:TransparentText.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    // the rendering quality.
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // a red rectangle.
    Rectangle2D r = new Rectangle2D.Double(50, 50, 550, 100);
    g2.setPaint(Color.red);//from   w  w  w .j a  v a2s.co m
    g2.fill(r);
    // a composite with transparency.
    Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
    g2.setComposite(c);
    // Draw some blue text.
    g2.setPaint(Color.blue);
    g2.setFont(new Font("Times New Roman", Font.PLAIN, 72));
    g2.drawString("Java Source and Support", 25, 130);
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 72);
    g2.setFont(font);//w  ww .j a  v  a 2  s  .c  o m

    String s = "this is a test";
    float x = 50, y = 150;

    // Draw the baseline.
    FontRenderContext frc = g2.getFontRenderContext();
    float width = (float) font.getStringBounds(s, frc).getWidth();
    Line2D baseline = new Line2D.Float(x, y, x + width, y);
    g2.setPaint(Color.red);
    g2.draw(baseline);

    // Draw the ascent.
    LineMetrics lm = font.getLineMetrics(s, frc);
    Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent());
    g2.draw(ascent);

    // Draw the descent.
    Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent());
    g2.draw(descent);

    // Draw the leading.
    Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width,
            y + lm.getDescent() + lm.getLeading());
    g2.draw(leading);

    // Render the string.
    g2.setPaint(Color.black);
    g2.drawString(s, x, y);
}

From source file:MainClass.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    Font f = new Font("Arial", Font.PLAIN, 48);
    g2.setFont(f);/*from  w w  w  .  j av a 2 s .c  o m*/

    g2.drawString("www.java2s.com", 10, 60);

    RenderingHints rh = g2.getRenderingHints();
    rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHints(rh);

    g2.drawString("Antialiased text", 10, 120);

}

From source file:TexturedText.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Times New Roman", Font.PLAIN, 72);
    g2.setFont(font);/*from   ww w. j  a  v  a2 s  .co  m*/

    String s = "Java Source and Support";
    Dimension d = getSize();
    float x = 20, y = 100;

    BufferedImage bi = getTextureImage();
    Rectangle r = new Rectangle(0, 0, bi.getWidth(), bi.getHeight());
    TexturePaint tp = new TexturePaint(bi, r);
    g2.setPaint(tp);

    g2.drawString(s, x, y);
}

From source file:FontShow.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    Font font = new Font("Dialog", Font.PLAIN, 96);
    g2.setFont(font);//from  www  .  j ava  2  s  .  co m
    int width = getSize().width;
    int height = getSize().height;

    String message = "Java2s";

    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics metrics = font.getLineMetrics(message, frc);
    float messageWidth = (float) font.getStringBounds(message, frc).getWidth();

    // center text
    float ascent = metrics.getAscent();
    float descent = metrics.getDescent();
    float x = (width - messageWidth) / 2;
    float y = (height + metrics.getHeight()) / 2 - descent;

    int PAD = 25;
    g2.setPaint(getBackground());
    g2.fillRect(0, 0, width, height);

    g2.setPaint(getForeground());
    g2.drawString(message, x, y);

    g2.setPaint(Color.white); // Base lines
    drawLine(g2, x - PAD, y, x + messageWidth + PAD, y);
    drawLine(g2, x, y + PAD, x, y - ascent - PAD);
    g2.setPaint(Color.green); // Ascent line
    drawLine(g2, x - PAD, y - ascent, x + messageWidth + PAD, y - ascent);
    g2.setPaint(Color.red); // Descent line
    drawLine(g2, x - PAD, y + descent, x + messageWidth + PAD, y + descent);
}

From source file:RotatedText.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    String s = "111111111111111111111111111111";

    Font font = new Font("Courier", Font.PLAIN, 12);
    g2d.translate(20, 20);// w w  w.j a  v a2s. com

    FontRenderContext frc = g2d.getFontRenderContext();

    GlyphVector gv = font.createGlyphVector(frc, s);
    int length = gv.getNumGlyphs();
    System.out.println(length);

}

From source file:LineMetricsIllustration.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 72);
    g2.setFont(font);//from  ww  w . j a v a2 s  .  c  om

    String s = "Java Source and Support";
    float x = 50, y = 150;

    // Draw the baseline.
    FontRenderContext frc = g2.getFontRenderContext();
    float width = (float) font.getStringBounds(s, frc).getWidth();
    Line2D baseline = new Line2D.Float(x, y, x + width, y);
    g2.setPaint(Color.red);
    g2.draw(baseline);

    // Draw the ascent.
    LineMetrics lm = font.getLineMetrics(s, frc);
    Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent());
    g2.draw(ascent);

    // Draw the descent.
    Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent());
    g2.draw(descent);

    // Draw the leading.
    Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width,
            y + lm.getDescent() + lm.getLeading());
    g2.draw(leading);

    // Render the string.
    g2.setPaint(Color.black);
    g2.drawString(s, x, y);
}