Is there a way to make a java.awt.Font strike through?
Logical Fonts Java defines five logical font families that are Serif, SansSerif, Monospaced, Dialog, and DialogInput. It must be supported by the JRE. Note that JRE maps the logical font names to physical font because these are not the actual font libraries.
Basically, create a new AttributedString out of your string, add a STRIKETHROUGH attribute, and draw. NOTE: This will NOT make the FONT strikethrough, but you can set the text to that font and then strikethrough on the text. This is simply another way of doing it. Rich's answer actually makes the font strikethrough.
Strikethrough is a font effect that causes text to appear as though it is crossed out. For example, this text should have a line through the middle of it. The strikethrough effect may be enabled through font properties if a program supports it, or applied to text on a web page using the HTML or CSS.
All implementations of the Java Platform must support TrueType fonts; support for other font technologies is implementation dependent. Physical fonts may use names such as Helvetica, Palatino, HonMincho, or any number of other font names.
So i found this, which works.
Font font = new Font("helvetica", Font.PLAIN, 12);
Map attributes = font.getAttributes();
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
Font newFont = new Font(attributes);
underline and strikethrough examples in awt.
http://www.java2s.com/Code/Java/2D-Graphics-GUI/TextAttributeUnderlineandstrikethrough.htm
Basically, create a new AttributedString out of your string, add a STRIKETHROUGH attribute, and draw.
NOTE: This will NOT make the FONT strikethrough, but you can set the text to that font and then strikethrough on the text. This is simply another way of doing it. Rich's answer actually makes the font strikethrough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With