Why doesn't run? (Pshape pairs)

void setup() {
  size(400,400);
  Pair<PShape, PShape> layout;
  layout = new Pair (createShape(ELLIPSE, 0, 0, width, height), createShape(RECT, width/2, height/2, width/4, height/4));
}

void draw() {
  shape(layout.getFirst());
  shape(layout.getSecond());
}

class Pair<A,B> {
  A first;
  B second;

  Pair (A a, B b) {
    first = a;
    second = b;
  }

  A getFirst() { 
    return first;
  }

  B getSecond() {
    return second;
  }
}

Answers

  • Answer ✓

    scope problem

    you define layout in setup(), it's not available in draw();

    move line 3 before setup()

  • edited July 2016

    Merely declaring class Pair<A,B> { } is too vague for Java.
    We should rather be more specific in our generics.

    If it's about PShape, we should specify so via extends + type: O:-)
    class Pair<A extends PShape, B extends PShape> { }

    // forum.Processing.org/two/discussion/17457/why-doesn-t-run-pshape-pairs#Item_2
    // GoToLoop (2016-Jul-08);
    
    Pair<PShape, PShape> layout;
    
    void setup() {
      size(400, 400);
      frameRate(.5);
    
      stroke(0);
      strokeWeight(2.5);
    
      ellipseMode(CORNER);
      fill(#FFFF00); // yellow
      PShape circle = createShape(ELLIPSE, 0, 0, width>>1, height>>1);
    
      rectMode(CORNER);
      fill(#0080FF); // dark cyan
      PShape square = createShape(RECT, width>>1, height>>1, width>>2, height>>2);
    
      layout = new Pair<PShape, PShape>(circle, square);
    }
    
    void draw() {
      background((color) random(#000000));
      layout.displayFirst().displaySecond();
    }
    
    class Pair<A extends PShape, B extends PShape> {
      final A first;
      final B second;
    
      Pair (A a, B b) {
        first  = a;
        second = b;
      }
    
      Pair displayFirst() {
        shape(first);
        return this;
      }
    
      Pair displaySecond() {
        shape(second);
        return this;
      }
    }
    
  • edited July 2016

    However, given that both first & second are supposed to be of datatype PShape, we can simplify:
    class Pair<A extends PShape, B extends PShape> { } for just class Pair<Shape extends PShape> { }

    // forum.Processing.org/two/discussion/17457/why-doesn-t-run-pshape-pairs#Item_3
    // GoToLoop (2016-Jul-08);
    
    Pair<PShape> layout;
    
    void setup() {
      size(400, 400);
      frameRate(.5);
    
      stroke(0);
      strokeWeight(2.5);
    
      ellipseMode(CORNER);
      fill(#FFFF00); // yellow
      PShape circle = createShape(ELLIPSE, 0, 0, width>>1, height>>1);
    
      rectMode(CORNER);
      fill(#0080FF); // dark cyan
      PShape square = createShape(RECT, width>>1, height>>1, width>>2, height>>2);
    
      layout = new Pair<PShape>(circle, square);
    }
    
    void draw() {
      background((color) random(#000000));
      layout.displayFirst().displaySecond();
    }
    
    class Pair<Shape extends PShape> {
      final Shape first, second;
    
      Pair (Shape a, Shape b) {
        first  = a;
        second = b;
      }
    
      Pair displayFirst() {
        shape(first);
        return this;
      }
    
      Pair displaySecond() {
        shape(second);
        return this;
      }
    }
    
  • edited July 2016

    Actually I don't see any actual need to parameterize your custom class w/ generics at all. >-)
    How about just remove them all to leave things simpler, shall we? ;;)

    // forum.Processing.org/two/discussion/17457/why-doesn-t-run-pshape-pairs#Item_4
    // GoToLoop (2016-Jul-08);
    
    ShapePair layout;
    
    void setup() {
      size(400, 400);
      frameRate(.5);
    
      stroke(0);
      strokeWeight(2.5);
    
      ellipseMode(CORNER);
      fill(#FFFF00); // yellow
      PShape circle = createShape(ELLIPSE, 0, 0, width>>1, height>>1);
    
      rectMode(CORNER);
      fill(#0080FF); // dark cyan
      PShape square = createShape(RECT, width>>1, height>>1, width>>2, height>>2);
    
      layout = new ShapePair(circle, square);
    }
    
    void draw() {
      background((color) random(#000000));
      layout.displayFirst().displaySecond();
    }
    
    class ShapePair {
      final PShape first, second;
    
      ShapePair (PShape a, PShape b) {
        first  = a;
        second = b;
      }
    
      ShapePair displayFirst() {
        shape(first);
        return this;
      }
    
      ShapePair displaySecond() {
        shape(second);
        return this;
      }
    }
    
  • Answer ✓

    who says he only wants to use Pair to hold PShapes?

  • edited July 2016 Answer ✓

    Indeed his original implementation merely stores 2 generic types and got 2 methods to return them separately.

    It seems so little iMHO. That coulda been accomplished more easily w/ an ArrayList for example.

    In my tweaked versions, the class acts upon the stored references and invokes shape().

  • Thanks for ur answers. I understood my error I'm not so expert in Processing but I'm quite good on java. I'm creating a simple class wich contains 2 object a background (which could be also an Image) and a shape on the front which will be modify with "effects". Some good ideas? :)

  • //the questions are at the last line :)

    PShape shape; PImage img; Layout layout;

    void setup() { size(400,400); background(0); img = loadImage("Default.jpg"); shape = createShape(RECT, 0, 0, 50, 50); layout = new Layout(shape,img); } void draw() { layout.display(); }

    class Layout { /**Field: */ PShape shape; PImage image; Coordinate shapeXY; Coordinate imageXY;

    /**Builder: / Layout(PShape shape, PImage image) { this.shape = shape; this.image = image; shapeXY = new Coordinate(width0.5, height*0.5); imageXY = new Coordinate(0,0); }

    /**Access Methods: */ PShape getShape() { return shape; } PImage getImage() { return image; }

    /**Utility Methods: */ void display() { image(image, 0, 0); shape(getShape(), shapeXY.getX(), shapeXY.getY()); }

    /Nested Class: */ class Coordinate { /Nested Field: */ float x; float y;

    /**Nested Builder: */
    Coordinate() { this(0,0); }
    Coordinate(float x, float y) { setXY(x,y); }
    
    /**Nested Access Methods: */
    float getX() { return x; }
    float getY() { return y; }
    
    /**Modifier Methods: */
    void setX(float x) { this.x = x; }
    void setY(float y) { this.y = y; }
    void setXY(float x, float y) { setX(x); setY(y); }
    

    } }

    /* this is quite the same thing as using 2 obj as a "Pair". now i want to display it "correctly" (image full sized or resized and the shape at the center) */

  • edit post, highlight code, press ctrl-o...

  • Sorry i don't understand

  • Answer ✓

    look at your previous post. does it look alright?

    you need to indent code in order for it to show as code in the forum. otherwise it's gets printed as text and is unreadable. just edit the post, select the code and press ctrl-o.

Sign In or Register to comment.