Window Location

edited April 2014 in Questions about Code

frame.setLocation(333, 444);

I put this line in draw() but gave error as "Exception in thread "Animation Thread" java.lang.NullPointerException"

So?

Tagged:

Answers

  • It works for me! :)]

  • Why do you put this in draw(), anyway? No need to repeat this over and over.

  • So say me where I must put it in? Do you think that in setup()?

  • edited April 2014

    Placing setLocation() in setup() won`t work b/c the canvas is invisible then.
    And only after setup() has finished, Processing takes care of its latest adjustments.
    And that includes setLocation(), overriding ours! :O

  • edited April 2014

    In order to avoid redundant repeating calls to setLocation() within draw(),
    we can make a thread("") function for the sole purpose to wait & reposition the canvas after it isVisible()!

    /**
     * Reposition Canvas (v1.01)
     * by GoToLoop (2014/Apr)
     *
     * forum.processing.org/two/discussion/4712/window-location
     */
    
    static final int WINDOW_X = 300, WINDOW_Y = 400;
    static final int FPS = 60, FREQ = (1<<5) - 1;
    
    void setup() {
      size(800, 600, JAVA2D);
      frameRate(FPS);
      smooth(4);
    
      thread("repositionCanvas");
    }
    
    void draw() {
      //if (frameCount % FREQ == 0)
      if ((frameCount & FREQ) == 0)
        background((color) random(#000000));
    }
    
    void repositionCanvas() {
      while (!frame.isVisible())  delay(2);
      frame.setLocation(WINDOW_X, WINDOW_Y);
    }
    
  • That code and also frame.setLocation(int, int) work in processing. But I try to build my program in eclipse. This code does not work too in eclipse (like also frame.setLocation(int, int) in draw()).

  • Just maybe you need to import java.awt.Window; or something like that?
    Processing auto-imports a bunch of Java's standard libraries!

    http://docs.oracle.com/javase/8/docs/api/java/awt/Window.html#setLocation-int-int-

  •     import processing.core.*;
        import java.awt.*;
        import java.applet.*;
    
        public class WindowLocation extends PApplet {
    
            public void setup() {
              size(800, 600);
            }
    
            public void draw() {
              background(random(255), random(255), random(255));
              frame.setLocation(500, 300);
            }
        }
    

    That does not work in eclipse. It means by processing code, it does not seem possible to move it. So I have to focus on classical ways...

  • edited May 2014

    I only know Processing's IDE for now, sorry!
    But I wonder where's the main() function in your code? Or perhaps Eclipse takes care of that too? X_X

    In case it doesn't, here's another more advanced attempt of mine:

    static final void main(String[] args) {
      final String sketch = Thread.currentThread()
        .getStackTrace()[1].getClassName();
    
      final String[] newArgs = append(args, "--location=500,300");
    
      PApplet.main(append(newArgs, sketch));
    }
    
  • I forgot about the need to be in draw() to call this method. But you can call it only once by using a boolean or doing this only at frameCount == 2, for example.

Sign In or Register to comment.