Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 11 ■ DEBUGGING WITH ECLIPSE

Figure 11-10. Incorrect values in the debugger


The thing that tipped me off was that the endX and endY values were the same as the value for the
length of the line. Those two values should be somewhere near the middle of the width and height of the
panel, which would be 200 for endX and 100 for endY (though endX could vary a great deal more than endY
because of the random starting X location).
So, on my second debug run, I paid special attention to where endX and endY got defined: in the two
lines just before the drawLine method call. That’s when I realized that I had forgotten to account for the
offset from the edges of the screen (one of those palm-to-forehead moments wherein I call myself an
idiot, but at least I found the problem).
Finally, I corrected the code to the version you saw in Chapter 10, which I'll give you again in
Listing 11-2.


Listing 11-2. The corrected 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() + burstX;
    int endY = new Double(yPrime).intValue() + burstY;

Free download pdf