font
Draw mixed style text example
In this tutorial you are going to see how can you draw a line of text that each word has a different style
In order to draw mixed style text in Java, all you have to do is:
- Set the desired font if different from default font using the
Fontclass - Use
AttributedStringto create String that you can work on each substring - Use its
addAttributemethod to set the font family and the color of substrings - Get an
TextLayoutinstance and give aGraphics.getFontRenderContext()as an arguments to its constructor, to create a drawble string
Take a look at the code snippet that follows:
package com.javacodegeeks.snippets.desktop;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.text.AttributedString;
public class DrawMixedStyleText {
public static void main(String[] args) {
// Create a frame
Frame frame = new Frame();
// Add a component with a custom paint method
frame.add(new CustomPaintComponent());
// Display the frame
int frameWidth = 300;
int frameHeight = 300;
frame.setSize(frameWidth, frameHeight);
frame.setVisible(true);
}
/**
* To draw on the screen, it is first necessary to subclass a Component and
* override its paint() method. The paint() method is automatically called
* by the windowing system whenever component's area needs to be repainted.
*/
static class CustomPaintComponent extends Component {
public void paint(Graphics g) {
// Retrieve the graphics context; this object is used to paint shapes
Graphics2D g2d = (Graphics2D) g;
/**
* The coordinate system of a graphics context is such that the
* origin is at the northwest corner and x-axis increases toward the
* right while the y-axis increases toward the bottom.
*/
int x = 0;
int y = 20;
// Set the desired font if different from default font
Font font = new Font("Serif", Font.PLAIN, 16);
// Apply styles to text
AttributedString astr = new AttributedString("This is a test string");
astr.addAttribute(TextAttribute.FONT, font, 0, 4);
astr.addAttribute(TextAttribute.FOREGROUND, Color.RED,5,9);
astr.addAttribute(TextAttribute.BACKGROUND, Color.CYAN, 10, 21);
// Draw mixed-style text such that its base line is at x, y
TextLayout tl = new TextLayout(astr.getIterator(), g2d.getFontRenderContext());
tl.draw(g2d, x, y);
}
}
}
This was an example on how to draw mixed styled text in Java.

