Note that there are some explanatory texts on larger screens.

plurals
  1. POJava applet ignoring getWidth()
    primarykey
    data
    text
    <p>I'm trying to build a pyramid in Java using GRect. I use getWidth() to position the pyramid in the center of the canvas but it tends to trip the left part of pyramid.</p> <p>I'm learning programming through <a href="http://see.stanford.edu/see/courseinfo.aspx?coll=824a47e1-135f-4508-a5aa-866adcae1111" rel="nofollow">CS106A</a> and using Java 1.6 so tried to get some other peoples code and they too showed the same bug. I tried to draw a simple rect and it works fine.</p> <p>Is it some issue with Java applet as it tends to ignore getWidth() value at all. I used println(); to get getWidth() value and it works.</p> <pre><code> import acm.graphics.GRect; import acm.program.GraphicsProgram; public class Pyramid extends GraphicsProgram { /** * eclipse generated SVUID */ private static final long serialVersionUID = 5703830460071233096L; /** Width of each brick in pixels */ private static final int BRICK_WIDTH = 30; /** Width of each brick in pixels */ private static final int BRICK_HEIGHT = 12; /** Number of bricks in the base of the pyramid */ private static final int BRICKS_IN_BASE = 14; public void run() { //centering the pyramid int _canvasWidth = getWidth(); //canvas width //println(_canvasWidth); //to check if getWidth returns a value int _brickSpace = BRICKS_IN_BASE * BRICK_WIDTH; //space consumed by the base row int _freeSpace = _canvasWidth - _brickSpace; //empty base level space int _xOffset = _freeSpace / 2; //x for left most brick of base row for(int row=0; row&lt;BRICKS_IN_BASE; row++){ //the row number int _rowTab = (BRICK_WIDTH/2) * row; //indentaion for each row int _brickInRow = BRICKS_IN_BASE - row; //bricks as per row for(int _brickNumber=0; _brickNumber&lt;_brickInRow; _brickNumber++){ //placing bricks till _brickInRow int x = (_xOffset + _rowTab) + (_brickNumber * BRICK_WIDTH); int y = (BRICK_HEIGHT * (BRICKS_IN_BASE - row)); //y as per row num GRect _brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT); //creating brick instance add(_brick); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload