I have an assignment to draw rectangles that get larger and larger each time I draw them. Just like shown below. I’ve labeled them TL = Top Left, TR = Top Right, BL = Bottom Left, BR = Bottom Right. The smallest boxes are 8 pixels by 8 pixels. From this drawing I pulled these algorithms for each box. Where n represents the level you are drawing. Level 0 is the inside set of four and grows out by 1.

`TL = (x-n(8), y - n(8)) TR = ((x + 8) - (n*8), y - n(8)) BL = (x - n(8), y - (n+1)(8)) BR = ((x+8)+(n*8), (y+8)-(n*8)); `

My real questions is this. I feel like I can combine these algorithms into something smaller but i’m new to this type of thinking. This is the first Computer Science Class I’ve taken that required me to do this type of thinking. Do you have any suggestions?

From these algorithms I wrote this code in Java.

` public static void drawRectangle(Graphics g, int xCenter, int yCenter, int levelsNeeded){ //all squres are based off an 8 pixel system. I use this to shift things around ALOT. final int CHANGECONSTANT = 8; int width = 8; int height = 8; int dcg = 1; //Dimension Change Factor Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT); //Sets Origin to top left of inner most set of squares for (int level = 0; level < levelsNeeded; level++) { g.setColor(Color.GRAY); //Top Left g.fillRect( (int)origin.getX()-(level*CHANGECONSTANT), // XCord (int)origin.getY()-(level*CHANGECONSTANT), // YCord width, //width. This stays at 8 for all iterations height*dcg);//height. This gets multiplied by odd factors each time //Draw outline g.setColor(Color.BLACK); g.drawRect( (int)origin.getX()-(level*CHANGECONSTANT), // XCord (int)origin.getY()-(level*CHANGECONSTANT), // YCord width, //width. This stays at 8 for all iterations height*dcg);//height. This gets multiplied by odd factors each time g.setColor(Color.GRAY); //Bottom Left g.fillRect( (int)origin.getX()-(level*CHANGECONSTANT), //XCrod (int)origin.getY()+((level+1)*CHANGECONSTANT), //YCord width*dcg, //width. This gets multiplied by odd factors each time height); //height. This stays at 8 for all iterations g.setColor(Color.BLACK); //Draw Outline g.drawRect( (int)origin.getX()-(level*CHANGECONSTANT), //XCrod (int)origin.getY()+((level+1)*CHANGECONSTANT), //YCord width*dcg, //width. This gets multiplied by odd factors each time height); //height. This stays at 8 for all iterations g.setColor(Color.WHITE); //Top Right g.fillRect( ((int)origin.getX()+CHANGECONSTANT)-(level*CHANGECONSTANT), //XCord (int)origin.getY()-(level*CHANGECONSTANT), //YCord width*dcg,//width. This gets multiplied by odd factors each time height);//height. This stays at 8 for all iterations //Draw Outline g.setColor(Color.BLACK); g.drawRect( ((int)origin.getX()+CHANGECONSTANT)-(level*CHANGECONSTANT), //XCord (int)origin.getY()-(level*CHANGECONSTANT), //YCord width*dcg,//width. This gets multiplied by odd factors each time height);//height. This stays at 8 for all iterations g.setColor(Color.WHITE); //Bottom Right g.fillRect( ((int)origin.getX()+CHANGECONSTANT)+(level*CHANGECONSTANT), //Xcord ((int)origin.getY()+CHANGECONSTANT)-(level*CHANGECONSTANT), //Ycord width,//width. This gets multiplied by odd factors each time height*dcg);//Height. This stays at 8 for all iterations. //Draw Outline g.setColor(Color.BLACK); g.drawRect( ((int)origin.getX()+CHANGECONSTANT)+(level*CHANGECONSTANT), //Xcord ((int)origin.getY()+CHANGECONSTANT)-(level*CHANGECONSTANT), //Ycord width,//width. This gets multiplied by odd factors each time height*dcg);//Height. This stays at 8 for all iterations. dcg += 2; //Increment DCG by two at end of each loop } } `

Here is what my final product looks like right now. Disregard the triangle shapes that a different method that calls the first one multiple times.

This is my first quarter learning Java and i’m new to programming.