0

原始文件是 WaveFont.pde,一个处理草图和一个使用贝塞尔曲线创建字符的示例。它使用 Fontastic 库(一个处理库)。我将此 .pde 文件导出到 android 并自动生成代码。但问题是,每当我尝试在我的 Android 设备上运行它时,它总是强制关闭,而且它也不会像在处理中那样生成 ttf。请查看以下代码行。谢谢你。

这是MainActivity.java

import android.app.Activity;
import android.os.Bundle;   

public class MainActivity extends Activity {
public static Wave font;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    font = new Wave();
    font.setup();
}
}

这是名为Wave.java的类

import fontastic.FPoint;
import fontastic.Fontastic;
import processing.core.PApplet;
import processing.core.PFont;

public class Wave extends PApplet {
    Fontastic f;
    float charWidth = 512;
    PFont myFont;
    int version = 0;
    boolean fontBuilt = false;

    public void setup(){
        randomize();
        create();
    }

    public void randomize(){
        version++;
        if (f != null) { f.cleanup(); }
        f = new Fontastic(this, "WaveFont" + nf(version,4));
        f.setAdvanceWidth(PApplet.parseInt(charWidth));
        for (int i=0; i<Fontastic.alphabet.length; i++) {
            char c = Fontastic.alphabet[i];
            FPoint[] points = new FPoint[4];
            float rectSize = charWidth * 0.5f;
            float rnd = charWidth * 0.2f;
            points[0] = new FPoint(charWidth / 2 - rectSize / 2, charWidth / 2 - rectSize / 2);
            points[1] = new FPoint(charWidth / 2 - rectSize / 2, charWidth / 2 + rectSize / 2);
            points[2] = new FPoint(charWidth / 2 + rectSize / 2, charWidth / 2 + rectSize / 2);
            points[3] = new FPoint(charWidth / 2 + rectSize / 2, charWidth / 2 - rectSize / 2);
            points[0].setControlPoint1(points[0].x + rnd, points[0].y + random(-rnd, rnd));
            points[1].setControlPoint1(points[1].x + random(-rnd, rnd), points[1].y - rnd);
            points[2].setControlPoint1(points[2].x - rnd, points[2].y + random(-rnd, rnd));
            points[3].setControlPoint1(points[3].x - random(-rnd, rnd), points[3].y + rnd);
            points[0].setControlPoint2(points[0].x + random(-rnd, rnd), points[0].y + rnd);
            points[1].setControlPoint2(points[1].x + rnd, points[1].y + random(-rnd, rnd));
            points[2].setControlPoint2(points[2].x + random(-rnd, rnd), points[2].y - rnd);
            points[3].setControlPoint2(points[3].x - rnd, points[3].y + random(-rnd, rnd));
            f.addGlyph(c).addContour(points);
        }
    }

    public void create(){
        f.buildFont();
        f.cleanup();
        myFont = createFont(f.getTTFfilename(), 200);
        fontBuilt = true;
    }
}

处理草图(WaveFont.pde)是:

import fontastic.*;
import java.util.List;

Fontastic f;

float charWidth = 512;

PFont myFont;
int version = 0;

boolean fontBuilt = false;

void setup() {

  size(1400, 600);
  fill(0);

  randomizeFont();

  createFont();

}


void draw() {

  background(255);

  strokeWeight(2);
  textSize(25); // for small numbers at bezier lines

  for (int i=0; i<5; i++) {
    pushMatrix();
    translate(width/2, height/4);
    scale(0.5);
    translate(-4*charWidth / 2 + i*charWidth, 0);
    translate(-charWidth/2, charWidth/2);
    renderGlyphOutline(Fontastic.alphabet[i],color(255,0,0), color(100)); 
    // fill(0,255,0,80);
    // renderGlyphSolid(Fontastic.alphabet[i]);
    popMatrix();
  }

  // fill(0,255,0,80);
  // noStroke();
  // renderGlyphSolid('A');

  if(fontBuilt) {
    pushMatrix();
    textFont(myFont);
    textAlign(CENTER, CENTER);
    fill(0);
    textSize(charWidth);
    text("ABCDE", width/2, height*0.6);
    popMatrix();
  }

}

void randomizeFont() {

  version++;

  if (f != null) { f.cleanup(); }

  f = new Fontastic(this, "WaveFont" + nf(version,4));

  f.setAdvanceWidth(int(charWidth));

  for (int i=0; i<Fontastic.alphabet.length; i++) {

    char c = Fontastic.alphabet[i];

    FPoint[] points = new FPoint[4];

    float rectSize = charWidth*0.5;
    float rnd = charWidth*0.2;

    points[0] = new FPoint(charWidth/2 - rectSize/2, charWidth/2 - rectSize/2);
    points[1] = new FPoint(charWidth/2 - rectSize/2, charWidth/2 + rectSize/2);
    points[2] = new FPoint(charWidth/2 + rectSize/2, charWidth/2 + rectSize/2);
    points[3] = new FPoint(charWidth/2 + rectSize/2, charWidth/2 - rectSize/2);

    points[0].setControlPoint1(points[0].x + rnd, points[0].y + random(-rnd, rnd));
    points[1].setControlPoint1(points[1].x + random(-rnd, rnd), points[1].y - rnd);
    points[2].setControlPoint1(points[2].x - rnd, points[2].y + random(-rnd, rnd));
    points[3].setControlPoint1(points[3].x - random(-rnd, rnd), points[3].y + rnd);

    points[0].setControlPoint2(points[0].x + random(-rnd, rnd), points[0].y + rnd);
    points[1].setControlPoint2(points[1].x + rnd, points[1].y + random(-rnd, rnd));
    points[2].setControlPoint2(points[2].x + random(-rnd, rnd), points[2].y - rnd);
    points[3].setControlPoint2(points[3].x - rnd, points[3].y + random(-rnd, rnd));

    f.addGlyph(c).addContour(points);

  }

}

void renderGlyphOutline(char c, color linecolor, color handlecolor) {

    FPoint[] points = f.getGlyph(c).getContour(0).getPointsArray();

    // Draw the outline in Processing 
    for (int i=0; i<points.length; i++) {
      FPoint p1 = points[i];
      FPoint p2 = points[(i+1)%points.length];
      bezierWithHandles(i+"", p1.x, -p1.y, p1.controlPoint2.x, -p1.controlPoint2.y, p2.controlPoint1.x, -p2.controlPoint1.y, p2.x, -p2.y, linecolor, handlecolor);
    }

}

void renderGlyphSolid(char c) {

  FContour[] contours = f.getGlyph(c).getContoursArray();

  for (int j=0; j<contours.length; j++) {

    FPoint[] points = f.getGlyph(c).getContour(j).getPointsArray();

    if (points.length > 0) { //just to be sure    
      // Draw the solid shape in Processing
      beginShape();      
      for (int i=0; i<points.length; i++) {
        FPoint p1 = points[i];
        FPoint p2 = points[(i+1)%points.length];
        if (p1.hasControlPoint2() && p2.hasControlPoint1()) {
          if (i == 0) { 
            vertex(points[0].x, -points[0].y);
          }
          bezierVertex(p1.controlPoint2.x, -p1.controlPoint2.y, p2.controlPoint1.x, -p2.controlPoint1.y, p2.x, -p2.y);
        }
        else {
          vertex(p1.x, -p1.y);
        }
      }
      endShape();
    }
  }

}


void createFont() {

  f.buildFont();
  f.cleanup();

  myFont = createFont(f.getTTFfilename(), 200);

  fontBuilt = true;

}



void bezierWithHandles(String legend, float p1x, float p1y, float cp1x, float cp1y, float cp2x, float cp2y, float p2x, float p2y, color linecolor, color handlecolor) {

  stroke(handlecolor);
  line(p1x, p1y, cp1x, cp1y);
  line(p2x, p2y, cp2x, cp2y);

  fill(handlecolor);
  noStroke();
  float ellipseSize = 10;
  ellipse(cp1x, cp1y, ellipseSize,ellipseSize);
  ellipse(cp2x, cp2y, ellipseSize,ellipseSize);

  stroke(linecolor);
  noFill();
  bezier(p1x, p1y, cp1x, cp1y, cp2x, cp2y, p2x, p2y);

  stroke(0);
  textAlign(CENTER,CENTER);
  text(legend, p1x, p1y+25);

}

void keyPressed() {

  if (key == ' ') {
    randomizeFont();
    fontBuilt = false;
  }

  if (key == 's') {
    createFont();
  }
}
4

1 回答 1

0

您的错误是抱怨代码使用java.awt.Point,这是有道理的,因为Android API没有实现该类。

看起来您使用的是 Java 库,而不是 Android 库。Android 和 Java几乎相同,但 Android 不包含 Swing 或 AWT 之类的东西,而这正是java.awt.Point类的来源。

要解决此问题,您需要找到专门为 Android 而不是一般 Java 构建的 API 版本。

于 2016-02-15T19:40:39.170 回答