-2

I am trying to create my own GUI library, and I am having some difficulty with Super class (This is kind of new to me, and couldn't find a good source to learn from). But I have all my components extend from "GGUI", which are then stored in an ArrayList for easy access. My problem is that I get an error: "Implicit super constructor GrimGUI.GGUI() is undefined. must explicitly invoke another constructor".

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;
    }
  }
}

Button:

    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");
  }
}
  • This is pretty simple. As long as you do not define any constructor in your Super-Class, Java creates one at compile time. And it also creates a call to this constructor from each extending class if you don't call super yourself. But you define several different constructors and no default constructor in your super class. As you are forced to call at least one super-constructor but Java can't choose for you, the error is thrown. Either you implement an empty constructor in your super class or you call one of your super constructors from each of your local constructors. –  Mar 09 '17 at 19:39

1 Answers1

1

Implicit super constructor GrimGUI.GGUI() is undefined

So define it

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

must explicitly invoke another constructor

Or you need to invoke super() constructor

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

But, that doesn't make sense because buttons seems to have float x,y and not int x,y like the GGUI class expects.

Think about it this way - Can the GUI really place a button on a fraction of a pixel?


Note that you can significantly reduce your code like so

  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);
  }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thank you. The source I learned this from completely left out having to call the "super()" within each constructor-AT THE BEGINNING(? Not sure, but only spot it worked). Buttons were my first component, I just did not realize that floats couldn't be used- and was to lazy to change float to int like I did the other components. – Tim Leitzke Mar 10 '17 at 01:09
  • And I did not know you could even call a constructor from a constructor – Tim Leitzke Mar 10 '17 at 01:58
  • You must call the other constructors as the very first instruction, yes – OneCricketeer Mar 10 '17 at 02:45