Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Practice MT2: Q9 (drawSquare)

+7 votes

How did you do the drawSquare method?  

(Note: You can use the <>  "add code" button in the Q&A to add better-formatted Java code to your answer.)

asked Jan 25, 2015 in (Winter 14) by Forrest Stonedahl (3,438 points)

1 Answer

+2 votes

Since we make a method with 4 parameters and no return values. it will be a simple method header consisting of one Graphics parameter and 3 int parameters. The header would look as follows:

public static void drawSquare(Graphics g, int x, int y, int length){

we want to draw the yellow box first because the boarder and lines overlay the square. what is drawn first will be "underneath" the next drawings.

we would first change the color of the pen to yellow then fill the square. the code is as follows:

g.setColor(Color.YELLOW);
g.fillRect(x,y,length,length);

since x and y are the upper left coordiantes of the square, not much math is needed and those variables can be placed directly in the fillRect method. similarly, the length variable is the length sides of the square which can just be put in as length and length for width and height.

Next, we would change the color of the "pen" to blue to draw our blue outline of the square. after we change the color, we would then simply DRAW the rect rather than FILL the rect to just draw the outline of it. using the same values as our fillRect will result in a border around the rect. code is as follows:

g.setColor(Color.BLUE);
g.drawRect(x,y,length,length);

next, we want to draw the black lines through the center of the square.. first we would set the color to black of the pen because we want black lines.. next takes a little thinking.. We want the line to start directly in the middle of one side and run the length of the square. so if we took the length and divided it by two, that would be the center of the side. However! our x position can be different.... if we were to start the square at (0,0) all would be good with setting the lines to start at half the side. However, the lines would have to be adjustable with different top left origins. this can be done by just adding x to the x start position. For example...

if we started our square at 5 pixels to the right with a side length of 10 using the "x start " of the line being (length/2).. our line would start at 5 pixels to the right of the drawing panel.. this would draw a line down the side that is already drawn.. to combat this. we add our "origin x" to our "line start x" resulting in our "line start x" to be (length/2 + x) making it start 10 pixels to the right of the drawing panel.. which is where the center of our square side would be.

we might make the "center" of a side a variable so it is cleaner to enter in to draw methods and we dont have to retype it many times.. an example of this section of code may be:

int centersidex = length/2 + x;
int centersidey = length/2 + y;
g.setColor(Color.BLACK);
//draw the verticle line
g.drawLine(centersidex, y, centersidex, y + length);
//the first y is the amount of pixles down that the line will start to draw
//which happens to be the same amount down as the top of the square
//(also the same y coordiante as the top left of the square or simply.. 'y'

//draw the horizontal line
g.drawLine(centersidey,x,centersidey, x + length);
//this will begin the line at the center of the verticle side of the square
//also starting at x pixels to the right. ending at the center of the other
//verticle side, and the length of a square side long.

A Full code would look as follows: 

public static void drawSquare(Graphics g, int x, int y, int length){
//set pen color to yellow
g.setColor(Color.YELLOW);
//create the yellow square
g.fillRect(x,y,length,length);
//set pen color to blue and draw the outline
g.setColor(Color.BLUE);
g.drawRect(x,y,length,length);
//find the center of each of the sides no matter what the square's origin is
int centersidex = length/2 + x;
int centersidey = length/2 + y;
g.setColor(Color.BLACK);
//draw the verticle line
g.drawLine(centersidex, y, centersidex, y + length);
//the first y is the amount of pixles down that the line will start to draw
//which happens to be the same amount down as the top of the square
//(also the same y coordiante as the top left of the square or simply.. 'y'

//draw the horizontal line
g.drawLine(centersidey,x,centersidey, x + length);
//this will begin the line at the center of the verticle side of the square
//also starting at x pixels to the right. ending at the center of the other
//verticle side, and the length of a square side long.
}

 

answered Jan 26, 2015 by Ethan Blatti (100 points)
I have not tested this answer, but you can try it out yourself in Eclipse... just make a main() method that calls drawSquare(...) and see if it works!
This did NOT work! the line meant to go horizontal happened to go vertical! so.... the following changes were made:

"g.drawLine(centersidey,x,centersidey, x + length);"

was changed to:

"g.drawLine(x,centersidey,x + length, centersidey);"
...