JAVA HELP please public static void drawRow Graphics g int x
JAVA HELP please
public static void drawRow (Graphics g, int x, int y, int numberPairs, int sizeBox){
for(int i = 0; i < numberPairs; i++){
g.setColor(Color.BLACK);
g.fillRect(x + 2 * i * sizeBox, y, sizeBox, sizeBox);
g.setColor(Color.BLUE);
g.drawLine(x + 2 * i * sizeBox, y, x + 2 * i * sizeBox + sizeBox, y + sizeBox);
g.drawLine(x + 2 * i * sizeBox, y + sizeBox, x + 2 * i * sizeBox + sizeBox, y);
g.setColor(Color.WHITE);
g.fillRect(x + 2 * i * sizeBox + sizeBox, y, sizeBox, sizeBox);
}
}
Is there a simpler way to write this for loop, especially for the parts that are bolded. This is JAVA.
Solution
One way you can do this above given code block to write efficiently and in a simpler way is by taking out the common factor in the statements within the loop and execute it once and save it to a variable. Then afterwards, instead of computing its value again and again, just put the variable name there...thats it. It will look like shown below:
