/**
* @(#)Text2Image.java 1.0 December 2002
*
* Copyright 2002 BeanSoft. All rights reserved.
*
* This software is the proprietary information of BeanSoft.
* Use is subject to license terms.
*
*
* Text2Image 可轻松将您输入的汉字,英文转换成由字符组成的特殊字体.
* 可将转换后的文字用于QQ,ICQ,MSN及邮件中.
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.datatransfer.*;
public class Text2Image extends Frame implements ActionListener
{
int width = 10, height = 10;
ByteArrayOutputStream out = new ByteArrayOutputStream();
String symbol;
FileDialog fileDialog;
Thread convertThread = null;
public boolean isStandardAlone = true;
TextArea textArea = new TextArea();
Panel panelInput = new Panel();
TextField fieldInput = new TextField(20);
Choice choiceSymbol = new Choice();
Label labelInputHint = new Label();
Label labelCharHint = new Label();
Panel panelButtons = new Panel();
Button buttonSave = new Button();
Button buttonCopy = new Button();
Button buttonConvert = new Button();
Button buttonAbout = new Button();
Button buttonHelp = new Button();
Label labelFontHint = new Label();
TextField fieldFontName = new TextField();
Label labelHeightHint = new Label();
TextField fieldHeight = new TextField();
Button buttonExit = new Button();
GridLayout gridLayoutInputPanel = new GridLayout();// Target character choice
public Text2Image()
{
// Set event handlers
this.addWindowListener (new WindowAdapter(){
public void windowClosing(WindowEvent e){
buttonExit_actionPerformed(null);
}
});
this.setIconImage(Toolkit.getDefaultToolkit().getImage("icon.jpg"));
fieldInput.addActionListener(this);
fieldFontName.addActionListener(this);
fieldHeight.addActionListener(this);
buttonConvert.addActionListener(this);
try {
buildSymbolChoice();
jbInit();
} catch(Exception e) {
e.printStackTrace();
}
this.setSize(800, 500);
this.show();
}
public void actionPerformed(ActionEvent e) {
if(convertThread == null) {
convertThread = new Thread() {
public void run() {
convert();
}
};
convertThread.start();
}
}
void buildSymbolChoice() {
choiceSymbol.addItem("■");
choiceSymbol.addItem("★");
choiceSymbol.addItem("□");
choiceSymbol.addItem("☆");
choiceSymbol.addItem("◇");
choiceSymbol.addItem("▲");
choiceSymbol.addItem("△");
choiceSymbol.addItem("▼");
choiceSymbol.addItem("▽");
choiceSymbol.addItem("◎");
choiceSymbol.addItem("·");
choiceSymbol.addItem("∞");
choiceSymbol.addItem("∷");
choiceSymbol.addItem("╳");
choiceSymbol.addItem("√");
choiceSymbol.addItem("¥");
}
void convert() {
try {
buttonConvert.setEnabled(false);
textArea.setText("正在转换中,请稍候...");
out.reset();
Image image = createImage(width, height);// This is a temparary image
String s = fieldInput.getText();
String fontName = fieldFontName.getText();
symbol = choiceSymbol.getSelectedItem();
if(s.equals("")) throw new Exception("对不起,转换之前请首先输入要转换的字符!");
if(fontName.equals("")) throw new Exception("对不起,转换之前请首先输入一个字体名!");
height = Integer.parseInt(fieldHeight.getText());
Font f = new Font(fontName, Font.PLAIN, height);
Graphics g = image.getGraphics();
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
width = fm.stringWidth(s);
height = fm.getHeight();
image = createImage(width, height);// This is the really image to draw string
g = image.getGraphics();
g.setFont(f);
g.drawString(s, 0, fm.getAscent());
handlePixels(image, 0, 0, width, height);
textArea.setText(getCleanedEmptyLineOutput());
} catch(Exception ex) {
if(ex instanceof NumberFormatException)
textArea.setText("错误: 请您输入有效的字体高度.");
else textArea.setText("错误: " + ex.getMessage());
}
buttonConvert.setEnabled(true);
convertThread = null;
}
String getCleanedEmptyLineOutput() {
byte[] bits = out.toByteArray();
String result = "";
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(bits)));
String line;
while((line = in.readLine()) != null) {
if(line.indexOf(symbol) != -1)// Decide if a emtpy line
result += line + "\r\n";
}
in.close();
}catch(Exception ex){
}
return result;
}
public void handlesinglepixel(int x, int y, int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
// Deal with the pixel as necessary...
String s = ((red + green + blue) == 0)? symbol:" ";// 2 spaces
try {
out.write(s.getBytes());
if(x == width - 1) {
out.write("\r\n".getBytes());
}
}catch(Exception ex){}
}
public void handlePixels(Image img, int x, int y, int w, int h) {
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return;
}
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
handlesinglepixel(x+i, y+j, pixels[j * w + i]);
}
}
}
// Menu Save As...
void saveAs() {
fileDialog = new FileDialog(this, "");
fileDialog.setMode(FileDialog.SAVE);
fileDialog.setTitle("另存为");
fileDialog.show();
String fileName = fileDialog.getFile();
if(fileName == null) return;
else saveFile(fileDialog.getDirectory() + fileName);
}
// Save the text to the file
void saveFile(String filePath) {
if(filePath == null || filePath.equals("")) return;
String s = new String(out.toByteArray());
try
{
FileWriter out = new FileWriter(filePath);
out.write(s);
out.close();
}
catch(Exception e)
{
textArea.append("无法保存文件" + filePath + ".原因:" + e + "\n");
}
s = null;
}
private void jbInit() throws Exception {
this.setTitle("Text2Image 1.0 字符文本图片转换器 - 作者:刘长炯");
labelInputHint.setText("请输入要转换的字符[个数不限]:");
fieldInput.setColumns(20);
fieldInput.setText("通信工程");
labelCharHint.setText("请选择显示的符号:");
buttonSave.setLabel("保存");
buttonSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonSave_actionPerformed(e);
}
});
buttonCopy.setLabel("复制");
buttonCopy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonCopy_actionPerformed(e);
}
});
buttonConvert.setLabel("转换");
buttonAbout.setLabel("关于");
buttonAbout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonAbout_actionPerformed(e);
}
});
buttonHelp.setLabel("帮助");
buttonHelp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonHelp_actionPerformed(e);
}
});
labelFontHint.setText("请输入字体名[某些字体名系统可能不支持]:");
fieldFontName.setColumns(4);
fieldFontName.setText("宋体");
labelHeightHint.setText("输出字体高度[点数]:");
fieldHeight.setColumns(2);
fieldHeight.setText("18");
buttonExit.setLabel("退出");
buttonExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonExit_actionPerformed(e);
}
});
panelInput.setLayout(gridLayoutInputPanel);
gridLayoutInputPanel.setColumns(2);
gridLayoutInputPanel.setRows(0);
gridLayoutInputPanel.setHgap(10);
gridLayoutInputPanel.setVgap(5);
panelInput.setBackground(new Color(192, 192, 255));
panelButtons.setBackground(new Color(255, 200, 100));
textArea.setForeground(Color.green);
textArea.setBackground(Color.black);
this.add(textArea, BorderLayout.CENTER);
this.add(panelInput, BorderLayout.NORTH);
panelInput.add(labelInputHint, null);
panelInput.add(fieldInput, null);
panelInput.add(labelCharHint, null);
panelInput.add(choiceSymbol, null);
panelInput.add(labelFontHint, null);
panelInput.add(fieldFontName, null);
panelInput.add(labelHeightHint, null);
panelInput.add(fieldHeight, null);
this.add(panelButtons, BorderLayout.SOUTH);
panelButtons.add(buttonSave, null);
panelButtons.add(buttonCopy, null);
panelButtons.add(buttonConvert, null);
panelButtons.add(buttonAbout, null);
panelButtons.add(buttonHelp, null);
panelButtons.add(buttonExit, null);
}
void buttonExit_actionPerformed(ActionEvent e) {
this.dispose();
if(isStandardAlone)
System.exit(0);
}
void buttonCopy_actionPerformed(ActionEvent e) {
copy(textArea.getText());
}
// Copy selected text to clipboard
private void copy(String s) {
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
if ( s == null || s.equals("") ) return;
try {
clip.setContents( new StringSelection(s), null);
}catch(Exception e) {
}
}
public static void main(String args[])
{
new Text2Image();
}
void buttonSave_actionPerformed(ActionEvent e) {
saveAs();
}
void buttonAbout_actionPerformed(ActionEvent e) {
textArea.setText("Text2Image 1.0 字符文本图片转换器\n"
+ "可轻松将您输入的汉字,英文转换成由字符组成的特殊字体.\n"
+ "可将转换后的文字用于QQ,ICQ,MSN及邮件中.\n"
+ "版本: 1.0; 免费软件; 作者:刘长炯; 日期: 2002年12月\n"
+ "本程序用 Java 语言编写, 可运行于任何支持 Java 的 OS 上.\n"
+ "Copyright (c) 2002 通信工程学院 刘长炯. All rights reserved.\n"
);
}
void buttonHelp_actionPerformed(ActionEvent e) {
textArea.setText(
"输入要转换的字符串, 选择显示的符号, 输入字体名, 指定字体高度,\n" +
"最后按下\"转换\"按钮, 或者在上面的三个输入区域按下\"Enter\"键就开始转换了.\n" +
"如果输入的字符较多,或者字体太高, 可能需要多等一会儿.\n" +
"保存: 将当前转换结果保存到文件中." +
"复制: 将转换结果复制到剪贴板上." +
"\n\n" +
"我们强烈建议您使用 Java 2 平台来运行这个程序, 这样您就可以选择更多的字体效果.\n" +
"在低版本的 Java 平台上字体高度不能选的太大, 否则会出现失真现象."
);
}
}