Please help with passing data to a method, desperate!!!

I just started learning Java and ran into a snag. My goal is to display a pyramid and secondary to that have each row be a different color. I can get it to display the pyramid and in one color but getting stuck on the rest. My idea was to have one method called drawPyramid call another called drawRow multiple times. What I need is to pass an int telling it what row it is on and 3 other ints to be used as an rgb value for color. It has to be rbg values since I have 15 rows and as far as I know there are only 13 named constants for colors. This is what I have so far, I know it is something really simple, just not seeing it. Any recommendations would be much appreciated.

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class Pyramid extends GraphicsProgram {

public void run()
{
drawPyramid();
}//end run

/**
This method draws the pyramid
*/
public void drawPyramid()
{
double brickWidth = (double) getWidth() / BRICKS_IN_BASE;
double brickHeight = (double) getHeight() / BRICKS_IN_BASE / 2;
drawRow(int 0 int 128 int 128 int 128);
}//end drawPyramid

/**
This method draws a row
*/
public double drawRow(int i int r int g int b)
{
double initialX = i * brickWidth / 2;
for (int j = 0; j < (BRICKS_IN_BASE - i); j++){
double x = initialX + (j * brickWidth);
double y = brickHeight * (BRICKS_IN_BASE - i);
GRect bk = new GRect(x, y, brickWidth, brickHeight);
bk.setFilled(true); //sets rect to filled
bk.setFillColor(new Color(r,g,b)); //sets color
add(bk);
}//end for
}//end drawRow

/** Private constants */
//private static final double BRICK_HEIGHT = (double) getHeight() / BRICKS_IN_BASE;
/** The number of bricks used in the base */
private static final int BRICKS_IN_BASE = 15;

}//end class

Comments

  • : I just started learning Java and ran into a snag. My goal is to
    : display a pyramid and secondary to that have each row be a different
    : color. I can get it to display the pyramid and in one color but
    : getting stuck on the rest. My idea was to have one method called
    : drawPyramid call another called drawRow multiple times. What I need
    : is to pass an int telling it what row it is on and 3 other ints to
    : be used as an rgb value for color. It has to be rbg values since I
    : have 15 rows and as far as I know there are only 13 named constants
    : for colors. This is what I have so far, I know it is something
    : really simple, just not seeing it. Any recommendations would be
    : much appreciated.
    :
    : import acm.graphics.*;
    : import acm.program.*;
    : import java.awt.*;
    :
    : public class Pyramid extends GraphicsProgram {
    :
    : public void run()
    : {
    : drawPyramid();
    : }//end run
    :
    : /**
    : This method draws the pyramid
    : */
    : public void drawPyramid()
    : {
    : double brickWidth = (double) getWidth() / BRICKS_IN_BASE;
    : double brickHeight = (double) getHeight() / BRICKS_IN_BASE / 2;
    : drawRow(0,128,128,128);
    : }//end drawPyramid
    :
    : /**
    : This method draws a row
    : */
    : public double drawRow(int i, int r, int g, int b)
    : {
    : double initialX = i * brickWidth / 2;
    : for (int j = 0; j < (BRICKS_IN_BASE - i); j++){
    : double x = initialX + (j * brickWidth);
    : double y = brickHeight * (BRICKS_IN_BASE - i);
    : GRect bk = new GRect(x, y, brickWidth, brickHeight);
    : bk.setFilled(true); //sets rect to filled
    : bk.setFillColor(new Color(r,g,b)); //sets color
    : add(bk);
    : }//end for
    : }//end drawRow
    :
    : /** Private constants */
    : //private static final double BRICK_HEIGHT = (double) getHeight() /
    : BRICKS_IN_BASE;
    : /** The number of bricks used in the base */
    : private static final int BRICKS_IN_BASE = 15;
    :
    : }//end class
    :

    Hai, add comma as seperator after each argument declaration in the function/method paranthesis whenever you are calling a function or declaring function. You don't have to specify argument type when you are calling a function. Try above given code.
    Okeyyyyyy...! Wish you good programming days....!

  • If you needs any kinds of code just go throw that

    http://crazzyjava.blogspot.com/

    within 2 hours you will get solution..

    Just post your question....

    I have try ...Great....



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories