Drawing in the wrong window

edited January 2015 in Questions about Code

Hello Processing community!

I am having the following problem:

I have a program that at some point creates a second window.

In that second window I have the usual setup() and draw(), and here's what's funny:

When I include a function that does some drawing (for example a rectangle) in the second window's draw(), that rectangle appears in the main window. Yet when I try rect() straight into the second window draw() it plots in the second window as expected.

Here's some code to illustrate:

DOESN'T WORK:

import javax.swing.JFrame;

PFrame statsWindow;
secondWindow s;

public class secondWindow extends PApplet {
  public void setup() {
    background(255);
  }

  void draw() {
    plotter();
  }
}

void plotter() {
  fill(0);
  rect(10,10,100,100);
}

public class PFrame extends JFrame {
  public PFrame() {
    setBounds(100, 100, 220, 220);
    s = new secondWindow();
    add(s);
    s.init();
    setDefaultCloseOperation(HIDE_ON_CLOSE);
  }
}

WORKS (plots in the second window):

public class secondWindow extends PApplet {
  public void setup() {
    background(255);
  }

  void draw() {
    fill(0);
    rect(10,10,100,100);
  }
}

(duplicate code omitted)

Any ideas why the first code doesn't plot on the second window? How do I do this properly using an auxiliary function? (I don't want to put all my code inside the draw() function)

Regards,

Alex

Comments

  • Running the code you posted only displays a single window for me, and none of your code is triggered. Can you post your code as an MCVE that actually shows two windows?

  • edited January 2015

    Hi @KevinWorkman

    Sorry, I should have been more clear. I can't post my entire program as it is too big, but here's a little example that illustrates the problem:

    import javax.swing.JFrame;
    
    PFrame statsWindow;
    secondWindow s;
    
    void setup() {
      size(300,300);
      frame.setTitle("Main window");
      noLoop();
    }
    
    void draw() {
      background(255);
      fill(0);
      text("a box should NOT appear here, yet it does..", 0, height/2+20);
      text("click mouse to show second window", 0, height/2);
    }
    
    void mouseClicked() {
      statsWindow = new PFrame();
    }
    
    
    public class secondWindow extends PApplet {
      public void setup() {
        noLoop();
      }
    
      void draw() {
        background(255);
        fill(0);
        text("The box should appear in this window.. right? but it doesn't!!", 0, height/2,width,height/2);
        plotter();
      }
    }
    
    void plotter() {
      fill(0);
      rect(10, 10, 100, 100);
    }
    
    public class PFrame extends JFrame {
      public PFrame() {
        setBounds(100, 100, 300, 300);
        s = new secondWindow();
        add(s);
        s.init();
        setVisible(true);
        setTitle("Second window");
      }
    }
    

    Hope that helps you understand what I'm trying to say.

    Regards, Alex

  • Try moving the plotter method indide the SecondWindow class

    BTW by conventil the name of a class starts with an uppercase letter.

    Alternatively you could use the G4P library as it has good support fror multple windows. ;)

  • edited January 2015

    Hi @quark

    I know this, thank you. The whole deal for me was to have the class that controls the second window in a separate file, trying to make it as less messy as possible, but I guess I'll have to do with having it inside the same class.

    Thank you all.

    EDIT: Forgot answering about G4P, thanks. I have been avoiding using external libraries, but I might give it a try too.

  • @alejoar::

    or like this??? (it seems more clear for me without "plotter()", but!)

        import javax.swing.*;
        import java.awt.*;
        Graphics gr;
        JPanel pan;
        PFrame statsWindow;
    
    
    
        void setup() {
          size(400, 400);
          background(255);
    
        };
    
        void draw() {
    
        background(255);
    
    
        fill(0);
        text("not any rect!", 100,200);
        if(mousePressed){
          new PFrame();
        }
        };
    
    
        class MyPanel extends JPanel {
    
            public MyPanel() {
    
                setBackground(Color.green);
            }
    
            public void paintComponent(Graphics g) {
                super.paintComponent(g);       
    
                // Draw Text
                g.setColor(Color.red);
                g.drawString("Works!",100,200);
               //Draw Rect
                 g.setColor(Color.black);
                g.fillRect(20, 20, 80, 80);
            }  
        };
    
    
        public class PFrame extends JFrame {
          public PFrame() {
            JFrame statsWindow = new JFrame("2° Window");
            statsWindow.setBounds(100, 100, 520, 520);
            //statsWindow.setBackground(Color.ORANGE);//Définition de sa couleur de fond mais on peut le faire dans la classe paint
           pan = new MyPanel();
           statsWindow.add(pan);
            statsWindow.setVisible( true);
            statsWindow.setDefaultCloseOperation(HIDE_ON_CLOSE);
            // frame.pack();
          }
    
        }
    
  • Thank you @akenaton

    Although your example is exactly what @quark suggested (moving the method into the second window class) I appreciate your answer.

  • thanks! -- i have tried to make classes independant, as you ask for...

  • edited January 2015

    You might try this.

      // inside class
      void draw() {
          background(255);
          plotter(this);  
      } // end of draw
    
    } // end of class
    
    void plotter(PApplet app) {
      app.fill(0);
      app.rect(10, 10, 100, 100);
    }
    
  • @quark you just nailed it!

    Thank you very much!

Sign In or Register to comment.