Trouble with window size when using SecondApplet

First off I am new to processing and GUI in java. I am trying to port a program I made in matlab (I know what you are thinking, but it was due to the fact I had to take a class that used it for my degree, and since it has a super simple GUI maker) to java so I can make an executable from it and distribute it.

Now the issue I am having is for some reason the settings function under the SecondApplet class seems to affect the window that is made using the original setup function. So when the program is ran, the window is reduced to a super small ~100x100 size even if both size calls are size(1280,650), as well the second window now is the correct size. But if the settings function is commented out, the original window is correct, and the new window will be ~100x100.

Goal is to have the button called "Suspension_Setup" open a new window to enter data about the suspension points on the users vehicle, and then save them in a file to be used by the original window to display what I am trying to do.

If anyone can, could I also get an answer on a good way to close the second window, while preserving the original window? I have looked, but the only thing I have seen is using PFrame, and I don't know if I need that yet.

This is super bare bones right now due to the fact I am trying to just make the framework for the interface first, and ran into this issue very quickly.

Code:

import controlP5.*;  // Importing GUI library

ControlP5 cp5;  // Creating a new object for the GUI library

CheckBox checkbox;  // Creating a checkbox object

DropdownList d1, d2;  // Creating a Dropdown menu object

boolean show = false;  // Setting the default state of the second window to false

void setup()
{
  size(1280, 650);
  cp5 = new ControlP5(this);
  checkbox = cp5.addCheckBox("checkBox")
    .setPosition(500, 500)
    .setColorForeground(color(120))
    .setColorActive(color(255))
    .setColorLabel(color(255))
    .setSize(40, 40)
    .setItemsPerRow(3)
    .setSpacingColumn(30)
    .setSpacingRow(20)
    .addItem("0", 0)
    .addItem("50", 50)
    .addItem("100", 100)
    .addItem("150", 150)
    .addItem("200", 200)
    .addItem("255", 255);

  // create a DropdownList
  d1 = cp5.addDropdownList("myList-d1").setPosition(500, 20);

  formatDropdown(d1); // customize the first list

  // create a second DropdownList
  d2 = cp5.addDropdownList("myList-d2").setPosition(500, 80);

  formatDropdown(d2); // customize the second list

  // create a new button with name 'buttonA'
  cp5.addButton("Suspension_Setup")
    .setPosition(0, 0)
    .setSize(80, 30);
}


void formatDropdown(DropdownList ddl)
{
  // a convenience function to customize a DropdownList
  ddl.setSize(200, 200);
  ddl.setBackgroundColor(color(190));
  ddl.setItemHeight(30);
  ddl.setBarHeight(30);
  ddl.setCaptionLabel("dropdown");
  for (int i = 0; i < 40; i++)
  {
    ddl.addItem("item " + i, i);
  }
  //ddl.scroll(0);
  ddl.setColorBackground(color(60));
  ddl.setColorActive(color(255, 128));
  ddl.close();

  // remove items from the pulldown menu  by name
  // d1.removeItem("item "+cnt);

  // add new items to the pulldown menu
  // int n = (int)(random(100000));
  // d1.addItem("item "+n, n);
}

void keyPressed()  // Might be used in future for shortcuts or special features
{

}

// function Suspension_Setup will receive changes from controller with name Suspension_Setup
public void Suspension_Setup(int theValue) {
  println("a button event from Suspension_Setup: "+theValue);
  show = true;
  String[] args = {"TwoFrameTest"};
  SecondApplet sa = new SecondApplet();
  PApplet.runSketch(args, sa);
}

void controlEvent(ControlEvent theEvent)
{
  // DropdownList is of type ControlGroup.
  // A controlEvent will be triggered from inside the ControlGroup class.
  // therefore you need to check the originator of the Event with
  // if (theEvent.isGroup())
  // to avoid an error message thrown by controlP5.

  if (theEvent.isGroup())
  {
    // check if the Event was triggered from a ControlGroup
    println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());
  } else if (theEvent.isController())
  {
    println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());
  }
}

void draw()
{
  background(0);
}


public class SecondApplet extends PApplet
{
  public void settings()
  {
    size(1280, 650);
  }
  public void draw()
  {
    if (show)
    {
      background(255);
      noFill();
      ellipse(100, 50, 10, 10);
    }
    show = false;
  }
}

Thank you for the help!

Answers

  • edited December 2016 Answer ✓

    When we don't have any settings() in our sketch, PDE's pre-processor auto-creates 1 and moves the 1st size() it finds into it.

    However, you've already got 1 settings() inside your other PApplet subclass.
    Therefore the pre-processor won't execute that background operation anymore. :-SS
    In order to fix it, just implement a settings() for your top window sketch as well. *-:)

    Some useful links to help you on your multi-window sketch: :-bd

    1. https://forum.Processing.org/two/discussion/16457/size-method-for-intial-window-not-working-when-more-windows-are-are-added#Item_1

    2. https://forum.processing.org/two/discussions/tagged?Tag=papplet.runsketch()

  • That makes so much sense...that solved that problem. Now onto the next step, how to close a new window.

    Now I read a thread, where you mentioned another thread that you commented on with this code:

    static final void removeExitEvent(final PSurface surf) {
      final java.awt.Window win
        = ((processing.awt.PSurfaceAWT.SmoothCanvas) surf.getNative()).getFrame();
    
      for (final java.awt.event.WindowListener evt : win.getWindowListeners())
        win.removeWindowListener(evt);
    }
    

    Which is supposed to allow the close of the second window without closing the first, but for some reason it has no effect on my program...Do I have to do more than just add this to the program?

  • Well, have you invoked removeExitEvent() within your program anywhere? :-/

  • edited December 2016 Answer ✓

    This code in the second PApplet should work too (if you make SecondApplet a global variable):

      public void exit()
      {
        sa = null;
        dispose();
      }
    

    Edit: also only works if you are not using a PGraphics renderer for the second applet

  • Thanks for the help! I went with colouredmirrorball's code as I understand it better, and it is cleaner. But as a response to GoToLoop, I tried to invoke it but I obviously did it wrong. :P

    For now that is all the help I need, will most likely be back at some point when I run into a weird issue.

Sign In or Register to comment.