- JDK :OpenJDK-11
- OS :CentOS 7.6.1810
- IDE :Eclipse 2019‑03
- typesetting :Markdown
package per.jizuiku.gui; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; /** * @author 給最苦 * @date 2019/06/30 * @blog www.cnblogs.com/jizuiku */ public class Demo { /** * @param args */ public static void main(String[] args) { try { getImage(0); getImage(1); // style=2,本應該是斜體效果。但是經過生成的圖片卻看不出來 getImage(2); getImage(3); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param fontStyle */ public static void getImage(int fontStyle) throws FileNotFoundException, IOException { // 獲得圖片緩衝區 int width = 100; int height = 50; int imageType = BufferedImage.TYPE_INT_BGR; BufferedImage myImage = new BufferedImage(width, height, imageType); // 獲得畫筆 Graphics2D pen = (Graphics2D)myImage.getGraphics(); // 設置筆的顏色,即背景色 pen.setColor(Color.WHITE); // 畫出一個矩形 // 座標x 座標y 寬度100 長度50 pen.fillRect(0, 0, 100, 50); // Mononspace 大小25 Font font = new Font("Mononspace", fontStyle, 25); pen.setFont(font); // 字的顏色 和 背景的顏色 要不一樣的 // 第一次沒有注意到,結果 運行了三四次 都發現沒有字呀 pen.setColor(Color.blue); // 寫字 pen.drawString("abcd", 20, 35); ImageIO.write(myImage, "JPEG", new FileOutputStream(fontStyle + ".jpg")); } }
/** * The plain style constant. */ public static final int PLAIN = 0; /** * The bold style constant. This can be combined with the other style * constants (except PLAIN) for mixed styles. */ public static final int BOLD = 1; /** * The italicized style constant. This can be combined with the other * style constants (except PLAIN) for mixed styles. */ public static final int ITALIC = 2;
/** * Creates a new {@code Font} from the specified name, style and * point size. * <p> * The font name can be a font face name or a font family name. * It is used together with the style to find an appropriate font face. * When a font family name is specified, the style argument is used to * select the most appropriate face from the family. When a font face * name is specified, the face's style and the style argument are * merged to locate the best matching font from the same family. * For example if face name "Arial Bold" is specified with style * {@code Font.ITALIC}, the font system looks for a face in the * "Arial" family that is bold and italic, and may associate the font * instance with the physical font face "Arial Bold Italic". * The style argument is merged with the specified face's style, not * added or subtracted. * This means, specifying a bold face and a bold style does not * double-embolden the font, and specifying a bold face and a plain * style does not lighten the font. * <p> * If no face for the requested style can be found, the font system * may apply algorithmic styling to achieve the desired style. * For example, if {@code ITALIC} is requested, but no italic * face is available, glyphs from the plain face may be algorithmically * obliqued (slanted). * <p> * Font name lookup is case insensitive, using the case folding * rules of the US locale. * <p> * If the {@code name} parameter represents something other than a * logical font, i.e. is interpreted as a physical font face or family, and * this cannot be mapped by the implementation to a physical font or a * compatible alternative, then the font system will map the Font * instance to "Dialog", such that for example, the family as reported * by {@link #getFamily() getFamily} will be "Dialog". * * @param name the font name. This can be a font face name or a font * family name, and may represent either a logical font or a physical * font found in this {@code GraphicsEnvironment}. * The family names for logical fonts are: Dialog, DialogInput, * Monospaced, Serif, or SansSerif. Pre-defined String constants exist * for all of these names, for example, {@code DIALOG}. If {@code name} is * {@code null}, the <em>logical font name</em> of the new * {@code Font} as returned by {@code getName()} is set to * the name "Default". * @param style the style constant for the {@code Font} * The style argument is an integer bitmask that may * be {@code PLAIN}, or a bitwise union of {@code BOLD} and/or * {@code ITALIC} (for example, {@code ITALIC} or {@code BOLD|ITALIC}). * If the style argument does not conform to one of the expected * integer bitmasks then the style is set to {@code PLAIN}. * @param size the point size of the {@code Font} * @see GraphicsEnvironment#getAllFonts * @see GraphicsEnvironment#getAvailableFontFamilyNames * @since 1.0 */ public Font(String name, int style, int size) { this.name = (name != null) ? name : "Default"; this.style = (style & ~0x03) == 0 ? style : 0; this.size = size; this.pointSize = size; }
感謝幫助過 給最苦 的人們。
Java、Groovy和Scala等基於JVM的語言,優秀,值得學習。
規範的命名和代碼格式等,有助於溝通和理解。
JVM的配置、監控與優化,比較實用,值得學習。java