Set present background colour

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.

//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);
}
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;
  }
}
1 Like

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?

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?

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;
  }
}

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.

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

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:

  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);
  }
2 Likes