编程珠玑banner函数
编写一个banner函数,该函数的输入为大写字母,输出为一个字符数组,该数组以图形化的方式表示该字母
package third; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import javax.swing.JLabel; public class Banner { public static void outBanner(String c) { Font font = new Font("sanserif", Font.PLAIN, 12); // use a JLabel to get a FontMetrics object FontMetrics metrics = new JLabel().getFontMetrics(font); int width = metrics.stringWidth(c); int height = (int) (metrics.getAscent() - metrics.getDescent()) - 1; // use ARGB or the background will be black as well BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // create a Graphics2D object from the BufferedImage Graphics2D g2d = bi.createGraphics(); g2d.setFont(font); g2d.setColor(Color.black); g2d.drawString(c, 0, height); g2d.dispose(); for (int j = 0; j < height; j++) { // Scroll through rows for (int i = 0; i < width; i++) { // Scroll through columns // check for != 0 as black is -(2^24) and that's negative System.out.print(bi.getRGB(i, j) != 0 ? c : " "); // If pixel // rgb value // is zero, // output // '0', // else // '1'(want // monochome // image) } System.out.print("\n"); // next line } } public static void main(String[] args) { outBanner("E"); } }
输出结果为:
EEEEEE
E
E
E
EEEEEE
E
E
E
EEEEEE

浙公网安备 33010602011771号