サンプルで出力したフラッシュのswfファイル

サンプルコード


import com.anotherbigidea.flash.movie.*;
import com.anotherbigidea.flash.structs.*;
 
public class JavaSwfSample2 {
  
  public static void main(String[] args) throws Exception {
    
    final int width = 400;
    final int height = 50;
    
    Shape text = new Shape();
    text.defineFillStyle(new Color(126, 126, 192));
    text.defineLineStyle(1, new Color(255, 255, 255));
    text.setRightFillStyle(1);
    text.setLineStyle(1);
    text.line(width, 0);
    text.line(width, height);
    text.line(0, height);
    text.line(0, 0);
    
    final float fontsize = 30.0f;
    
    java.awt.Font font = java.awt.Font.decode(null).deriveFont(fontsize);
    GryphOut.PathDrawer drawer = new GryphOut.SwfPathDrawer(text);
    GryphOut go = new GryphOut();
    go.setFont(font);
    go.draw("helloこんにちは赤ちゃん!♪", 0, fontsize, drawer);
    
    Movie movie = new Movie();
    movie.setWidth(width);
    movie.setHeight(height);
    movie.setBackColor(new Color(128, 0, 128));
    Frame frame = movie.appendFrame();
    frame.placeSymbol(text, 0, 0);
    frame.stop();
 
    movie.write("sample2.swf");
  }
  
}

GryphOut クラス

java.awt.Font からフォントの形状を抜き出して、FlatteningPathIterator で平坦化。
平坦化した PathIterator を com.anotherbigidea.flash.movie.Shape で描画する。


import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
 
import com.anotherbigidea.flash.movie.Shape;
 
public class GryphOut {
 
  private final FontRenderContext frc;
  private Font font = java.awt.Font.decode(null);
  
  public GryphOut(){
    this.frc = createFontRenderContext();
  }
 
  public void setFont(Font font){
    this.font = font;
  }
  
  public void draw(String text, float x, float y, PathDrawer drawer){
    GlyphVector gv = font.createGlyphVector(frc, text.toCharArray());
    for(int i=0; i<text.length(); i++){
      java.awt.Shape s = gv.getGlyphOutline(i, x, y);
      // FlatteningPathIterator で平坦化。
      // SEG_QUADTO や SEG_CUBICTO を無くして
      // SEG_MOVETO, SEG_LINETO, SEG_CLOSE だけにする。
      PathIterator pi = s.getPathIterator(null);
      pi = new FlatteningPathIterator(pi, 0.1);
      drawPath(pi, drawer);
    }
  }
 
  private static FontRenderContext createFontRenderContext(){
    //FontRenderContext frc = new FontRenderContext( null, true, false );
    BufferedImage bi = new BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bi.createGraphics();
    //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    //g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    FontRenderContext frc = g.getFontRenderContext();
    return frc;
  }
 
  /**
   * PathIterator オブジェクトの構成点を出力する。
   */
  private static void drawPath(PathIterator pi, PathDrawer drawer){
 
    while (!pi.isDone()) {
      double coords[] = new double[6];
      int type = pi.currentSegment(coords);
      switch (type) {
        case PathIterator.SEG_CLOSE :
          //System.out.println("CLOSE :");
          break;
        case PathIterator.SEG_CUBICTO :
          //System.out.println("CUBICTO :");
          break;
        case PathIterator.SEG_LINETO :
          //System.out.println("LINETO :");
          drawer.line(coords[0], coords[1]);
          break;
        case PathIterator.SEG_MOVETO :
          //System.out.println("MOVETO :");
          drawer.move(coords[0], coords[1]); 
          break;
        case PathIterator.SEG_QUADTO :
          //System.out.println("QUADTO :");
          drawer.quad(coords[0], coords[1], coords[2], coords[3]); 
          break;
      }
      //System.out.print(coords[0]); System.out.print(",");
      //System.out.print(coords[1]); System.out.print(",");
      //System.out.print(coords[2]); System.out.print(",");
      //System.out.print(coords[3]); System.out.print(",");
      //System.out.print(coords[4]); System.out.print(",");
      //System.out.print(coords[5]); System.out.println();
      pi.next();
    }
  }
  
  public static interface PathDrawer{
    void move(double x, double y);
    void line(double x, double y);
    void quad(double x, double y, double cx, double cy);
  }
  
  public static class SwfPathDrawer implements PathDrawer {
 
    private final Shape shape;
 
    public SwfPathDrawer(Shape shape) {
      this.shape = shape;
    }
 
    public void move(double x, double y) {
      shape.move(x, y);
    }
 
    public void line(double x, double y) {
      shape.line(x, y);
    }
 
    public void quad(double x, double y, double cx, double cy) {
      // これはダメ:ギザギザになってしまう
      // quad をどうやったら curve の引数に変換できるんだろうか?
      shape.curve(x, y, cx, cy);
    }
  }
}

tags: zlashdot Java Flash Java JavaSWF

Posted by NI-Lab. (@nilab)