hey guys,

I drew many lines for the purpose of my project but for some reason, I cannot draw this specific line(code line 44) and cannot figure out why.

Note: I have debugged the code and this code line is red by Java but it's not applied to the graphic.

package projectilemotion;

/**
 *
 * @author Taimoor
 */
import javax.swing.*;
import java.awt.*;

public class DrawProjectilePath extends JPanel
{
    int yIncrement = 535;
    int xIncrement = 25;
    final int xLeft = 23;
    final int xRight = 27;

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        this.setBackground(Color.LIGHT_GRAY);

        g.drawString("Y Axes", 20, 25);
        g.drawLine(25, 25, 25, 535);
        g.drawLine(26, 25, 26, 535);
        g.drawLine(25, 25, 20, 30);
        g.drawLine(25, 26, 20, 31);
        g.drawLine(26, 25, 31, 30);
        g.drawLine(27, 27, 31, 31);
                
        g.drawString("X Axes", 570, 550);
        g.drawLine(25, 535, 575, 535);
        g.drawLine(25, 536, 575, 536);
        g.drawLine(575, 535, 570, 530);
        g.drawLine(574, 535, 569, 530);
        g.drawLine(575, 536, 570, 541);
        g.drawLine(574, 536, 569, 541);
        //g.drawLine(23, 408, 27, 408);

        while (yIncrement > 25)
        {
            yIncrement -= 127;
            g.setColor(Color.RED);
            g.drawLine(xRight, yIncrement, xLeft, yIncrement);
            
        }

    }

}
Member Avatar for audiomatic

You are decrementing yIncrement to the point that it will reach a negative number. I don't think you can map out to a negative coordinate.

You are decrementing yIncrement to the point that it will reach a negative number. I don't think you can map out to a negative coordinate.

hey,

It will not draw line with negative value since while(yIncrement > 25) ...

You never reset yIncrement though, so after the first time paintComponent is called, possibly even before the window is completely done showing itself, it will go below 25 and never again enter that loop. paintComponent() gets called every time the component needs to be redrawn, which can be triggered by many things such as layout change, window resize, dragging another window over it, etc. The method needs to re-render the current state of the component from scratch when called, so keep that in mind when writing the code.

Hey Ezzaral,

The first time the loop runs, shouldn't it draw the line at the x,y coordinate location mentioned? Cuz I know where that specified location on my screen is, but i do not see any line there. Also, I tried replacing line 44 by System.out.print("hello") and the system does print out "hello" about 5 times. So i dont understant y it wouldnt draw the lines...

Put the same println outside the loop in paintComponent() and you'll see it's called three times by the time the screen is finished displaying itself.

You are right Ezzaral, it prints it twice even thought it is told to print once only. So what do i need to do?

Thank you again Ezzaral, you are great :-). I fixed the problem by reinitializing yIncrement in the PaintComponent method.

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.