Drawing consecutive rectangles with an algorithm












4















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.










share|improve this question









New contributor




Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.

    – Mast
    7 hours ago
















4















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.










share|improve this question









New contributor




Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.

    – Mast
    7 hours ago














4












4








4


1






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.










share|improve this question









New contributor




Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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.







java beginner algorithm homework graphics






share|improve this question









New contributor




Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 7 hours ago









Mast

7,44763686




7,44763686






New contributor




Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Luke KellyLuke Kelly

213




213




New contributor




Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.

    – Mast
    7 hours ago



















  • Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.

    – Mast
    7 hours ago

















Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.

– Mast
7 hours ago





Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers.

– Mast
7 hours ago










1 Answer
1






active

oldest

votes


















2














Naming




    public static void drawRectangle(Graphics g, int xCenter, int yCenter, int levelsNeeded){



I would call this drawRectangles, as it draws four rectangles on each iteration of the loop. Also, that leaves room for a drawRectangle method.



Typos




    //all squres are based off an 8 pixel system.  I use this to shift things around ALOT.



This has two typos.



    // all squares are based off an 8 pixel system.  I use this to shift things around A LOT.


Comments




    Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT); //Sets Origin to top left of inner most set of squares



If you break this up into two lines, it's easier to read



    // Sets Origin to top left of inner most set of squares
Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT);


And it doesn't put a scroll bar on my StackExchange screen.



Refactoring




        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



You repeat this pattern four times. Consider



    public static void drawRectangle(Graphics g, int x, int y, int width, int height, Color color) {
g.setColor(color);
g.fillRect(x, y, width, height);

g.setColor(Color.BLACK);
g.drawRect(x, y, width, height);
}


Then we can just write



    // the corner coordinates change by a constant amount each level
final int maximumDelta = levelsNeeded * CHANGECONSTANT;
for (int delta = 0; delta < maximumDelta; delta += CHANGECONSTANT)
{
final int left = (int)origin.getX() - delta;
final int top = (int)origin.getY() - delta;

// the width and height change twice as fast as the corner locations
final int wide = narrow + 2 * delta;
final int tall = small + 2 * delta;

drawRectangle(g, left, top, narrow, tall, Color.GRAY);
drawRectangle(g, left, bottom - delta, wide, small, Color.GRAY);
drawRectangle(g, right - delta, top, wide, small, Color.WHITE);
drawRectangle(g, right + delta, bottom + delta, narrow, tall, Color.WHITE);
}


This assumes a previous change of width and height to narrow and small. We still have width and height in the helper method. Add right and bottom with them, outside the loop.



    final int right = (int)origin.getX() + CHANGECONSTANT;
final int bottom = (int)origin.getY() + CHANGECONSTANT;


This is both shorter and more readable in my opinion. Rather than recalculating everything as needed, it calculates everything once. By using these names, we can easily see that a particular rectangle is the "left, top" that is drawn gray in "narrow, tall" aspect. We don't need comments, as these names are self-documenting.



We can make all of these final, but you don't need to do so.



I eliminated dcg. It was confusingly named and as I looked at it more carefully, unnecessary.



You could also make things easier by changing this from a static (class) method to an object method. Then your constructor could take a Graphics object. That would save passing it around all the time. You don't provide enough context to say whether this is a good idea. This might require moving this method to a different class.



I would add an underscore to CHANGECONSTANT and call it CHANGE_CONSTANT. Or change the name to something else entirely, e.g. INTERVAL or STANDARD_DIMENSION.



You haven't provided driver code; I haven't tried to run this version to see if it gives the same behavior. Beware of errors that might have been found by running the code.






