We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Window possition
Page Index Toggle Pages: 1
Window possition (Read 3270 times)
Window possition
May 26th, 2010, 12:26am
 
Hi
I would like to know if it is possible to set/get window position on the screen.
I can't find function for this on Reference page.

Regards
Re: Window possition
Reply #1 - May 26th, 2010, 1:05am
 
you have to search in the java reference.

but here is a processing example, that should answer your question

Code:
import java.awt.Frame;
import java.awt.Color;

BouncingWindow w[];

void setup()
{
w = new BouncingWindow[8];
for (int i = 0; i < w.length; i++)
w[i] = new BouncingWindow();
}

void draw()
{
frame.setVisible(false);
for (int i = 0; i < w.length; i++)
w[i].draw();
}


class BouncingWindow
{
Frame frame;

final static int SIZE = 20;
float x, y;
float vx, vy;

BouncingWindow()
{
x = random(0, screen.width - SIZE);
y = random(0, screen.height - SIZE);
vx = random(-20, 20);
vy = random(-20, 20);

frame = new Frame();
frame.setSize(SIZE, SIZE);
frame.setLocation(floor(x), floor(y));
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setBackground(new Color(random(1), random(1), 1));
frame.setVisible(true);
}

void draw()
{
x += vx;
y += vy;

if (x < 0) { x = 0; vx *= -1; }
if (x > screen.width - SIZE) { x = screen.width - SIZE; vx *= -1; }
if (y < 0) { y = 0; vy *= -1; }
if (y > screen.height - SIZE) { y = screen.height - SIZE; vy *= -1; }

frame.setLocation(floor(x), floor(y));
}
}
Re: Window possition
Reply #2 - May 26th, 2010, 10:42am
 
Actually, the import statement isn't necessary (though it seems to me like it should be, but whatever.)  You can just fool around with the main sketch's frame directly.

This sketch moves the window around in response to the W/A/S/D keys.
Code:
int stepSize = 10;

void setup() {
size(200, 200);
}

void draw() {
background(0);
}

void keyPressed() {
int x = frame.getX();
int y = frame.getY();

if (key=='a')
x -= stepSize;
else if (key=='d')
x += stepSize;
else if (key=='w')
y -= stepSize;
else if (key=='s')
y += stepSize;

frame.setLocation(x, y);
}
Re: Window possition
Reply #3 - May 26th, 2010, 10:11pm
 
Smitty wrote on May 26th, 2010, 10:42am:
Actually, the import statement isn't necessary

It is necessary in Cedric's code because it is for Processing 1.1 and he creates new frames.
It is not necessary in your code because you just act on the existing frame.
Re: Window possition
Reply #4 - May 27th, 2010, 4:06am
 
Thank you guys.
This is exactly what I need. Cheesy

Best regards
Re: Window possition
Reply #5 - May 27th, 2010, 7:20am
 
PhiLho  wrote on May 26th, 2010, 10:11pm:
Smitty wrote on May 26th, 2010, 10:42am:
Actually, the import statement isn't necessary

It is necessary in Cedric's code because it is for Processing 1.1 and he creates new frames.
It is not necessary in your code because you just act on the existing frame.

I was curious about this (I'm still a Java noob) so I tried it, and I found that Cedric's code works in 1.1 even if I comment out the java.awt.Frame import.

I still don't understand why... maybe the Processing pre-compiler always imports java.awt.Frame
Re: Window possition
Reply #6 - May 27th, 2010, 7:27am
 
The PApplet class has these imports
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
import java.util.zip.*;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;

import processing.core.PShape;


this just about covers everything you might want to use from Java.
Smiley
Re: Window possition
Reply #7 - May 27th, 2010, 7:51am
 
If you make a one line sketch, and see the generated Java file, you can see the default imports:
Code:
import processing.core.*; 
import processing.xml.*;

import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;

Indeed, I was wrong, I supposed all AWT was removed but there is still some bases remaining, so sorry for the noise! Smiley

For the record, this list comes from the preproc.imports.list property from preferences.txt file:
Quote:
preproc.imports.list=java.applet.*,java.awt.Dimension,java.awt.Frame,java.awt.ev
ent.MouseEvent,java.awt.event.KeyEvent,java.awt.event.FocusEvent,java.awt.Image,
java.io.*,java.net.*,java.text.*,java.util.*,java.util.zip.*,java.util.regex.*

If you frequently do an import, you can add it there...
Re: Window possition
Reply #8 - May 27th, 2010, 7:59am
 
Quark wrote on May 27th, 2010, 7:27am:
The PApplet class has these imports

this just about covers everything you might want to use from Java.
Smiley

Do import statements carry through to subclasses  I was under the impression that an import was simply a compiler/coding convenience to prevent you from having to type out full package pathnames.  

For example, if you removed "import java.awt.Frame;" from the PApplet source code and replaced every instance of "Frame" with "java.awt.Frame", shouldn't it still compile just the same  And then if I'm later writing a subclass of PApplet, shouldn't the compiler not care whether the original PApplet used an import or not

Just to confuse myself even further, I tried both my sketch and Cedric's in Eclipse--and it turns out that Cedric's sketch does require the import statement and mine still doesn't.  Does Eclipse (or the compiler or whatever) automatically import java.awt.Frame because I'm subclassing PApplet, which has a field of the Frame type
Re: Window possition
Reply #9 - May 27th, 2010, 8:01am
 
PhiLho  wrote on May 27th, 2010, 7:51am:
If you make a one line sketch, and see the generated Java file, you can see the default imports:

(snip)

Ah, okay, well that does explain it as far as the PDE goes.  I still don't entirely get why one sketch requires the import in Eclipse and the other doesn't, but I'm happy just letting that be a minor mystery of Java for now. Wink

Thanks for helping me clear that up!
Re: Window possition
Reply #10 - May 27th, 2010, 8:18am
 
Quark shows the list of imports made by PApplet.java, which is a useful set.
These imports doesn't propagate into your own sketches, PDE always includes my list. If you write your code in Eclipse, you have to do these imports yourself (although it helps you there).
The frame variable you use was defined in PApplet, and you can use it without declaring the import because Java already knows its type. As you guessed, these imports are useful only at compile time, the classes are fully typed in the jars.
And yes, you can do no import if you give the full path when you use the class name. Sometime we do that when a class name is used only once in the code. Full path can be useful too to distinguish identical class names, for example java.awt.List from java.util.List or the numerous Element classes found in various libraries...
Re: Window possition
Reply #11 - May 27th, 2010, 8:47am
 
Okay, that makes sense now.  Thanks!
Re: Window possition
Reply #12 - May 27th, 2010, 12:12pm
 
Hello!

I'm new in this... First of all, thanks for the patienth  Smiley
how can i do to put the window always in the top corner (0,0) ?? I need it because i'm using a big window, and sometimes appears out of the screen...
thnks

J
Re: Window possition
Reply #13 - May 27th, 2010, 12:18pm
 
See if this works for you:

Code:

void draw() {
if (frameCount==1)
getParent().setLocation(0,0);

// Rest of draw() code goes here.
}
Re: Window possition
Reply #14 - May 27th, 2010, 12:26pm
 
Oks, thks!
I've try this and works too  Smiley


void draw() {

     frame.setLocation(0, 0);
}

Your solution is better because alloe you to change later the position.
Thanks
Page Index Toggle Pages: 1