0

我有一个菜单系统,可以显示可以在屏幕上绘制的不同类型的对象。

所以基本上我已经显示了菜单,然后当你点击一个选项时,它会调用一个名为 newShape() 的函数,它会记下对象的类型并给它一个初始位置。

然后,当 draw() 被循环时,它将调用一个名为 drawShapes() 的函数,该函数将使用数组中的值并创建用户单击的对象。然后它将跟随用户的鼠标指针,直到它被点击。

代码被分成3个文件,以使其整洁。

主文件

import processing.opengl.*;

void setup(){
  size(500,500,OPENGL);
  background(255);
  setupMenu();
}

void draw(){
  background(255);
  fill(0);
  drawShapes();
}

菜单文件

import controlP5.*;

ControlP5 cp5;
ListBox l;
String CI[] = {"Box", "Sphere", "Ellipsoid", "Toroid", "Text"};

void setupMenu() {

  cp5 = new ControlP5(this);
  l = cp5.addListBox("createList")
         .setPosition(0,16)
         .setSize(120, 120)
         .setItemHeight(15)
         .setBarHeight(15)
         .setColorBackground(color(40, 128))
         .setColorActive(color(255, 128))
         ;

  l.captionLabel().toUpperCase(true);
  l.captionLabel().set("Create Item");
  l.captionLabel().setColor(0xffff0000);
  l.captionLabel().style().marginTop = 3;
  l.valueLabel().style().marginTop = 3;


  for (int i = 0; i < CI.length; i++){
    ListBoxItem lbi = l.addItem(CI[i], i);
    lbi.setColorBackground(0xffff0000);
  }

}

void controlEvent(ControlEvent theEvent) {

  if(theEvent.isGroup() && theEvent.name().equals("createList")){
    int Choice = (int)theEvent.group().value();    
    newShape(Choice);
  }
}

绘制形状文件

 int ob = 0;
 int Type[];
 float Pos[][];
 boolean posSet[];

void newShape(int c){
  Type[ob] = c;
  Pos[ob][0] = mouseX;
  Pos[ob][1] = mouseY;
  posSet[ob] = false;
  ob++;
}

void drawShapes(){
  for (int i = 0; i < ob; i++){
    switch(Type[ob]){

      case 0: {
        if (posSet[ob] == false){
          box(Pos[ob][0],Pos[ob][1], 50);
          }
         else {
           box(mouseX,mouseY, 50); break;
         }
      }
    }
  }
}

编辑:还应该添加错误代码...

30/05/2012 9:57:09 AM controlP5.ControlBroadcaster printMethodError
SEVERE: An error occured while forwarding a Controller event, please check your code at controlEvent
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at         sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
at controlP5.ControlBroadcaster.invokeMethod(Unknown Source)
at controlP5.ControlBroadcaster.broadcast(Unknown Source)
at controlP5.ListBox.controlEvent(Unknown Source)
at controlP5.Controller.broadcast(Unknown Source)
at controlP5.Button.setValue(Unknown Source)
at controlP5.Button.activate(Unknown Source)
at controlP5.Button.mouseReleased(Unknown Source)
at controlP5.Controller.setMousePressed(Unknown Source)
at controlP5.ControllerGroup.setMousePressed(Unknown Source)
at controlP5.ControllerGroup.setMousePressed(Unknown Source)
at controlP5.ControlWindow.mouseReleasedEvent(Unknown Source)
at controlP5.ControlWindow.mouseEvent(Unknown Source)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at processing.core.PApplet$RegisteredMethods.handle(Unknown Source)
at processing.core.PApplet.handleMouseEvent(Unknown Source)
at processing.core.PApplet.dequeueMouseEvents(Unknown Source)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException
at Main.newShape(Main.java:43)
at Main.controlEvent(Main.java:104)
... 25 more
4

2 回答 2

0

你检查过是否

int Type[]; 

null调用时不在您的 Draw Shape File 中newShape(int)

于 2012-05-30T06:46:29.017 回答
0

好的,问题是数组没有初始化。

int Type[];

本来应该...

int[] Type = new int[size];

我不知道数组需要什么大小...所以我只是输入了一个很大的值。这是所有声明的代码...

int ob = 0, maxShapes = 300, currentShape;
int[] Type = new int[maxShapes];
float[][] Pos = new float[maxShapes][maxShapes];
boolean[] posSet = new boolean[maxShapes];
于 2012-05-30T16:47:28.930 回答