share|improve this answer























    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Luke Kelly is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211386%2fdrawing-consecutive-rectangles-with-an-algorithm%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Naming




        public static void drawRectangle(Graphics g, int xCenter, int yCenter, int levelsNeeded){



    I would call this drawRectangles, as it draws four rectangles on each iteration of the loop. Also, that leaves room for a drawRectangle method.



    Typos




        //all squres are based off an 8 pixel system.  I use this to shift things around ALOT.



    This has two typos.



        // all squares are based off an 8 pixel system.  I use this to shift things around A LOT.


    Comments




        Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT); //Sets Origin to top left of inner most set of squares



    If you break this up into two lines, it's easier to read



        // Sets Origin to top left of inner most set of squares
    Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT);


    And it doesn't put a scroll bar on my StackExchange screen.



    Refactoring




            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



    You repeat this pattern four times. Consider



        public static void drawRectangle(Graphics g, int x, int y, int width, int height, Color color) {
    g.setColor(color);
    g.fillRect(x, y, width, height);

    g.setColor(Color.BLACK);
    g.drawRect(x, y, width, height);
    }


    Then we can just write



        // the corner coordinates change by a constant amount each level
    final int maximumDelta = levelsNeeded * CHANGECONSTANT;
    for (int delta = 0; delta < maximumDelta; delta += CHANGECONSTANT)
    {
    final int left = (int)origin.getX() - delta;
    final int top = (int)origin.getY() - delta;

    // the width and height change twice as fast as the corner locations
    final int wide = narrow + 2 * delta;
    final int tall = small + 2 * delta;

    drawRectangle(g, left, top, narrow, tall, Color.GRAY);
    drawRectangle(g, left, bottom - delta, wide, small, Color.GRAY);
    drawRectangle(g, right - delta, top, wide, small, Color.WHITE);
    drawRectangle(g, right + delta, bottom + delta, narrow, tall, Color.WHITE);
    }


    This assumes a previous change of width and height to narrow and small. We still have width and height in the helper method. Add right and bottom with them, outside the loop.



        final int right = (int)origin.getX() + CHANGECONSTANT;
    final int bottom = (int)origin.getY() + CHANGECONSTANT;


    This is both shorter and more readable in my opinion. Rather than recalculating everything as needed, it calculates everything once. By using these names, we can easily see that a particular rectangle is the "left, top" that is drawn gray in "narrow, tall" aspect. We don't need comments, as these names are self-documenting.



    We can make all of these final, but you don't need to do so.



    I eliminated dcg. It was confusingly named and as I looked at it more carefully, unnecessary.



    You could also make things easier by changing this from a static (class) method to an object method. Then your constructor could take a Graphics object. That would save passing it around all the time. You don't provide enough context to say whether this is a good idea. This might require moving this method to a different class.



    I would add an underscore to CHANGECONSTANT and call it CHANGE_CONSTANT. Or change the name to something else entirely, e.g. INTERVAL or STANDARD_DIMENSION.



    You haven't provided driver code; I haven't tried to run this version to see if it gives the same behavior. Beware of errors that might have been found by running the code.






    share|improve this answer




























      2














      Naming




          public static void drawRectangle(Graphics g, int xCenter, int yCenter, int levelsNeeded){



      I would call this drawRectangles, as it draws four rectangles on each iteration of the loop. Also, that leaves room for a drawRectangle method.



      Typos




          //all squres are based off an 8 pixel system.  I use this to shift things around ALOT.



      This has two typos.



          // all squares are based off an 8 pixel system.  I use this to shift things around A LOT.


      Comments




          Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT); //Sets Origin to top left of inner most set of squares



      If you break this up into two lines, it's easier to read



          // Sets Origin to top left of inner most set of squares
      Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT);


      And it doesn't put a scroll bar on my StackExchange screen.



      Refactoring




              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



      You repeat this pattern four times. Consider



          public static void drawRectangle(Graphics g, int x, int y, int width, int height, Color color) {
      g.setColor(color);
      g.fillRect(x, y, width, height);

      g.setColor(Color.BLACK);
      g.drawRect(x, y, width, height);
      }


      Then we can just write



          // the corner coordinates change by a constant amount each level
      final int maximumDelta = levelsNeeded * CHANGECONSTANT;
      for (int delta = 0; delta < maximumDelta; delta += CHANGECONSTANT)
      {
      final int left = (int)origin.getX() - delta;
      final int top = (int)origin.getY() - delta;

      // the width and height change twice as fast as the corner locations
      final int wide = narrow + 2 * delta;
      final int tall = small + 2 * delta;

      drawRectangle(g, left, top, narrow, tall, Color.GRAY);
      drawRectangle(g, left, bottom - delta, wide, small, Color.GRAY);
      drawRectangle(g, right - delta, top, wide, small, Color.WHITE);
      drawRectangle(g, right + delta, bottom + delta, narrow, tall, Color.WHITE);
      }


      This assumes a previous change of width and height to narrow and small. We still have width and height in the helper method. Add right and bottom with them, outside the loop.



          final int right = (int)origin.getX() + CHANGECONSTANT;
      final int bottom = (int)origin.getY() + CHANGECONSTANT;


      This is both shorter and more readable in my opinion. Rather than recalculating everything as needed, it calculates everything once. By using these names, we can easily see that a particular rectangle is the "left, top" that is drawn gray in "narrow, tall" aspect. We don't need comments, as these names are self-documenting.



      We can make all of these final, but you don't need to do so.



      I eliminated dcg. It was confusingly named and as I looked at it more carefully, unnecessary.



      You could also make things easier by changing this from a static (class) method to an object method. Then your constructor could take a Graphics object. That would save passing it around all the time. You don't provide enough context to say whether this is a good idea. This might require moving this method to a different class.



      I would add an underscore to CHANGECONSTANT and call it CHANGE_CONSTANT. Or change the name to something else entirely, e.g. INTERVAL or STANDARD_DIMENSION.



      You haven't provided driver code; I haven't tried to run this version to see if it gives the same behavior. Beware of errors that might have been found by running the code.






      share|improve this answer


























        2












        2








        2







        Naming




            public static void drawRectangle(Graphics g, int xCenter, int yCenter, int levelsNeeded){



        I would call this drawRectangles, as it draws four rectangles on each iteration of the loop. Also, that leaves room for a drawRectangle method.



        Typos




            //all squres are based off an 8 pixel system.  I use this to shift things around ALOT.



        This has two typos.



            // all squares are based off an 8 pixel system.  I use this to shift things around A LOT.


        Comments




            Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT); //Sets Origin to top left of inner most set of squares



        If you break this up into two lines, it's easier to read



            // Sets Origin to top left of inner most set of squares
        Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT);


        And it doesn't put a scroll bar on my StackExchange screen.



        Refactoring




                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



        You repeat this pattern four times. Consider



            public static void drawRectangle(Graphics g, int x, int y, int width, int height, Color color) {
        g.setColor(color);
        g.fillRect(x, y, width, height);

        g.setColor(Color.BLACK);
        g.drawRect(x, y, width, height);
        }


        Then we can just write



            // the corner coordinates change by a constant amount each level
        final int maximumDelta = levelsNeeded * CHANGECONSTANT;
        for (int delta = 0; delta < maximumDelta; delta += CHANGECONSTANT)
        {
        final int left = (int)origin.getX() - delta;
        final int top = (int)origin.getY() - delta;

        // the width and height change twice as fast as the corner locations
        final int wide = narrow + 2 * delta;
        final int tall = small + 2 * delta;

        drawRectangle(g, left, top, narrow, tall, Color.GRAY);
        drawRectangle(g, left, bottom - delta, wide, small, Color.GRAY);
        drawRectangle(g, right - delta, top, wide, small, Color.WHITE);
        drawRectangle(g, right + delta, bottom + delta, narrow, tall, Color.WHITE);
        }


        This assumes a previous change of width and height to narrow and small. We still have width and height in the helper method. Add right and bottom with them, outside the loop.



            final int right = (int)origin.getX() + CHANGECONSTANT;
        final int bottom = (int)origin.getY() + CHANGECONSTANT;


        This is both shorter and more readable in my opinion. Rather than recalculating everything as needed, it calculates everything once. By using these names, we can easily see that a particular rectangle is the "left, top" that is drawn gray in "narrow, tall" aspect. We don't need comments, as these names are self-documenting.



        We can make all of these final, but you don't need to do so.



        I eliminated dcg. It was confusingly named and as I looked at it more carefully, unnecessary.



        You could also make things easier by changing this from a static (class) method to an object method. Then your constructor could take a Graphics object. That would save passing it around all the time. You don't provide enough context to say whether this is a good idea. This might require moving this method to a different class.



        I would add an underscore to CHANGECONSTANT and call it CHANGE_CONSTANT. Or change the name to something else entirely, e.g. INTERVAL or STANDARD_DIMENSION.



        You haven't provided driver code; I haven't tried to run this version to see if it gives the same behavior. Beware of errors that might have been found by running the code.






        share|improve this answer













        Naming




            public static void drawRectangle(Graphics g, int xCenter, int yCenter, int levelsNeeded){



        I would call this drawRectangles, as it draws four rectangles on each iteration of the loop. Also, that leaves room for a drawRectangle method.



        Typos




            //all squres are based off an 8 pixel system.  I use this to shift things around ALOT.



        This has two typos.



            // all squares are based off an 8 pixel system.  I use this to shift things around A LOT.


        Comments




            Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT); //Sets Origin to top left of inner most set of squares



        If you break this up into two lines, it's easier to read



            // Sets Origin to top left of inner most set of squares
        Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT);


        And it doesn't put a scroll bar on my StackExchange screen.



        Refactoring




                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



        You repeat this pattern four times. Consider



            public static void drawRectangle(Graphics g, int x, int y, int width, int height, Color color) {
        g.setColor(color);
        g.fillRect(x, y, width, height);

        g.setColor(Color.BLACK);
        g.drawRect(x, y, width, height);
        }


        Then we can just write



            // the corner coordinates change by a constant amount each level
        final int maximumDelta = levelsNeeded * CHANGECONSTANT;
        for (int delta = 0; delta < maximumDelta; delta += CHANGECONSTANT)
        {
        final int left = (int)origin.getX() - delta;
        final int top = (int)origin.getY() - delta;

        // the width and height change twice as fast as the corner locations
        final int wide = narrow + 2 * delta;
        final int tall = small + 2 * delta;

        drawRectangle(g, left, top, narrow, tall, Color.GRAY);
        drawRectangle(g, left, bottom - delta, wide, small, Color.GRAY);
        drawRectangle(g, right - delta, top, wide, small, Color.WHITE);
        drawRectangle(g, right + delta, bottom + delta, narrow, tall, Color.WHITE);
        }


        This assumes a previous change of width and height to narrow and small. We still have width and height in the helper method. Add right and bottom with them, outside the loop.



            final int right = (int)origin.getX() + CHANGECONSTANT;
        final int bottom = (int)origin.getY() + CHANGECONSTANT;


        This is both shorter and more readable in my opinion. Rather than recalculating everything as needed, it calculates everything once. By using these names, we can easily see that a particular rectangle is the "left, top" that is drawn gray in "narrow, tall" aspect. We don't need comments, as these names are self-documenting.



        We can make all of these final, but you don't need to do so.



        I eliminated dcg. It was confusingly named and as I looked at it more carefully, unnecessary.



        You could also make things easier by changing this from a static (class) method to an object method. Then your constructor could take a Graphics object. That would save passing it around all the time. You don't provide enough context to say whether this is a good idea. This might require moving this method to a different class.



        I would add an underscore to CHANGECONSTANT and call it CHANGE_CONSTANT. Or change the name to something else entirely, e.g. INTERVAL or STANDARD_DIMENSION.



        You haven't provided driver code; I haven't tried to run this version to see if it gives the same behavior. Beware of errors that might have been found by running the code.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 18 hours ago









        mdfst13mdfst13

        17.4k52156




        17.4k52156






















            Luke Kelly is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Luke Kelly is a new contributor. Be nice, and check out our Code of Conduct.













            Luke Kelly is a new contributor. Be nice, and check out our Code of Conduct.












            Luke Kelly is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211386%2fdrawing-consecutive-rectangles-with-an-algorithm%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Terni

            A new problem with tex4ht and tikz

            Sun Ra