# Merging two sketches (pt.2)

**URL:** https://discourse.processing.org/t/merging-two-sketches-pt-2/33259
**Category:** Coding Questions
**Tags:** homework
**Created:** [November 2, 2021, 1:57pm UTC](https://discourse.processing.org/t/merging-two-sketches-pt-2/33259 "2021-11-02T13:57:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![acb](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@acb](https://discourse.processing.org/u/acb)
#### Post date: [November 2, 2021, 1:57pm UTC](https://discourse.processing.org/t/merging-two-sketches-pt-2/33259/1 "2021-11-02T13:57:58Z")

</div>

As mentioned in my last post, I am trying to merge these codes but I keep getting the error; “java.lang.reflect.InvocationTargetException” . From my research I was not able to understand (I am new to coding), could anyone explain the issue and what I need to change for it to run?  
Thanks

```auto
float angle; 
float x, y;
float xStep = 50;
float yStep = 50;
float a, a_;
int num = 328;

void setup() {
  size(200, 200);
    surface.setLocation(175,0);
  strokeWeight (5);
}

  void draw () {
background(15, 20, 30);
strokeWeight(xStep);
strokeCap(CORNER);
int n=8;
while (n<num) {
  stroke (255-255*cos(radians (a)),255*cos(radians(a)),255-255*sin(radians(a)), 255-255*sin(radians(a)));
  line (x, y, x, y+yStep);
  x+=xStep;
  if (x>width) {
  x=xStep/2;
  y+=yStep;
}
if (y>=height) {
  y=0;
  a=0;
}
n++;
a+=a_;
}
a_+=0.1;
}
{
 background (300);
  translate(width/2, height/2);
  for (int i=0; i < 10; i ++){
    fill (i*90, 300-i*90, 400-i*800);
    scale (0.99);
  rotate(radians(angle));
  rect (0, 0, 75, 75);
  angle+=0.03;
  }
}

```

---

<div class="post-metadata">

### Author: ![acb](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@acb](https://discourse.processing.org/u/acb)
#### Post date: [November 2, 2021, 2:00pm UTC](https://discourse.processing.org/t/merging-two-sketches-pt-2/33259/2 "2021-11-02T14:00:07Z")

</div>

(post deleted by author)

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [November 2, 2021, 3:03pm UTC](https://discourse.processing.org/t/merging-two-sketches-pt-2/33259/3 "2021-11-02T15:03:19Z")

</div>

Hello,

I commented the code that is the issue and program works.  
Commenting code is also a good way to troubleshoot.

The code that is commented should be inside `draw()` :

```auto
void draw() {
  // Put code here between the opening and closing brackets
   }

```

You will have to give some thought to where to put it…

```auto
float angle; 
float x, y;
float xStep = 50;
float yStep = 50;
float a, a_;
int num = 328;

void setup() {
  size(200, 200);
  surface.setLocation(175, 0);
  strokeWeight (5);
}

void draw () {
  background(15, 20, 30);
  strokeWeight(xStep);
  strokeCap(CORNER);
  int n=8;
  while (n<num) {
    stroke (255-255*cos(radians (a)), 255*cos(radians(a)), 255-255*sin(radians(a)), 255-255*sin(radians(a)));
    line (x, y, x, y+yStep);
    x+=xStep;
    if (x>width) {
      x=xStep/2;
      y+=yStep;
    }
    if (y>=height) {
      y=0;
      a=0;
    }
    n++;
    a+=a_;
  }
  a_+=0.1;
}

// This code needs to be inside draw(): 
//{
// background (300);
// translate(width/2, height/2);
// for (int i=0; i < 10; i ++) {
// fill (i*90, 300-i*90, 400-i*800);
// scale (0.99);
// rotate(radians(angle));
// rect (0, 0, 75, 75);
// angle+=0.03;
// }
//}

```

---

<div class="post-metadata">

### Author: ![acb](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@acb](https://discourse.processing.org/u/acb)
#### Post date: [November 2, 2021, 5:20pm UTC](https://discourse.processing.org/t/merging-two-sketches-pt-2/33259/4 "2021-11-02T17:20:03Z")

</div>

Hello glv,  
Thank you for responding to my post! Though I am still having difficulty finding where to place the code into the void drawing section. After trying to place it as written between every line within void draw, it either becomes a black screen or the background code spins around instead of the foreground. When you say place it into the drawing do you mean keep the code (down below) as written, or rather separate the text, (within the drawing) in order to get it to merge?  
Thankyou

```auto
{
background (300);
translate(width/2, height/2);
for (int i=0; i < 10; i ++) {
fill (i*90, 300-i*90, 400-i*800);
scale (0.99);
rotate(radians(angle));
rect (0, 0, 75, 75);
angle+=0.03;
}
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [November 2, 2021, 7:08pm UTC](https://discourse.processing.org/t/merging-two-sketches-pt-2/33259/5 "2021-11-02T19:08:16Z")

</div>

Hello,

You will have to give some thought to where to put it (your code)…

and which elements of code to keep or discard (comment for now).

> [@glv](#):
>
> Commenting code is also a good way to troubleshoot.

Keep working at it and experiment a little.

Keep in mind that you are digitally “painting on a canvas” from the start of draw to the end of draw and repeating.

If you call `background()` in the middle of your sketch or end of your sketch what happens?

Color is from 0 to 255 (default) and you are using 300 which ends up as black.

Lots of resources here:  
_[https://processing.org/](https://processing.org/)_

`:)`

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [November 2, 2021, 9:13pm UTC](https://discourse.processing.org/t/merging-two-sketches-pt-2/33259/6 "2021-11-02T21:13:35Z")

</div>

Just knock out the unwanted brackets and comment out the second background call and it should run just fine.
