# Thread: Help with getWidth() method!!!

1. Member
Join Date
Mar 2013
Posts
2
Rep Power
0

## Help with getWidth() method!!!

Hi,
I am trying to find my x,y coordintes with getWidth() and getHieght() methods.
My getWidth() is giving 200 as the width.
Is this the actual width of the screen?
I need to build a pyramid with 14 brinks at the botton and each brick is 30 pixels wide.The pyramid should be centered .
To find x coordinate I used the formula
x=( (getWidth() - (number of bricks * width of brick))/2
this gives me -110. I dont understand!!!

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,655
Rep Power
9

## Re: Help with getWidth() method!!!

The getWidth() is returning the width of your panel or frame, not the screen. If you post your code between [code][/code] tags it will be easier to help you.

Regards,
Jim

3. Member
Join Date
Mar 2013
Posts
2
Rep Power
0

## Re: Help with getWidth() method!!!

Thanks for the quick reply...How do I find center of the screen?

I am trying to create a pyramid...can you guide me please.

This is the code I came up with

/** Number of bricks in the base of the pyramid */
private static int BRICKS_IN_BASE = 30;
/**Number of rows in the pyramid*/
private static final int NUMBER_OF_ROWS = 14;

public void run() {

/** formula for calcualting x coordintes (x=getWidth()-figureWidth))*/

double x = (getWidth() - (BRICK_WIDTH *BRICKS_IN_BASE)) /2;
println( "first pixel = " + x);
double y =getHeight() ;
println("second =" + y);
for(int j=0;j<=NUMBER_OF_ROWS;j++){
for(int i =0;i<=BRICKS_IN_BASE;i++){
x= i*BRICK_WIDTH;
y=j*BRICK_HEIGHT;
GRect rect = new GRect(x,y,BRICK_WIDTH,BRICK_HEIGHT);

}
BRICKS_IN_BASE = BRICKS_IN_BASE - 1;
}

}

}

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,655
Rep Power
9

## Re: Help with getWidth() method!!!

To find the center of the screen, use the Toolkit.getDefaultToolkit().getScreenSize() method.

The center coordinate would be width/2, height/2,

And please remember to use code tags (see earlier post).

Regards,
Jim
Last edited by jim829; 03-10-2013 at 06:07 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•