# Class and background image

**URL:** <https://discourse.processing.org/t/class-and-background-image/17846>\
**Category:** Beginners\
**Created:** [February 15, 2020, 11:00pm UTC](https://discourse.processing.org/t/class-and-background-image/17846 "2020-02-15T23:00:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Taby](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Taby](https://discourse.processing.org/u/Taby)\
**Post date:** [February 15, 2020, 11:00pm UTC](https://discourse.processing.org/t/class-and-background-image/17846/1 "2020-02-15T23:00:27Z")

</div>

Hello.  
I wrote a code using functions and had a background image written like that:

```auto
PImage bg;

Void setup() { 
  size (600,400);
  bg = loadimage(“floral.jpg”);
}

Void draw() {
  background(bg);
  stroke(0);
} 

```

However, now I’m trying to convert everything to a class and not sure where to put the background image. Because when it’s in void setup, along with the size, it says that variable bg doesn’t exist. Although I declared it by saying PImage = bg.

If someone could please give any advice I’d appreciate it a lot.

Thanks!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 15, 2020, 11:25pm UTC](https://discourse.processing.org/t/class-and-background-image/17846/2 "2020-02-15T23:25:29Z")

</div>

> [@Taby](#):
>
> `Void setup() {`

> [@Taby](#):
>
> `Void draw() {`

There’s no `Void` keyword in Java. It’s `void` instead:

> **[void / Reference](https://processing.org/reference/void.html)**
>
> Keyword used to indicate that a function returns no value. Each function must either return a value of a specific datatype or use the keyword void to specify it returns nothing.

> [@Taby](#):
>
> `bg = loadimage(“floral.jpg”);`

It’s not **loadimage()** but **loadImage()**:

> **[loadImage() / Reference](https://processing.org/reference/loadImage_.html)**
>
> Loads an image into a variable of type PImage. Four types of images ( .gif, .jpg, .tga, .png) images may be loaded. To load correctly, images must be located in the …

> [@Taby](#):
>
> However, now I’m trying to convert everything to a `class`…

You can do something like this example:

```auto
static final String IMG_FILENAME = "floral.jpg";
BackgroundImage bgImg;

void setup() {
  size(600, 400);
  stroke(0);

  bgImg = new BackgroundImage(loadImage(IMG_FILENAME));
}

void draw() {
  bgImg.show();
}

class BackgroundImage {
  PImage bg;

  BackgroundImage(final PImage img) {
    bg = img;
    fitToCanvas();
  }

  BackgroundImage fitToCanvas() {
    bg.resize(width, height);
    return this;
  }

  BackgroundImage show() {
    background(bg);
    return this;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Taby](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Taby](https://discourse.processing.org/u/Taby)\
**Post date:** [February 16, 2020, 11:08am UTC](https://discourse.processing.org/t/class-and-background-image/17846/4 "2020-02-16T11:08:33Z")

</div>

Okay so it worked when I created separate class for the background. Thank you!
