从列表中访问原始类的超类

时间:2017-03-09 19:30:05

标签: java class processing

我正在尝试创建自己的GUI库,而我在使用Super class时遇到了一些困难(这对我来说很新,并且找不到好的资源来学习)。但我的所有组件都从" GGUI"扩展而来,然后存储在ArrayList中以便于访问。我的问题是我得到一个错误:"隐式超级构造函数GrimGUI.GGUI()未定义。必须显式调用另一个构造函数"。

GrimGUI:

Button testScreen;
Slider firstSlider;
Slider secondSlider;
Text firstSliderText;
Text secondSliderText;

boolean onHome;
boolean onTest;

ArrayList<GGUI> gguiScreen = new ArrayList<GGUI>();
void setup() {
  size(500, 500);
  onHome = true;
  background(255);
  gguiScreen.add(new Button(75, 50, 40, 20));

  testScreen = new Button(115, 50, 40, 20);
  testScreen.cNormal = color(0, 255, 125);


  firstSlider = new Slider(250, 250, 100);
  secondSlider = new Slider(250, 280, 100);

  firstSliderText = new Text(280, 250, "0");
  secondSliderText = new Text(280, 280, "0");
}

void draw() {
  background(255);
  HomeScreen();
  TestScreen();

  endFix();
}

void HomeScreen() {
  if (onHome) {
    testScreen.Update();
    firstSlider.Update();
    firstSliderText.content = str(firstSlider.value);
    firstSliderText.Update();
    if (testScreen.released) {
      onTest = true;
      onHome = false;
    }
  }
}

void TestScreen() {
  if (onTest) {
    Button trash = (Button)gguiScreen.get(0);//Testing that I can get the only object placed in it to view on screen
    trash.Update();
    secondSlider.Update();
    secondSliderText.content = str(secondSlider.value);
    secondSliderText.Update();
    if (trash.WasClicked()) {
      onTest = false;
      onHome = true;
    }
  }
}

按钮:

    class Button extends GGUI {
  boolean visible, pressed, released, hovering;
  color cNormal, cPressed, cReleased, cHovering;
  float x, y, w, h;

  Button(float X, float Y, float W, float H) {
    visible = true;
    x = X;
    y = Y;
    w = W;
    h = H;
    cNormal = color(75, 75, 75);
    cPressed = color(100, 100, 100);
    cReleased = color(125, 125, 125);
    cHovering = color(175, 175, 175);
  }
  Button(boolean visibility, float X, float Y, float W, float H) {
    visible = visibility;
    x = X;
    y = Y;
    w = W;
    h = H;
    cNormal = color(75, 75, 75);
    cPressed = color(100, 100, 100);
    cReleased = color(125, 125, 125);
    cHovering = color(175, 175, 175);
  }
  Button(boolean visibility, float X, float Y, float W, float H, color CNORMAL, color CPRESSED) {
    visible = visibility;
    x = X;
    y = Y;
    w = W;
    h = H;
    cNormal = CNORMAL;
    cPressed = CPRESSED;
    cReleased = color(125, 125, 125);
    cHovering = color(175, 175, 175);
  }
  Button(boolean visibility, float X, float Y, float W, float H, color CNORMAL, color CPRESSED, color CHOVERING) {
    visible = visibility;
    x = X;
    y = Y;
    w = W;
    h = H;
    cNormal = CNORMAL;
    cPressed = CPRESSED;
    cHovering = CHOVERING;
    cReleased = color(125, 125, 125);
  }
  Button(boolean visibility, float X, float Y, float W, float H, color CNORMAL, color CPRESSED, color CHOVERING, color CRELEASED) {
    visible = visibility;
    x = X;
    y = Y;
    w = W;
    h = H;
    cNormal = CNORMAL;
    cPressed = CPRESSED;
    cHovering = CHOVERING;
    cReleased = CRELEASED;
  }


  void Update() {
    CheckState();
    if (visible) {//WILL NOT RENDER IF NOT VISIBLE
      if (pressed && !released) {// IF PRESSED
        fill(cPressed);
      }
      if (released && !pressed) {// IF RELEASED
        fill(cReleased);
      }
      if (hovering && !released && !pressed) {// IF HOVERING
        fill(cHovering);
      }
      if (!hovering && !released && !pressed) {// IF NORMAL / NOT ACTIVE
        fill(cNormal);
      }
      stroke(1);
      rect(x, y, w, h);
    }
  }

  void CheckState() {
    hovering = false;
    released = false;
    if (mouseX > x && mouseX < x + w) {
      if (mouseY > y && mouseY < y + h) {
        hovering = true;
      }
    }
    if (hovering) {
      if (mPressed) {
        pressed = true; 
        released = false;
      }
      if (mClicked) {
        released = true;
        pressed = false;
      }
    } else {
      pressed = false;
      released = false;
      hovering = false;
    }
  }

  boolean WasClicked() {
    return pressed;
  }
}

GGUI:

class GGUI {
  int x, y;
  boolean visible;
  color cBase, cSecondary;

  GGUI(int _x, int _y, boolean _visible, color _cBase, color _cSecondary) {
    x = _x;
    y = _y;
    visible = _visible;
    cBase = _cBase;
    cSecondary = _cSecondary;
  }

  void Update() {
    println("Updated");
  }
}

1 个答案:

答案 0 :(得分:1)

  

隐式超级构造函数GrimGUI.GGUI()未定义

所以定义它

class GGUI {
    GGUI() {
        // Set some default values
    }
  

必须显式调用另一个构造函数

或者您需要调用super()构造函数

Button(<params...>) {
    super(<params...>)
    ... // set more things
}

但是,这没有意义,因为按钮似乎有float x,y而不是int x,y类所期望的GGUI

以这种方式思考 - GUI是否真的可以在一个像素的一小部分上放置一个按钮?

请注意,您可以大大减少代码

  Button(boolean visibility, float X, float Y, float W, float H, color CNORMAL, color CPRESSED, color CHOVERING, color CRELEASED) {
    visible = visibility;
    x = X;
    y = Y;
    w = W;
    h = H;
    cNormal = CNORMAL;
    cPressed = CPRESSED;
    cHovering = CHOVERING;
    cReleased = CRELEASED;
  }

  // For example... 
  Button(boolean visibility, float X, float Y, float W, float H, color CNORMAL, color CPRESSED, color CHOVERING) {
    // Call the other constructor
    this(visibility, X, Y, W, H, CNORMAL, CPRESSED, CHOVERING, color(125, 125, 125));
    cReleased = color(125, 125, 125);
  }