# Set present background colour

**URL:** <https://discourse.processing.org/t/set-present-background-colour/6957>\
**Category:** Coding Questions\
**Created:** [December 28, 2018, 4:48pm UTC](https://discourse.processing.org/t/set-present-background-colour/6957 "2018-12-28T16:48:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![garrettlynch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/garrettlynch/32/19853_2.png) [@garrettlynch](https://discourse.processing.org/u/garrettlynch)\
**Post date:** [December 28, 2018, 4:48pm UTC](https://discourse.processing.org/t/set-present-background-colour/6957/1 "2018-12-28T16:48:09Z")

</div>

I’m trying to set the present background colour of a sketch’s second window launched through `PApplet.runSketch()`, however nothing I’ve tried works. I don’t want to set processing’s background preference for all sketches. Below is my code with comments of what I’ve tried so far - most of it is ignored without errors.

Can anybody see what I’m doing wrong or know of an alternative solution? Thanks in advance.

```auto
//new window instance
Main win;

public void settings()
{
  size(320, 240);
}

void setup()
{
  surface.setLocation(20, 30);
  surface.setTitle("Menu");

  //** fourth attempt at setting present background colour, just in case it 
  //affects the child window but it seems to be ignored
  //frame.setBackground(new Color(255, 0, 0));

  //create new window
  win = new Main();
}

void draw()
{
  background(100, 100, 100);
}

```

```auto
class Main extends PApplet
{
  Main()
  {
    //** first attempt at setting present background colour, all flags work but bgcolor does nothing.
    PApplet.runSketch(new String[] {"--present", "--hide-stop", "--bgcolor=#ff0000", "--display=1", this.getClass().getSimpleName()}, this);
  }

  void settings()
  {
    size(800, 600);  
  }

  void setup()
  {
    //** second attempt at setting present background colour, both are ignored.
    //frame.setBackground(new java.awt.Color(255, 0, 0));
    //frame.setBackground(new Color(255, 0, 0));

    //** third attempt at setting present background colour, these create errors.
    //((JFrame) frame).getContentPane().setBackground(Color.BLACK);
    //frame.getContentPane().setBackground(new Color(255, 0, 0));

    surface.setTitle("Main");
    background(0, 0, 0);
  }

  void draw()
  {
    rect(random(width), random(height), random(50), random(50));
  }

  void exit()
  {
    //dispose of this class, set window to invisible and win to null
    dispose();
    surface.setVisible(false);
    win = null;
  }
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 7, 2019, 8:34pm UTC](https://discourse.processing.org/t/set-present-background-colour/6957/2 "2019-01-07T20:34:53Z")

</div>

> [@garrettlynch](#):
>
> I’m trying to set the present background colour of a sketch’s second window

Maybe I’m not understanding what you are trying to do. Is the basic idea that you want to use runSketch to create multiple windows? Is your issue related to this?

> <https://github.com/processing/processing/issues/5255>
>
> From \[this StackOverflow question\](https://stackoverflow.com/questions/46134893/…calling-drawing-commands-externally-in-processing), would we expect this to work?
> 
> \`\`\`java
> SecondApplet sa;
> 
> void setup() {
> String\[\] args = {"TwoFrameTest"};
> sa = new SecondApplet();
> PApplet.runSketch(args, sa);
> }
> 
> void settings() {  
> size(200, 200);
> }
> 
> void draw() {
> background(0);
> sa.ellipse(mouseX, mouseY, 25, 25);
> }   
> 
> void mousePressed() {
> sa.background(255, 0, 0);
> }
> 
> public class SecondApplet extends PApplet {
> 
> public void settings() {
> size(200, 200);
> }
> 
> void draw() {
> }
> }
> \`\`\`
> 
> Here we create an instance of a second sketch, and then call \`runSketch()\` to run it. We then call the \`background()\` and \`ellipse()\` functions on that second sketch. However, the second sketch remains just a gray window. Is this the expected behavior?
> 
> Note that the original question was about the G4P library, which I'm guessing is doing something similar behind the scenes.

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/12319/using-papplet-runsketch-to-create-multiple-windows-in-a-ps3-sketch)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

When I set `background()` to red and green respectively in a simplified version of your sketch, the two windows are red and green. Are you trying to do something different?

```auto
Main win;

public void settings() {
  size(320, 240);
}

void setup() {
  surface.setLocation(20, 30);
  win = new Main();
}

void draw() {
  background(255, 0, 0);
}

class Main extends PApplet
{
  Main() {
    PApplet.runSketch(new String[] {"--present", "--hide-stop", "--display=1", this.getClass().getSimpleName()}, this);
  }

  void settings() {
    size(800, 600);
  }

  void setup() {
    background(0, 255, 0);
  }

  void draw() {
  }

  void exit() {
    dispose();
    surface.setVisible(false);
    win = null;
  }
}

```

---

<div class="post-metadata">

**Author:** ![garrettlynch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/garrettlynch/32/19853_2.png) [@garrettlynch](https://discourse.processing.org/u/garrettlynch)\
**Post date:** [January 17, 2019, 3:14pm UTC](https://discourse.processing.org/t/set-present-background-colour/6957/3 "2019-01-17T15:14:17Z")

</div>

Yes `runSketch()`, no not `background()` - the background colour of the ‘present’ mode (fullscreen) - not the background colour of the sketch but its frame/window, which appears around the sketch. Anyway I’ve done it using JFrame rather than Frame (my third attempt wasn’t quite right). Thanks.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 18, 2019, 2:43am UTC](https://discourse.processing.org/t/set-present-background-colour/6957/4 "2019-01-18T02:43:07Z")

</div>

> [@garrettlynch](#):
>
> I’ve done it using JFrame rather than Frame

Very glad you found a solution that worked for you. Would you be willing to share ii?

---

<div class="post-metadata">

**Author:** ![garrettlynch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/garrettlynch/32/19853_2.png) [@garrettlynch](https://discourse.processing.org/u/garrettlynch)\
**Post date:** [January 18, 2019, 3:07pm UTC](https://discourse.processing.org/t/set-present-background-colour/6957/5 "2019-01-18T15:07:26Z")

</div>

Sure. Import swing’s JFrame like so:

`import javax.swing.JFrame;`

and then in the setup() of the class get the sketch surface’s JFrame so you can set its background like so:

```auto
  void setup()
  {
    //get this sketches JFrame
    PSurfaceAWT surf = (PSurfaceAWT) getSurface();
    PSurfaceAWT.SmoothCanvas canvas = (PSurfaceAWT.SmoothCanvas) surf.getNative();
    JFrame jframe = (JFrame) canvas.getFrame();

    //set frame background colour to dark gray
    jframe.getContentPane().setBackground(new Color(100, 100, 100));
  
    //set background of sketch to black
    background(0, 0, 0);
  }

```
