I'm encountering an issue with drawing squares using the CodeDraw
library in Java. I'm trying to draw a series of red and blue squares vertically & horizontally on a canvas. The images are down below.
However, I've noticed that the bottom line of the last blue square on the right side is always missing, no matter what i do.
Here's my code:
import codedraw.CodeDraw;
import codedraw.Palette;
public class Aufgabe5 {
// Constants defining canvas dimensions
private static final double CANVAS_WIDTH = 300.0;
private static final double CANVAS_HEIGHT = 300.0;
private static final double MIN_X = 0.0;
private static final double MAX_X = CANVAS_WIDTH;
private static final double MIN_Y = 0.0;
private static final double MAX_Y = CANVAS_HEIGHT;
private static final double n = 9.0;
private static int i;
public static void main(String[] args) {
CodeDraw myDrawObj = new CodeDraw(300, 300);
double fraction = 0.0;
boolean inRange = (n >= 5 && n <= 19);
boolean isOdd = (n % 2 != 0);
if (inRange && isOdd) {
for (i = 0; i <= n; i++) {
drawSquares(myDrawObj, fraction);
fraction = i / n;
}
} else {
System.out.println("Invalid Input: n has to be an odd number between 5 and 19.");
}
myDrawObj.show();
}
private static void drawSquares(CodeDraw myDrawObj, double fraction) {
double sideLength = CANVAS_WIDTH / n;
myDrawObj.setLineWidth(4);
myDrawObj.setColor(Palette.RED);
myDrawObj.drawSquare(MAX_X * fraction, MAX_Y * fraction, sideLength);
myDrawObj.setLineWidth(2);
myDrawObj.setColor(Palette.BLUE);
myDrawObj.drawSquare(MIN_X, MAX_Y - (MAX_Y * fraction), sideLength);
if (i < n) {
myDrawObj.drawSquare((MAX_X - sideLength), (MAX_Y * fraction), sideLength);
}
}
}
Notice the missing bottom line of the last blue square on the right.
I have tried many things but none seem to work for me. the closest I've come to having a complete square is when in the main method, i changed the for (i = 0; i <= n; i++)
to for (i = 0; i < n; i++)
. While it does give me a full last Square, it also removes the Red last Square down below as well as the top right Blue Square. Idk where I messed up.
I would appreciate any insights || any possible solutions, thanks. (Also, the task description doesn't allow the Red Squares being drawn over the Blue Squares.)
This is my solution right now. I couldn't cram everything into one method for cleaner code, so I split each square set into separate methods. Also, I split the main loop into two to handle different conditions. It feels a bit redundant, but it gets the job done for the exercise. New Output: https://i.sstatic.net/CEGs1.png
import codedraw.Palette;
public class Task5 {
private static double CANVAS_WIDTH = 300.0;
private static double CANVAS_HEIGHT = 300.0;
private static double MIN_X = 0.0;
private static double MAX_X = CANVAS_WIDTH;
private static double MIN_Y = 0.0;
private static double MAX_Y = CANVAS_HEIGHT;
private static double n = 9.0;
public static void main(String[] args) {
CodeDraw myDrawObj = new CodeDraw(300, 300);
double fraction = 0.0;
boolean inRange = (n >= 5 && n <= 19);
boolean isOdd = (n % 2 != 0);
if (inRange && isOdd) {
for (int i = 0; i <= n; i++) {
drawRedSquares(myDrawObj, fraction);
drawBlueSquares1(myDrawObj, fraction);
fraction = i / n;
}
for (int i = 0; i < n; i++) {
drawBlueSquares2(myDrawObj, fraction);
fraction = i / n;
}
} else {
System.out.println("Invalid Input: n has to be an odd number between 5 and 19.");
}
myDrawObj.show();
}
private static void drawRedSquares(CodeDraw myDrawObj, double fraction) {
double sideLength = CANVAS_WIDTH / n;
myDrawObj.setLineWidth(4);
myDrawObj.setColor(Palette.RED);
myDrawObj.drawSquare(MAX_X * fraction, MAX_Y * fraction, sideLength);
}
private static void drawBlueSquares1(CodeDraw myDrawObj, double fraction) {
double sideLength = CANVAS_WIDTH / n;
myDrawObj.setLineWidth(2);
myDrawObj.setColor(Palette.BLUE);
if (fraction * MAX_Y > MIN_Y) {
myDrawObj.drawSquare(MIN_X, MAX_Y * fraction, sideLength);
}
}
public static void drawBlueSquares2(CodeDraw myDrawObj, double fraction) {
double sideLength = CANVAS_WIDTH / n;
if (fraction * MAX_Y < MAX_Y) {
myDrawObj.drawSquare((MAX_X - sideLength), MAX_Y * fraction, sideLength);
}
}
}