mymail 发表于 2013-2-7 17:55:06

一个用来记单词的swing小程序

    构思很久,终于完成了一个用来记英文单词的swing小程序。
    程序主要由两个界面构成,一个学习与默写的界面,和一个输入界面。http://www.agoit.com/upload/attachment/140072/669e9b82-3a3a-336a-93b4-adf0dca88ce4.jpg
http://www.agoit.com/upload/attachment/140074/05da1dd3-3a75-373d-a943-ea66020caf47.jpg
 
http://www.agoit.com/upload/attachment/140082/602fd8fd-dce2-3321-afb8-121e74eb9889.jpg
 
 
     在写程序的过程中遇到一个问题,就是国际音标的输入。金山词霸上的国际音标用的是金山字体,这种东东我可玩不转,于是就用一个偷懒的方法,那就是从金山词霸2007上截图,图片存入数据库。java截图方法是从《swing hack》上
借鉴的,主要思路是整个屏幕,得到一张图片,写一个类继承JComponent,重写paintComponent方法把得到图片绘制该组件上,把该组件放到一个JFrame上,去掉JFrame的边框,在JFrame上监听鼠标事件,进行指定区域的屏幕截图。
      在学习与默写界面使用JEditPane显示组装的htm文本,国标音标使用一个img标记来显示,但是不能再通过url来加载图片了,因为图片是存储在数据库中的。通过研究jdk源代码和调试终于找到了让img标记从本地数据库中加载的方法。
主要的思路是写一个MyHTMLEditorKit类 继承HTMLEditorKit,在MyHTMLEditorKit 写一个内部类MyViewFactory去继承HTMEditortoolkit的内部类ViewFactory,重写ViewFactory的create方法,当解析到IMG标记时返回自定义的ImageView的子类,不再返回ImageView类。
 
MyHTMLEditorKit 的代码:
package com.myswing;
import javax.swing.text.Element;
import javax.swing.text.LabelView;
import javax.swing.text.StyleConstants;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.InlineView;

public class MyHTMLEditorKit extends HTMLEditorKit {
 /**
  * serialVersionUID
  */
 private static final long serialVersionUID = -6075329078257754709L;
 
 
 @Override
 public ViewFactory getViewFactory() {
  
  return new MyViewFactory();
 }
 
 public class MyViewFactory extends HTMLFactory {
  
  
  
  
  
  
  /**
   * Creates a view from an element.
   *
   * @param elem
   *            the element
   * @return the view
   */
  public View create(Element elem) {
   Object o = elem.getAttributes().getAttribute(
     StyleConstants.NameAttribute);
   if (o instanceof HTML.Tag) {
    HTML.Tag kind = (HTML.Tag) o;
    if (kind == HTML.Tag.CONTENT) {
     return new InlineView(elem);
    
    } else if (kind == HTML.Tag.IMG) {
     return new MyImageView(elem);
    } else {
     return super.create(elem);     
    }
   }
   // default to text display
   return new LabelView(elem);
  }
 }
}
 

MyView类的代码:
package com.myswing;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.text.Element;
import javax.swing.text.View;
import javax.swing.text.html.HTML;
import javax.swing.text.html.ImageView;
import oracle.net.ano.SupervisorService;
import service.servericeimpl.BusinessServiceImpl;
import com.sun.java_cup.internal.internal_error;
public class MyImageView extends ImageView {
 public MyImageView(Element element) {
  super(element);
 }
 Image imageChache = null;
 @Override
 public Image getImage() {
  String src = (String) getElement().getAttributes().getAttribute(
    HTML.Attribute.SRC);
  if (src.startsWith("image:")) {
   System.out.println("test image");
   if (imageChache != null)
    return imageChache;
   String word = src.substring(src.indexOf(":")+1);
   URL url = this.getClass().getClassLoader().getResource("A-05.jpg");
   
   
   Image tempImage = BusinessServiceImpl.getIntance().getImage(word);
   if ( tempImage != null ) {
    this.imageChache = tempImage;
   } else {
    imageChache = null;
   }  
   
   return imageChache;
  }
  return super.getImage();
 }
 @Override
 public void setSize(float width, float height) {
  if (imageChache != null) {
   width = imageChache.getWidth(null);
   height = imageChache.getHeight(null);
   System.out.println(width + ":" + height);
  }
  super.setSize(width, height);
 }
 public void paint(Graphics g, Shape a) {
  Rectangle rect = (a instanceof Rectangle) ? (Rectangle) a : a
    .getBounds();
  Image image = getImage();
  Rectangle clip = g.getClipBounds();
  if (image != null) {
   int width = image.getWidth(null);
   int height = image.getHeight(null);
   // Draw the image
   g.drawImage(image, rect.x + 1, rect.y + 1, width,
     height, null);
  }
 }
}
 
 
 
 
页: [1]
查看完整版本: 一个用来记单词的swing小程序