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 › Help! Can Processing measure input dimensions
Page Index Toggle Pages: 1
Help! Can Processing measure input dimensions? (Read 350 times)
Help! Can Processing measure input dimensions?
Oct 24th, 2009, 1:28pm
 
I am trying to center an image on the artboard (or whatever Processing calls it). Instead of calculating the equal x,y margins every time I have an image to place, is there a way to simply have Processing place it centered on screen? I also have this function in consideration, but I need to know if there is a way to access the dimensions of the loaded image.

int xSize = 500;
int ySize = 300;
int xImage = ??; // this is where the mystery function will have it's
int yImage = ??; // values stored as variables for the formula
int x1, x2, newX, y1, y2, newY;

x1 = xSize/2;
y1 = ySize/2;
x2 = xImage/2;
y2 = yImage/2;
newX = x1 - x2;
newY = y1 - y2;


newX and newY will be plugged into the image() function for it's origin coordinates, and the galaxy will be at peace, or at least center-aligned.
Re: Help! Can Processing measure input dimensions?
Reply #1 - Oct 24th, 2009, 1:35pm
 
The PImage class has two fields, width and height:
PImage img = ...;
int xImage = img.width;
int yImage = img.height;


Otherwise, try this:
imageMode(CENTER);
image(img, width/2, height/2);
Re: Help! Can Processing measure input dimensions?
Reply #2 - Oct 24th, 2009, 2:01pm
 
[Edit] I didn't see that you had replied, Christian. I got your clue from someone else before I read the forum again. Thanks for the help!

PFont myFont;
import processing.pdf.*;
PImage logo;
int newX, newY; //store coords from the assignments below
void setup()
{
 size(500,300);
 myFont = createFont("Universe",48);
 beginRecord(PDF,"helloworld.pdf");
 background(255);
 logo = loadImage("shoeLogo.png");
 newX = width/2 - (logo.width)/2;    // used to find equal x/y margins
 newY = height/2 - (logo.height)/2;  // to place the image center-aligned
 image(logo,newX,newY);
 
 endRecord();
}


Page Index Toggle Pages: 1