CHAPTER 11 ■ DEBUGGING WITH ECLIPSE
developers are good musicians, master video game players, or both. All three pursuits require good
visualization skills, and all good developers are good puzzle solvers for the same reason. More so than
formal education or even experience, visualization is the key to being a good developer. Of course,
education and experience help you develop and refine your visualization skill.
Fixing the Fireworks Program
Now that you know enough to be able to understand how I used the debugger to find and fix the issue,
we can return to my problem of making searchlights when I wanted fireworks.
The first thing I had to do was figure out where the problem was happening. Fortunately, I had
isolated all of the code for drawing an individual firework into the draw() method of the Firework class.
Remember, each method should do one easily identified task. Making debugging easier is one of the
biggest reasons Java developers follow that practice.
Fireworks programListing 11-1 shows the version of the method that was causing the problem.
Listing 11-1. The flawed Firework.draw() method
void draw(Graphics g) {
Color drawColor = color;
g.setColor(drawColor);
int height = panelHeight - panelHeight / steps * currentStep;
if (height > burstY) {
g.drawLine(startX, startY, burstX, height);
}
if(currentStep >= burstStep) {
for (int i = 0; i < 12; i++) {
double xPrime, yPrime;
double currentRadians = Math.toRadians(30 * i);
int length = burstY / 2 / steps * currentStep;
xPrime = (Math.cos(currentRadians)
- Math.sin(currentRadians)) * length;
yPrime = (Math.sin(currentRadians)
- Math.cos(currentRadians)) * length;
int endX = new Double(xPrime).intValue();
int endY = new Double(yPrime).intValue();
g.drawLine(burstX, burstY, endX, endY);
}
}
currentStep++;
}
Within the draw method, I knew that the problem had to be inside the loop that drew the twelve
splines (the lines from the center to the edge of the firework). So, I was able to determine that a good
starting point for stepping through the code would be at the first line inside the for loop.
From that breakpoint, I stepped over each line to the last line inside the for loop (that is, until I got
to the drawLine method). At that point, I could see that endX and endY had values I didn't expect, and I
had my first clue. (Debugging often makes me feel like a detective—maybe that's why I like Sherlock
Holmes so much.) Figure 11-10 shows the values in the debugger.