Results 1 to 4 of 4
  1. #1
    SamanthaK is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    SamanthaK is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default 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;
    }

    }

    }

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default 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 05:07 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. getWidth and getHeight of JPanel
    By Kareem Mesbah in forum New To Java
    Replies: 3
    Last Post: 10-10-2012, 03:47 AM
  2. Help with getWidth andgetHeight please.
    By Neomus in forum New To Java
    Replies: 8
    Last Post: 11-25-2010, 12:33 PM
  3. getWidth(),GetHeight() using POI
    By Zeus in forum New To Java
    Replies: 0
    Last Post: 08-02-2010, 12:28 PM
  4. getWidth && getHeight
    By courteous in forum New To Java
    Replies: 1
    Last Post: 01-18-2009, 10:39 PM
  5. Img.getWidth(null); gives -1 why?
    By willemjav in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 02:06 PM

Posting Permissions

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