We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
scope problem
you define layout in setup(), it's not available in draw();
move line 3 before setup()
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> { }
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 justclass Pair<Shape extends PShape> { }
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? ;;)
who says he only wants to use Pair to hold PShapes?
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;
} }
/* 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
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.