Results 1 to 4 of 4
Thread: Help with getWidth() method!!!
- 03-09-2013, 10:06 PM #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!!!
- 03-09-2013, 10:25 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 679
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-10-2013, 04:31 AM #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);
add(rect);
}
BRICKS_IN_BASE = BRICKS_IN_BASE - 1;
}
}
}
- 03-10-2013, 05:00 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 679
- Rep Power
- 1
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,
JimLast edited by jim829; 03-10-2013 at 05:07 AM.
The Java™ Tutorial
YAT -- Yet Another Typo
Similar Threads
-
getWidth and getHeight of JPanel
By Kareem Mesbah in forum New To JavaReplies: 3Last Post: 10-10-2012, 03:47 AM -
Help with getWidth andgetHeight please.
By Neomus in forum New To JavaReplies: 8Last Post: 11-25-2010, 12:33 PM -
getWidth(),GetHeight() using POI
By Zeus in forum New To JavaReplies: 0Last Post: 08-02-2010, 12:28 PM -
getWidth && getHeight
By courteous in forum New To JavaReplies: 1Last Post: 01-18-2009, 10:39 PM -
Img.getWidth(null); gives -1 why?
By willemjav in forum New To JavaReplies: 0Last Post: 02-15-2008, 02:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks