1

there is this JFrame containg a JPanel containing a processing sketch. The code looks something like this:

import javax.swing.JFrame;
import javax.swing.JPanel;
import processing.core.PApplet;

public class TestCase {
    public static void main(String[] args) {
        ProcessingFrame pFrame = new ProcessingFrame();
        pFrame.setVisible(true);
    }
}

@SuppressWarnings("serial")
public class ProcessingFrame extends JFrame{    
    public ProcessingFrame(){
        this.setSize(600, 400);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        ProcessingSketch pSketch = new ProcessingSketch();
        panel.add(pSketch);
        this.add(panel);
        pSketch.init();
        this.setVisible(true);
    }
}

public class ProcessingSketch extends PApplet{
    public ProcessingSketch(){
        //...
    }   
    public void setup(){
        size(600, 400);
        background(200);
    }   
    public void draw(){
        rect(250,100,100,200);
    }
}

Too often (about 50%) the panel and/or sketch is not rendered to the appropriate location. Sometimes they are just about half of the frame size off to the right, sometimes I see just the bottom of the sketch in the frame.

After I resize the frame with the mouse, everything looks fine. I would like to set the JFrame to undecorated, but there is no way to resize the windows to put everything into place.

This is what I tried to solve that:

Any other ideas?

Community
  • 1
  • 1
7morgen
  • 23
  • 5
  • 1
    `The code looks something like this:` - doesn't help us. Post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Apr 01 '16 at 16:32
  • thank you @camickr, I tried to create a test case. – 7morgen Apr 01 '16 at 17:05
  • Sorry, I can't help I don't know what the 3rd party package does. Maybe it is affecting the layout. A SSCCE should be based on JDK classes. – camickr Apr 01 '16 at 17:40
  • @camickr This is Processing, which is a little bit more complicated than a Java library. It's a language of its own. You can mix it with Java, so a lot of Processing questions get the Java tag too. But I think this is as close to a [mcve] we're going to be able to get. – Kevin Workman Apr 01 '16 at 18:52

1 Answers1

1

This sounds like a threading issue. Processing uses its own animation thread, so I'm not really surprised you're having issues, and I'm not really surprised they're sporadic. That's the nature of threads.

In fact, the latest version of Processing removed the ability to add a Processing sketch to a Swing application like this. If you really want to fix this problem, you should update to the latest version of Processing and use it as a Java library. That's more work, but it's more "correct" than continuing with this old way.

But since I know that's not what you want to hear, the first thing I would try is calling the JFrame.revalidate() function. You have to do this after the Processing sketch has been initialized, which might be tricky with the aforementioned threading issues. You might have to put that in a delay of some kind, or maybe do it from the setup() function.

If that doesn't work, you could try programatically resizing your JFrame after some delay. That's pretty hackish, but again, the "correct" way to proceed is to update your Processing and stop embedding it the old way.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Thank you very much! The `JFrame.revaildate()` works just fine. I put it als last command into the `setup()`. As a matter of fact the change to processing 3 is one of the biggest points on my wish-todo list. – 7morgen Apr 04 '16 at 11:18