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 › Resize display window proportionally
Page Index Toggle Pages: 1
Resize display window proportionally (Read 704 times)
Resize display window proportionally
Feb 14th, 2010, 5:29pm
 
So I'm trying to get the scene to (proportionally) re-size when the display window is re-sized. Essentially what I have is a program that has two methods that draw a simple image and currently my window is just a set width and height but I want it to be able to be re-sized and the image just gets redrawn and fits inside the newly re-sized window.

I've looked through multiple topics on this but none seem to be very helpful for my issue. I've tried to implement what some of those other topics were talking about but haven't had much luck.

Below is my code for this:

Code:

void setup()
{
   size(500,500);
   frame.setResizable(true);
   noLoop();
   background(0,255,0);
}

//draw person method
void drawPerson()
{
 //create head
 ellipse(250,250,150,150);
 
 //left eye
 ellipse(220,220,20,20);
 ellipse(220,220,8,8);

 //right eye
 ellipse(280,220,20,20);
 ellipse(280,220,8,8);
 
 //mouth
 line(230,285,270,285);
 line(215,270,230,285);
 line(270,285,285,270);
 
 //body
 line(250,324,250,425);
 
 //arms
 line(200,345,300,345);
 
 //legs
 line(250,425,230,470);
 line(250,425,270,470);
}

void drawSun()
{
 ellipse(500,0,160,160);
 line(340,30,420,10);
 line(355,120,450,60);
 line(480,76,470,150);
}

void draw()
{
 drawSun();
 drawPerson();
}

public void mousePressed()
{
 redraw();
}


Any help would be appreciate, thanks!
Re: Resize display window proportionally
Reply #1 - Feb 14th, 2010, 7:32pm
 
What, like this?

Quote:
void setup()
{
  size(500,500);
  frame.setResizable(true);
  //    noLoop();
  background(0,255,0);
}

//draw person method
void drawPerson()
{
  //create head
  ellipse(250,250,150,150);

  //left eye
  ellipse(220,220,20,20);
  ellipse(220,220,8,8);

  //right eye
  ellipse(280,220,20,20);
  ellipse(280,220,8,8);

  //mouth
  line(230,285,270,285);
  line(215,270,230,285);
  line(270,285,285,270);

  //body
  line(250,324,250,425);

  //arms
  line(200,345,300,345);

  //legs
  line(250,425,230,470);
  line(250,425,270,470);
}

void drawSun()
{
  ellipse(500,0,160,160);
  line(340,30,420,10);
  line(355,120,450,60);
  line(480,76,470,150);
}

void draw()
{
  pushMatrix();
  scale( width/500.0, height/500.0);
  drawSun();
  drawPerson();
  popMatrix();
}

//public void mousePressed()
//{
//  redraw();
//}

Re: Resize display window proportionally
Reply #2 - Feb 15th, 2010, 3:10pm
 
That is exactly what I needed! Thank you very much! I probably never would have figured it out, because I had never heard of pushMatrix() and popMatrix(). I went and read up on it after looking at the code. Those some very useful little functions.
Re: Resize display window proportionally
Reply #3 - Feb 15th, 2010, 3:55pm
 
You don't actually need calls to pushMatrix() and popMatrix() in this example - I just throw them in so that if you wanted to draws something *else* in draw() after your guy and the sun, you could have the original positioning to work with again.

The real magic here is the call to scale()!
Page Index Toggle Pages: 1