here is an example in processing Java and in 3D, using box instead of rect
float angleRightArm=0.0;
float angleRightArmAdd=2;
void setup() {
size(1600, 900, P3D);
}
void draw() {
background(255);
lights();
rightArm();
}
void rightArm() {
// draw right arm
pushMatrix(); // store matrix
float armLength=300.0;
translate(255, 170, 0); // arm position
rotateX(radians(angleRightArm)); // rotate
sphere(); // for testing only
translate(0, 0, armLength/2-20); // that's the offset between arm center and where we want to have the shoulder / rotation !!!
// draw box/arm at 0,0,0 (box's center!) with the following width, height and depth:
fill(255);
stroke(0);
box (15, 35, armLength);
popMatrix(); // restore matrix
// manage angle and angle bouncing:
angleRightArm += angleRightArmAdd;
if (angleRightArm>12) {
angleRightArmAdd=-2;
}
if (angleRightArm<-152) {
angleRightArmAdd=2;
}
}
void sphere() {
// draw a sphere a the current 0,0,0 point
fill(255, 0, 0); // RED
noStroke();
sphere(11);
}
//