使用controlP5和创建对象时遇到麻烦

时间:2012-05-29 20:28:23

标签: java object listbox controls

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

所以基本上我显示了菜单,然后当你点击一个选项时,它会调用一个名为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

2 个答案:

答案 0 :(得分:0)

你检查过是否

int Type[]; 
调用null

时,Draw Shapefile中的

不是newShape(int)

答案 1 :(得分: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];