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.
Page Index Toggle Pages: 1
new window (Read 1269 times)
new window
Dec 30th, 2006, 9:14pm
 
Hi,

I am new to the 'window' interface in Java, but I managed to make a second window show up from processing - however, when I call 'rect(1,2,3,4);' the rectangle appears on the master window instead of the new one...

How would I go about drawing onto the new Window?

Cheers,
Crispin
Re: new window
Reply #1 - Dec 30th, 2006, 11:08pm
 
While this is not the best example, this sets up a new window and draws in it using the processing drawing commands.

Quote:


static Frame w;
Graphics grap;

void setup(){
 size(320,240);
 setupNew();
 
 frameRate(5);
 this.frame.setTitle("I'm the old window!");
}

void setupNew(){
 w=new Frame("Hiya! I'm new!");
 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

 w.setBounds(dim.width/2-160,dim.height/2-120,320,240);
 w.show();
 grap= w.getGraphics();
 
}

void draw()
{
 background(0,random(255),0);
 
 try {
   PGraphics stuff;
   stuff = createGraphics(320, 240, JAVA2D);
   
   //draw your stuff here
   stuff.beginDraw();
     stuff.background(random(255),0,0);
     stuff.stroke(255);
     stuff.line(40, 40, 50,50);
   stuff.endDraw();
   
   BufferedImage img = PG2BI(stuff);
   grap.drawImage(img, 0, 0, this);
 }
 catch(Exception e) {
   e.printStackTrace();
 }
 
}

BufferedImage PG2BI(PGraphics i) {
BufferedImage img=new BufferedImage(i.width, i.height, BufferedImage.TYPE_INT_RGB);
i.loadPixels();
img.setRGB(0, 0, i.width, i.height, i.pixels, 0, i.width);
return img;
}



Re: new window
Reply #2 - Dec 31st, 2006, 5:20pm
 
Thanks for the reply, Joseph.  I tried the code out and here are the results:

The example works fine - but unfortunately, it causes my processor to work about 50% harder than without the extra window.
Also, how would I go about calling the other events such as 'void mouseReleased()'.

Sorry for all of the questions!

Thanks again,
Crispin
Re: new window
Reply #3 - Dec 31st, 2006, 6:42pm
 
Here's the code. It's a bit of a Java-y mess, but here it is:
Code:
static Frame w; 
Graphics grap;

void setup(){
size(320,240);
setupNew();

frameRate(5);
this.frame.setTitle("I'm the old window!");
}

void setupNew(){
w=new Frame("Hiya! I'm new!");

//see http://java.sun.com/docs/books/tutorial/post1.0/ui/mouselistener.html
//you could probably pass this all to the regular processing stuff with super.processMouseEvent( e );
MouseListener Win2MouseListner = new MouseListener() {
public void mouseEntered(MouseEvent e) {
}
public void mouseClicked(MouseEvent e){
println("Click! ");
}
public void mouseReleased(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}


};

//see http://java.sun.com/docs/books/tutorial/post1.0/ui/mousemotionlistener.html
MouseMotionListener Win2MouseMovementListner = new MouseMotionListener(){
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
println(e.getX()+","+e.getY());
}
};

w.addMouseListener(Win2MouseListner);
w.addMouseMotionListener(Win2MouseMovementListner);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

w.setBounds(dim.width/2-160,dim.height/2-120,320,240);
w.show();
grap= w.getGraphics();

}

void draw()
{
background(0,random(255),0);

try {
PGraphics stuff;
stuff = createGraphics(320, 240, JAVA2D);

//draw your stuff here
stuff.beginDraw();
stuff.background(random(255),0,0);
stuff.stroke(255);
stuff.line(40, 40, 50,50);
stuff.endDraw();

BufferedImage img = PG2BI(stuff);
grap.drawImage(img, 0, 0, this);
}
catch(Exception e) {
e.printStackTrace();
}

}

BufferedImage PG2BI(PGraphics i) {
BufferedImage img=new BufferedImage(i.width, i.height, BufferedImage.TYPE_INT_RGB);
i.loadPixels();
img.setRGB(0, 0, i.width, i.height, i.pixels, 0, i.width);
return img;
}
Re: new window
Reply #4 - Dec 31st, 2006, 8:35pm
 
or, you could work your way along the recommendations in the source:

http://dev.processing.org/source/index.cgi/trunk/processing/core/src/processing/core/PApplet.java?view=markup

which will basically give you a second PApplet in the other frame:

Code:

ExampleFrame ef;
Embedded pa;

void setup() {
size(400, 400);

pa = new Embedded();
ef = new ExampleFrame( pa );
ef.pack();
ef.show();
}

void draw() {
background( 124 );
line( random(width), random(height), random(width), random(height) );
pa.setLineColor( (int)random( 255 ) );
}

class ExampleFrame extends Frame {

ExampleFrame( PApplet _p ) {
super("Embedded PApplet");
setLayout(new BorderLayout());
add(_p, BorderLayout.CENTER);
_p.init();
_p.start();
_p.loop();
}
}

class Embedded
extends PApplet
{

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

void draw() {
background( 124 );
line( random(width), random(height), random(width), random(height) );
}

void setLineColor ( int _c )
{
stroke( _c );
}
}


i have a question though, what are you trying to do? why would you need another window?

F
Re: new window
Reply #5 - Dec 31st, 2006, 11:23pm
 
My way would probably be easier to move data between windows, and your way is probably easier for a programmer who hasn't used much Java. But the java docs rein supreme. Look up Frame to see what I'm doing.
Re: new window
Reply #6 - Jan 3rd, 2007, 5:46am
 
Joseph and fjen,

Thanks for the code examples - in answer to your question, I am designing a GUI and I thought an external window would allow a bit more space.

Thanks for the help!
Crispin
Page Index Toggle Pages: 1