⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formattextcanvas.java

📁 一个简单的visio程序。
💻 JAVA
字号:
/**
 * FormatTextCanvas class, FormatTextCanvas.java
 * Author : Phong Nguyen Le
 * Created : 03/25/98
 */
 
package hc.util;

import java.awt.*;
import java.util.*;

public class FormatTextCanvas extends Canvas
{
  Vector m_lines = null;
  FontMetrics fm = null;

  public FormatTextCanvas(String text)
  {
    super();
    if(text == null) return;
    setFont(new Font("Dialog", Font.PLAIN, 12));
    fm = getFontMetrics(getFont());
    text = replace(text, "\t", "    ");
    m_lines = toLines(text);
    setSize(getFittedSize());
  }
  
  public FormatTextCanvas(String text, int width)
  {
    super();
    if(text == null) return;
    setFont(new Font("Dialog", Font.PLAIN, 12));
    fm = getFontMetrics(getFont());
    text = replace(text, "\t", "    ");
    m_lines = toLines(text);
    m_lines = formatText(m_lines, width);
    setSize(getFittedSize());
  }
  
  public FormatTextCanvas(String text, Font font) {
    super();
    if(text == null) return;
    Font aFont = font;
    if(font == null) {
	aFont = new Font("Dialog", Font.PLAIN, 12);
    }
    setFont(aFont);
    fm = getFontMetrics(aFont);
    text = replace(text, "\t", "    ");
    m_lines = toLines(text);
    setSize(getFittedSize());
  }
  
  public void update(Graphics g)
  {
    paint(g);
  }
  
  public void paint(Graphics g) {
    Dimension s = getSize();
    g.setColor(getBackground());
    g.fillRect(0, 0, s.width, s.height);
    g.setFont(getFont());
    g.setColor(getForeground());
    if(fm == null) {
	fm = getFontMetrics(getFont());
    }
    int fontH = fm.getHeight();
    int yoff = fm.getAscent();
    int n = m_lines.size();
    for(int i = 0; i < n; i++) {
	g.drawString((String)m_lines.elementAt(i), 0, yoff);
	yoff += fontH;
    }
  }

  Dimension getFittedSize() {
    if(m_lines == null) {
	return new Dimension(0, 0);
    }
    fm = getFontMetrics(getFont());
    if(fm == null) {
	System.out.println("can not get font metrics");
	return new Dimension(0, 0);
    }
    int maxLen = 0;
    int s = m_lines.size();
    for(int i = 0; i < s; i++) {
	int len = fm.stringWidth((String)m_lines.elementAt(i));
	if(len > maxLen) {
	    maxLen = len;
	}
    }
    return new Dimension(maxLen, s * fm.getHeight());
  }  
  
  String replace(String in, String old, String neu) {
    if(neu.compareTo(old) == 0) return in;
    String str = "";
    int i = 0, j = 0;
    int len = in.length();
    int m = old.length();
    while(i < len)
    {
	j = in.indexOf(old, i);
	if(j != -1)
	{
	    str += in.substring(i, j) + neu;
	    i = j + m;
	}
	else
	{
	    break;
	}
    }
    if(i < len)
    {
	str += in.substring(i);
    }
    return str;
  }
  
  /**
   * line separator = "\n"
   */
/*   
  String[] toLines(String str) {
    Vector vec = toLine(str, "\n");
    if(vec != null) {
	String[] lines = new String[vec.size()];
	vec.copyInto(lines);
	return lines;
    }
    return null;
  }
*/
  
  Vector toLines(String str) {
    return toLines(str, "\n");
  }
  
  Vector toLines(String str, String lineSep)
  {
    Vector re = new Vector();
    int begin = 0, end = 0;
    int len = lineSep.length();
    while((end = str.indexOf(lineSep, begin)) != -1)
    {
      re.addElement(str.substring(begin, end));
      begin = end + len;
    }
    re.addElement(str.substring(begin));
    return re;
  }
    
  Vector formatText(Vector ls, int width) {
    Vector re = new Vector();
    int s = ls.size();
    for(int i = 0; i < s; i++) {
	String temp = (String)ls.elementAt(i);
	while(fm.stringWidth(temp) > width) {
	    String[] pair = getALine(temp, width);
//	    System.out.println(temp);
	    re.addElement(pair[0]);
	    temp = pair[1];
	}
	if(temp.length() != 0) {
	    re.addElement(temp);
	}
    }
    return re;
  }
  
  String[] getALine(String line, int width) {
    String temp = line;
    String remainder = "";
    while(fm.stringWidth(temp) > width) {
	int id = temp.lastIndexOf(" ");
	if(id == -1) break;
	remainder = temp.substring(id) + remainder;
	temp = temp.substring(0, id);
    }
    int len = remainder.length();
    int i = 0;
    for(i = 0; i < len && remainder.charAt(i) == ' '; i++);
    if(i != 0) {
	remainder = remainder.substring(i);
    }
/*    
    while(remainder.startWith(" ")) {
	remainder = remainder.substring(1);
    }
*/    
//    System.out.println(line);
    String[] re = { temp, remainder };
    return re;   
  }
	  
} // end of FormatTextCanvas.java

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -