我对java(以及面向对象的编程)非常陌生。作为第一个 java 项目,我被指示启动并运行一个标签云引擎小程序。所以我找到了 WordCram 和一个使用 PApplets 的小示例代码,并认为我会很轻松......
但是,虽然我的代码本身不会产生任何错误,但它在运行时会抛出以下内容:
Exception in thread "Animation Thread" java.lang.NullPointerException
at processing.core.PApplet.handleDraw(PApplet.java:2336)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2176)
at java.lang.Thread.run(Thread.java:722)
我有大量代码,我很犹豫是否将其全部发布在这里......但是在试图深入了解我的代码的过程中,我总是对询问者提供的代码太少感到沮丧:所以我将暂时将其粘贴在这里,我会留意有人告诉我重新格式化或编辑部分或大部分内容。
两个类中的第一个:
package tagengine;
import java.awt.*;
public class TagFrame extends Frame {
public TagFrame()
{
super("Embedded PApplet");
Engine embed = new Engine();
setLayout(new BorderLayout());
add(embed, BorderLayout.CENTER);
embed.init();
}
public static void main(String[] args)
{
TagFrame tagFrame = new TagFrame();
}
}
第二个:
package tagengine;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PVector;
import wordcram.*;
public class Engine extends PApplet {
@Override public void setup() {
this.size(900, 500);
this.colorMode(HSB);
this.noLoop();
this.setVisible(true);
}
@Override public void draw() {
this.background(68);
new WordCram(this)
.fromWebPage("http://en.wikipedia.org/wiki/Barcamp")
.withColors(color(0, 0, 0),
color(0, 0, 255),
color(30, 255, 255)) // mustard, red)
.withFonts(PFont.list())
.withAngler(moreRandomByWeight())
.withPlacer(crazyPlacer())
.sizedByWeight(8, 100)
.maxNumberOfWordsToDraw(300)
.drawAll();
}
WordAngler moreRandomByWeight() {
return new WordAngler() {
@Override public float angleFor(Word w) {
float range = (1 - w.weight) * PI;
return random(-range, range);
}
};
}
WordPlacer crazyPlacer() {
return new WordPlacer() {
@Override public PVector place(Word w, int rank, int words,
int ww, int wh, int fw, int fh) {
float x = (fw - ww) * (1 - w.weight);
float y = random(fh - wh);
return new PVector(x, y);
}
};
}
@Override public void mousePressed() {
redraw();
}
}