There is no built in Java method to copy an object: it is just too complex. Some objects reference others objects - if the latter are immutable (eg. strings), they can be shared by all copies. But some referenced objects must be duplicated as well, and can reference other objects, etc.
One solution is to use
clone() or, perhaps simpler, to make a copy constructor.
Example of the latter (working well for such simple objects we do in P.):
Code:class Stuff
{
int x, y;
color c;
String m;
// Regular constructor
Stuff(int xx, int yy, color cc, String msg)
{
x = xx; y = yy; c = cc; m = msg;
}
// Copy constructor
Stuff(Stuff s)
{
this(s.x, s.y, s.c, s.m);
}
String toString()
{
return "Stuff: " + x + " " + y + " " + hex(c) + " " + m;
}
}
void setup()
{
Stuff s1 = new Stuff(10, 10, #FF0000, "First stuff");
println(s1);
// Copy
Stuff s2 = new Stuff(s1);
println(s2);
// Alter the first
s1.x = 20; s1.m += ". Yeah!";
println(s1);
println(s2);
// Alter the second
s2.y = 20; s2.m += ". Right.";
println(s1);
println(s2);
}